Skip to content

Instantly share code, notes, and snippets.

@thinkclay
Created March 23, 2014 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkclay/9723296 to your computer and use it in GitHub Desktop.
Save thinkclay/9723296 to your computer and use it in GitHub Desktop.
GO: AES Example
package main
import (
"crypto/aes"
"crypto/cipher"
"fmt"
)
func main() {
input := []byte("this is a test")
iv := []byte("532b6195636c6127")[:aes.BlockSize]
key := []byte("532b6195636c61279a010000")
fmt.Println("Input: ", input)
fmt.Println("Key: ", key)
fmt.Println("IV: ", iv)
encrypted := make([]byte, len(input))
EncryptAES(encrypted, input, key, iv)
fmt.Println("Output: ", encrypted)
}
func EncryptAES(dst, src, key, iv []byte) error {
aesBlockEncryptor, err := aes.NewCipher([]byte(key))
if err != nil {
return err
}
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncryptor, iv)
aesEncrypter.XORKeyStream(dst, src)
return nil
}
func DecryptAES(dst, src, key, iv []byte) error {
aesBlockEncryptor, err := aes.NewCipher([]byte(key))
if err != nil {
return err
}
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncryptor, iv)
aesEncrypter.XORKeyStream(dst, src)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment