挫娃子

Golang AES CBC 加密(PHP7 openssl_encrypt)

package main

import (
   "fmt"
   "crypto/aes"
   "crypto/cipher"
   "encoding/base64"
   "bytes"
)

const (
   key = "X2bJDtJtM7IAmbpB"
   iv  = "9338010611913253"
)

func main() {
   str := "我勒个去"
   es, _ := AesEncrypt(str, []byte(key))
   fmt.Println(es)

   ds, _ := AesDecrypt(es, []byte(key))
   fmt.Println(string(ds))

}

func AesEncrypt(encodeStr string, key []byte) (string, error) {
   encodeBytes := []byte(encodeStr)
   //根据key 生成密文
   block, err := aes.NewCipher(key)
   if err != nil {
      return "", err
   }

   blockSize := block.BlockSize()
   encodeBytes = PKCS5Padding(encodeBytes, blockSize)

   blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
   crypted := make([]byte, len(encodeBytes))
   blockMode.CryptBlocks(crypted, encodeBytes)

   return base64.StdEncoding.EncodeToString(crypted), nil
}

func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
   padding := blockSize - len(ciphertext)%blockSize
   //填充
   padtext := bytes.Repeat([]byte{byte(padding)}, padding)

   return append(ciphertext, padtext...)
}

func AesDecrypt(decodeStr string, key []byte) ([]byte, error) {
   //先解密base64
   decodeBytes, err := base64.StdEncoding.DecodeString(decodeStr)
   if err != nil {
      return nil, err
   }
   block, err := aes.NewCipher(key)
   if err != nil {
      return nil, err
   }
   blockMode := cipher.NewCBCDecrypter(block, []byte(iv))
   origData := make([]byte, len(decodeBytes))

   blockMode.CryptBlocks(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)]
}
且行且珍惜,转载请注明来自挫娃子《Golang AES CBC 加密(PHP7 openssl_encrypt)》

评论