package main import ( "crypto/aes" "crypto/cipher" "fmt" ) func main() { key := []byte("0123456789abcdef") // 128-bit key plaintext := []byte("Hello, world!") // message to encrypt block, err := aes.NewCipher(key) if err != nil { panic(err) } ciphertext := make([]byte, aes.BlockSize+len(plaintext)) iv := ciphertext[:aes.BlockSize] if _, err := rand.Read(iv); err != nil { panic(err) } mode := cipher.NewCBCEncrypter(block, iv) mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext) fmt.Printf("%x\n", ciphertext) }
package main import ( "crypto/des" "fmt" ) func main() { key := []byte("01234567") // 56-bit key plaintext := []byte("Hello, world!") // message to encrypt block, err := des.NewCipher(key) if err != nil { panic(err) } ciphertext := make([]byte, len(plaintext)) block.Encrypt(ciphertext, plaintext) fmt.Printf("%x\n", ciphertext) }In this program, we use a 56-bit DES key, which is less secure than AES, but still widely used in legacy systems. We create a DES cipher block, and use it to encrypt the plaintext in ECB mode (which doesn't require an IV). The resulting ciphertext is the same length as the plaintext.