packagemainimport("bytes""crypto/aes""crypto/cipher""encoding/hex""fmt")funcAesEncrypt(encryptStrstring,key[]byte,ivstring)(string,error){encryptBytes:=[]byte(encryptStr)block,err:=aes.NewCipher(key)iferr!=nil{return"",err}blockSize:=block.BlockSize()encryptBytes=pkcs5Padding(encryptBytes,blockSize)blockMode:=cipher.NewCBCEncrypter(block,[]byte(iv))encrypted:=make([]byte,len(encryptBytes))blockMode.CryptBlocks(encrypted,encryptBytes)returnhex.EncodeToString(encrypted),nil}funcAesDecrypt(decryptStrstring,key[]byte,ivstring)(string,error){decryptBytes,err:=hex.DecodeString(decryptStr)iferr!=nil{return"",err}block,err:=aes.NewCipher(key)iferr!=nil{return"",err}blockMode:=cipher.NewCBCDecrypter(block,[]byte(iv))decrypted:=make([]byte,len(decryptBytes))blockMode.CryptBlocks(decrypted,decryptBytes)decrypted=pkcs5UnPadding(decrypted)returnstring(decrypted),nil}funcpkcs5Padding(cipherText[]byte,blockSizeint)[]byte{padding:=blockSize-len(cipherText)%blockSizepadText:=bytes.Repeat([]byte{byte(padding)},padding)returnappend(cipherText,padText...)}funcpkcs5UnPadding(decrypted[]byte)[]byte{length:=len(decrypted)unPadding:=int(decrypted[length-1])returndecrypted[:(length-unPadding)]}funcmain(){data:="i am test data"key:=[]byte("1111111111111111")iv:="1111122211111111"encrypt,err:=AesEncrypt(data,key,iv)iferr!=nil{panic(err)return}fmt.Println(encrypt)decrypt,err:=AesDecrypt(encrypt,key,iv)iferr!=nil{panic(err)}fmt.Println(decrypt)}