Example #1
0
func receiveDecrypt(conn net.Conn) (Message, error) {
	// Our work is:
	// (receive) -> [de-GOB] -> [DECRYPT] -> [de-GOB] -> msg

	// Receive data and de-serialize to get the encrypted message
	encMsg := new([]byte)
	receive := gob.NewDecoder(conn)
	if err := receive.Decode(encMsg); err != nil {
		return Message{}, err
	}

	// Create decrypter and pass it the encrypted message
	r := bytes.NewReader(*encMsg)
	decrypter, err := saltsecret.NewReader(r, conf.Key, saltsecret.DECRYPT, false)
	if err != nil {
		return Message{}, err
	}

	// Read unencrypted serialized message and de-serialize it
	msg := new(Message)
	dec := gob.NewDecoder(decrypter)
	if err = dec.Decode(msg); err != nil {
		return Message{}, err
	}

	return *msg, nil
}
Example #2
0
func main() {
	// A 1 kilobyte message
	msg := []byte(`Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam egestas mi ut nisl lobortis rhoncus. Cras et tempus elit. Maecenas dictum viverra pretium. Integer porta felis lacus, ac maximus risus volutpat vel. Vivamus fermentum vitae turpis vitae lobortis. Quisque dictum, nunc eget blandit porttitor, risus nisl aliquam ante, vel congue nibh metus at velit. Suspendisse potenti. Pellentesque quis ipsum vitae tortor condimentum malesuada id vitae enim. Mauris mattis elit quis nibh venenatis, ut finibus mauris pulvinar. Duis facilisis tellus nec laoreet cursus. Sed mattis condimentum condimentum. Aenean a leo vel urna pharetra scelerisque tempor accumsan augue. Sed vel ante id turpis tempus ornare. Quisque lobortis enim auctor ipsum lacinia, et bibendum tortor pellentesque.

Vivamus ut elit nec arcu congue malesuada nec eget enim. Aliquam erat volutpat. Phasellus auctor consequat est et hendrerit. Nullam vitae odio ac nisi blandit viverra. Curabitur consequat urna quis ante molestie viverra. Donec malesuada amet.`)

	// Create a cryptographer Writer instance which encrypts with compression enabled and "qwerty" as key.
	var encryptedMsg bytes.Buffer
	w, err := saltsecret.NewWriter(&encryptedMsg, []byte("qwerty"), saltsecret.ENCRYPT, true)
	if err != nil {
		log.Fatalln(err)
	}
	// Write the msg to Writer
	w.Write(msg)
	// Call Flush (or Close) so the Writer knows it got all the data and thus can encrypt them.
	w.Flush()

	// Create a cryptographer Reader instance with "qwerty" key that decrypts and pass to it the encrypted message.
	r, err := saltsecret.NewReader(bytes.NewReader(encryptedMsg.Bytes()), []byte("qwerty"), saltsecret.DECRYPT, false)
	if err != nil {
		log.Fatalln(err)
	}
	// Grab the decrypted message from r.
	decryptedMsg, err := ioutil.ReadAll(r)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println("Decrypted message is:\n" + string(decryptedMsg))

	// Re-use Writer and Reader

	msgNew := []byte("Hello world")

	// Reset the Writer so we may re-use it:
	encryptedMsg.Reset()
	w.Reset(&encryptedMsg)
	// Write the new message and Close (or Flush, exactly the same):
	w.Write(msgNew)
	w.Close()

	// Reset the Reader so we may re-use it:
	r.Reset(bytes.NewReader(encryptedMsg.Bytes()))
	// Now grab the new message:
	decryptedMsg, err = ioutil.ReadAll(r)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println("Decrypted new message is:\n" + string(decryptedMsg))
}