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

相关推荐

  1. Golang 输入输出

    2023-12-07 06:14:02       27 阅读
  2. Base64解密C语言

    2023-12-07 06:14:02       35 阅读
  3. 解决Base64字符串出现不合法字符的情况

    2023-12-07 06:14:02       66 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-07 06:14:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-07 06:14:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-07 06:14:02       82 阅读
  4. Python语言-面向对象

    2023-12-07 06:14:02       91 阅读

热门阅读

  1. k8s 中externalTrafficPolicy应用场景和实践

    2023-12-07 06:14:02       60 阅读
  2. logback整合rabbitmq实现消息记录日志

    2023-12-07 06:14:02       48 阅读
  3. QT使用http通信的同步和异步

    2023-12-07 06:14:02       61 阅读
  4. C++ IO库

    C++ IO库

    2023-12-07 06:14:02      42 阅读
  5. springboot引入swagger2

    2023-12-07 06:14:02       46 阅读
  6. Spark常见算子汇总

    2023-12-07 06:14:02       65 阅读
  7. DevOps搭建(三)-Docker环境安装细步骤

    2023-12-07 06:14:02       53 阅读
  8. spark log4j日志配置

    2023-12-07 06:14:02       49 阅读
  9. ssh免密远程登录主机并执行命令

    2023-12-07 06:14:02       54 阅读