package main import ( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) var key = []byte("1234567890abcdef") func padding(src []byte, blocksize int) []byte { padnum := blocksize - len(src)%blocksize pad := bytes.Repeat([]byte{byte(padnum)}, padnum) return append(src, pad...) } func unpadding(src []byte) []byte { n := len(src) unpadnum := int(src[n-1]) return src[:n-unpadnum] } func encryptAES(src []byte) []byte { block, _ := aes.NewCipher(key) src = padding(src, block.BlockSize()) blockmode := cipher.NewCBCEncrypter(block, key) blockmode.CryptBlocks(src, src) return src } func decryptAES(src []byte) []byte { block, _ := aes.NewCipher(key) blockmode := cipher.NewCBCDecrypter(block, key) blockmode.CryptBlocks(src, src) src = unpadding(src) return src } func main() { x := []byte("武汉加油") x1 := encryptAES(x) x2 := decryptAES(x1) fmt.Println(string(x2)) // base64编码 x3 := base64.StdEncoding.EncodeToString(x2) fmt.Println(x3) // base64解码 x4, _ := base64.StdEncoding.DecodeString(x3) fmt.Println(string(x4)) }