There are many libraries that golang implement encryption and decryption, and here we use the aes library + base64 library to implement it.
When using it, you need to specify a private key for encryption and decryption. Here is the following:
var aeskey = []byte(“321423u9y8d2fwfl”)
On code:
package main import ( "fmt" "crypto/cipher" "crypto/aes" "bytes" "encoding/base64" ) func PKCS5Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize padtext := ([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) } func PKCS5UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] } func AesEncrypt(origData, key []byte) ([]byte, error) { block, err := (key) if err != nil { return nil, err } blockSize := () origData = PKCS5Padding(origData, blockSize) blockMode := (block, key[:blockSize]) crypted := make([]byte, len(origData)) (crypted, origData) return crypted, nil } func AesDecrypt(crypted, key []byte) ([]byte, error) { block, err := (key) if err != nil { return nil, err } blockSize := () blockMode := (block, key[:blockSize]) origData := make([]byte, len(crypted)) (origData, crypted) origData = PKCS5UnPadding(origData) return origData, nil } func main() { var aeskey = []byte("321423u9y8d2fwfl") pass := []byte("vdncloud123456") xpass, err := AesEncrypt(pass, aeskey) if err != nil { (err) return } pass64 := (xpass) ("After encryption:%v\n",pass64) bytesPass, err := (pass64) if err != nil { (err) return } tpass, err := AesDecrypt(bytesPass, aeskey) if err != nil { (err) return } ("Decrypted:%s\n", tpass) }
Output:
After encryption: rLyZug0MCEF2TBcJdhMyjg==
After decryption: vdncloud123456
Supplement: Golang AES CBC Encryption
I won't say much nonsense, let's just read the code~
package main import ( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) const ( key = "2018201820182018" iv = "1234567887654321" ) func main() { str := "What the hell" es, _ := AesEncrypt(str, []byte(key)) (es) ds, _ := AesDecrypt(es, []byte(key)) (string(ds)) } func AesEncrypt(encodeStr string, key []byte) (string, error) { encodeBytes := []byte(encodeStr) //Create ciphertext based on key block, err := (key) if err != nil { return "", err } blockSize := () encodeBytes = PKCS5Padding(encodeBytes, blockSize) blockMode := (block, []byte(iv)) crypted := make([]byte, len(encodeBytes)) (crypted, encodeBytes) return (crypted), nil } func PKCS5Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize //filling padtext := ([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) } func AesDecrypt(decodeStr string, key []byte) ([]byte, error) { //Decrypt base64 first decodeBytes, err := (decodeStr) if err != nil { return nil, err } block, err := (key) if err != nil { return nil, err } blockMode := (block, []byte(iv)) origData := make([]byte, len(decodeBytes)) (origData, decodeBytes) origData = PKCS5UnPadding(origData) return origData, nil } func PKCS5UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.