AES GCMAES GCM (Galois Counter Mode) is a stream cipher mode for AES. It is based on the CTR mode, but converts to a stream cipher. This provides low latency in the encryption/decryption process, and is fast to process. Along with this it integrates AEAD mode for authentication. |
Code
An outline of the code is:
package main import ( "crypto/aes" "crypto/cipher" "fmt" "io" "crypto/rand" "golang.org/x/crypto/pbkdf2" "crypto/sha256" ) func main() { passwd:="qwerty" nonce := make([]byte, 12) // 96 bits for nonce/IV if _, err := io.ReadFull(rand.Reader, nonce); err != nil { panic(err.Error()) } salt:=[]byte("000000000000") key := pbkdf2.Key([]byte(passwd), salt, 10000, 32, sha256.New) block, err := aes.NewCipher(key) if err != nil { panic(err.Error()) } msg:="hello" plaintext:=[]byte(msg) aesgcm, err := cipher.NewGCM(block) if err != nil { panic(err.Error()) } ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil) fmt.Printf("Message:\t%s\n", msg) fmt.Printf("Cipher:\t\t%x\n", ciphertext) fmt.Printf("Key:\t\t%x\n", key) fmt.Printf("Nonce:\t\t%x\n", nonce) plain, _ := aesgcm.Open(nil, nonce, ciphertext, nil) if err != nil { panic(err.Error()) } fmt.Printf("Decrypted:\t%s\n", plain) }