golang版aes-cbc-pkcs7加密解密base64&hex字符串输入输出

最近项目中golang项目中使用aes加解密,做个记录方便以后使用

aes-cbc-pkcs7加密解密base64输入输出

type AesBase64 struct {
	key []byte // 允许16,24,32字节长度
	iv  []byte // 只允许16字节长度
}

func NewAesBase64(key []byte, iv []byte) *AesBase64 {
	return &AesBase64{
		iv:  iv,
		key: key,
	}
}

func (s *AesBase64) Encrypt(text []byte) (string, error) {
	if len(text) == 0 {
		return "", nil
	}
	//生成cipher.Block 数据块
	block, err := aes.NewCipher(s.key)
	if err != nil {
		return "", err
	}
	//填充内容,如果不足16位字符
	blockSize := block.BlockSize()
	originData := s.pad(text, blockSize)
	//加密方式
	blockMode := cipher.NewCBCEncrypter(block, s.iv)
	//加密,输出到[]byte数组
	crypted := make([]byte, len(originData))
	blockMode.CryptBlocks(crypted, originData)
	return base64.StdEncoding.EncodeToString(crypted), nil
}

func (s *AesBase64) Decrypt(text string) ([]byte, error) {
	if len(text) == 0 {
		return []byte(text), nil
	}
	decodeData, err := base64.StdEncoding.DecodeString(text)
	if err != nil {
		return []byte(text), err
	}
	if len(decodeData) == 0 {
		return []byte(text), nil
	}
	//生成密码数据块cipher.Block
	block, _ := aes.NewCipher(s.key)
	//解密模式
	blockMode := cipher.NewCBCDecrypter(block, s.iv)
	//输出到[]byte数组
	originData := make([]byte, len(decodeData))
	blockMode.CryptBlocks(originData, decodeData)
	//去除填充,并返回
	return s.unPad(originData), nil
}

func (s *AesBase64) pad(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize
	padText := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(ciphertext, padText...)
}

func (s *AesBase64) unPad(ciphertext []byte) []byte {
	length := len(ciphertext)
	// 去掉最后一次的padding
	unPadding := int(ciphertext[length-1])
	return ciphertext[:(length - unPadding)]
}

aes-cbc-pkcs7加密解密hex字符串输入输出

type AesHex struct {
	key []byte // 允许16,24,32字节长度
	iv  []byte // 只允许16字节长度
}

func NewAesHex(key []byte, iv []byte) *AesHex {
	return &AesHex{
		iv:  iv,
		key: key,
	}
}

func (s *AesHex) Encrypt(text []byte) (string, error) {
	if len(text) == 0 {
		return "", nil
	}
	//生成cipher.Block 数据块
	block, err := aes.NewCipher(s.key)
	if err != nil {
		return "", err
	}
	//填充内容,如果不足16位字符
	blockSize := block.BlockSize()
	originData := s.pad(text, blockSize)
	//加密方式
	blockMode := cipher.NewCBCEncrypter(block, s.iv)
	//加密,输出到[]byte数组
	crypted := make([]byte, len(originData))
	blockMode.CryptBlocks(crypted, originData)
	return hex.EncodeToString(crypted), nil
}

func (s *AesHex) Decrypt(text string) ([]byte, error) {
	if len(text) == 0 {
		return []byte(text), nil
	}
	decodeData, err := hex.DecodeString(text)
	if err != nil {
		return []byte(text), err
	}
	if len(decodeData) == 0 {
		return []byte(text), nil
	}
	//生成密码数据块cipher.Block
	block, _ := aes.NewCipher(s.key)
	//解密模式
	blockMode := cipher.NewCBCDecrypter(block, s.iv)
	//输出到[]byte数组
	originData := make([]byte, len(decodeData))
	blockMode.CryptBlocks(originData, decodeData)
	//去除填充,并返回
	return s.unPad(originData), nil
}

func (s *AesHex) pad(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize
	padText := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(ciphertext, padText...)
}

func (s *AesHex) unPad(ciphertext []byte) []byte {
	length := len(ciphertext)
	// 去掉最后一次的padding
	unPadding := int(ciphertext[length-1])
	return ciphertext[:(length - unPadding)]
}

原文仓库地址:https://github.com/yanue/aes-cbc-pkcs7

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值