package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
const (
iv = "c87af41b409df323" //16位
)
// PKCS5填充方式
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
//填充
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
// Zero填充方式
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
//填充
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
// PKCS5 反填充
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// Zero反填充
func ZeroUnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// 加密
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 = ZeroPadding(encodeBytes, 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 AesDecrypt(decodeStr string, key []byte) ([]byte, error) {
decodeBytes, err := base64.StdEncoding.DecodeString(decodeStr) //先解密base64
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 = ZeroUnPadding(origData) //
origData = PKCS5UnPadding(origData) //
return origData, nil
}
func main() {
str := "NNJ6J7d4lE+ZrxHkJUU2Fg=="
es, _ := AesEncrypt("xixi575", []byte("c87af41b409df323d0f9ab6de2ce9022")) //16位key
fmt.Println(es) // 8lCuwh5+vZrZmU1J9c9Alw==
ds, err := AesDecrypt(str, []byte("c87af41b409df323d0f9ab6de2ce9022"))
if err != nil {
fmt.Println((err))
} else {
fmt.Println(string(ds))
}
}
AES在线解密 AES在线加密 Aes online hex 十六进制密钥 - The X 在线工具
对应的php代码:
$password = "xixixioahohno";
$a = base64_encode(openssl_encrypt($password, 'AES-256-CBC', "c87af41b409df323d0f9ab6de2ce9022" , OPENSSL_RAW_DATA, "c87af41b409df323"));
dump($a);
$a="NNJ6J7d4lE+ZrxHkJUU2Fg==";
$decrypted = openssl_decrypt(base64_decode($a), 'AES-256-CBC', "c87af41b409df323d0f9ab6de2ce9022", OPENSSL_RAW_DATA,"c87af41b409df323");
dump($decrypted);
参考如下代码
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
)
const (
iv = "1234567890ABCDEF" //16位
)
// PKCS5填充方式
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
//填充
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
// Zero填充方式
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
//填充
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
// PKCS5 反填充
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// Zero反填充
func ZeroUnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// 加密
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 = ZeroPadding(encodeBytes, blockSize) //PKCS5Padding(encodeBytes, blockSize)
blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
crypted := make([]byte, len(encodeBytes))
blockMode.CryptBlocks(crypted, encodeBytes)
hexstr := fmt.Sprintf("%x", crypted)
return hexstr, nil
//return base64.StdEncoding.EncodeToString(crypted), nil
}
// 解密
func AesDecrypt(decodeStr string, key []byte) ([]byte, error) {
//decodeBytes, err := base64.StdEncoding.DecodeString(decodeStr)//先解密base64
decodeBytes, err := hex.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 = ZeroUnPadding(origData) // origData = PKCS5UnPadding(origData)
return origData, nil
}
func main() {
str := "多可文档管理系统"
es, _ := AesEncrypt(str, []byte("2018201820182018")) //16位key
fmt.Println(es) // 8lCuwh5+vZrZmU1J9c9Alw==
ds, _ := AesDecrypt(es, []byte("2018201820182018"))
fmt.Println(string(ds))
}