Go语言AES加密,PKCS7

本文详细介绍了如何使用Python实现AES加密算法,包括PKCS5和Zero填充方式,并提供了实际的加密解密示例。重点展示了如何使用AES-256-CBC模式进行加密和解密操作,适合初学者理解基础加密技术。
摘要由CSDN通过智能技术生成
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))
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗先生的PHP记录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值