Golang : Encrypt and decrypt data with AES crypto
In this tutorial we will learn how to encrypt data with Golang's AES crypto package.
AES or Advanced Encryption Standard is encryption algorithm based on the Rijndael cipher developed by the Belgian cryptographers, Joan Daemen and Vincent Rijmen. AES was adopted for encryption by the United States government and is now used worldwide.
Below is the source code for encrypting and decrypting a 16 bytes string message :
package main
import (
"fmt"
"crypto/aes"
"crypto/cipher"
)
func main() {
//The key argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
key := "opensesame123456" // 16 bytes!
block,err := aes.NewCipher([]byte(key))
if err != nil {
panic(err)
}
fmt.Printf("%d bytes NewCipher key with block size of %d bytes\n", len(key), block.BlockSize)
str := []byte("Hello World!")
// 16 bytes for AES-128, 24 bytes for AES-192, 32 bytes for AES-256
ciphertext := []byte("abcdef1234567890")
iv := ciphertext[:aes.BlockSize] // const BlockSize = 16
// encrypt
encrypter := cipher.NewCFBEncrypter(block, iv)
encrypted := make([]byte, len(str))
encrypter.XORKeyStream(encrypted, str)
fmt.Printf("%s encrypted to %v\n", str, encrypted)
// decrypt
decrypter := cipher.NewCFBDecrypter(block, iv) // simple!
decrypted := make([]byte, len(str))
decrypter.XORKeyStream(decrypted, encrypted)
fmt.Printf("%v decrypt to %s\n", encrypted, decrypted)
}
Executing the above code will produce the following output :
16 bytes NewCipher key with block size of 9360 bytes
Hello World! encrypted to [246 121 236 39 139 97 102 181 16 102 237 145]
[246 121 236 39 139 97 102 181 16 102 237 145] decrypt to Hello World!
Hope this tutorial is useful for those learning Go and AES crypto.
Reference :
See also : Golang : md5 hash of a string
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.2k Golang : How to solve "too many .rsrc sections" error?
+28k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+4.8k Golang : Error handling methods
+15.7k Golang : How to tell if a file is compressed either gzip or zip ?
+25.1k Golang : Calculate future date with time.Add() function
+10.8k Golang : Determine if time variables have same calendar day
+16.5k Golang : Upload/Receive file progress indicator
+17.2k Golang : Send email with attachment
+6.5k Golang : Handling Yes No Quit query input
+14.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+4.1k Swift : Convert (cast) Float to Int or Int32 value
+10.8k Golang : Simple client-server HMAC authentication without SSL example