Symmetric key encryption

There are two major mechanisms used for encrypting data. The first uses a single key that is the same for both encryption and decryption. This key needs to be known to both the encrypting and the decrypting agents. How this key is transmitted between the agents is not discussed.

As with hashing, there are many encryption algorithms. Many are now known to have weaknesses, and in general algorithms become weaker over time as computers get faster. Go has support for several symmetric key algorithms such as Blowfish and DES.

The algorithms are block algorithms. That is they work on blocks of data. If your data is not aligned to the block size, then you will have to pad it with extra blanks at the end.

Each algorithm is represented by a Cipher object. This is created by NewCipher in the appropriate package, and takes the symmetric key as parameter.

Once you have a cipher, you can use it to encrypt and decrypt blocks of data. The blocks have to be 8-byte blocks for Blowfish. A program to illustrate this is

  1. /* Blowfish
  2. */
  3. package main
  4. import (
  5. "bytes"
  6. "golang.org/x/crypto/blowfish"
  7. "fmt"
  8. )
  9. func main() {
  10. key := []byte("my key")
  11. cipher, err := blowfish.NewCipher(key)
  12. if err != nil {
  13. fmt.Println(err.Error())
  14. }
  15. src := []byte("hello\n\n\n")
  16. var enc [512]byte
  17. cipher.Encrypt(enc[0:], src)
  18. var decrypt [8]byte
  19. cipher.Decrypt(decrypt[0:], enc[0:])
  20. result := bytes.NewBuffer(nil)
  21. result.Write(decrypt[0:8])
  22. fmt.Println(string(result.Bytes()))
  23. }

Blowfish is not in the Go 1 distribution. You have to install it by running go get golang.org/x/crypto/blowfish in a directory where you have source that needs to use it.