• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

openssl:AESCBCPKCS5加解密(C/GOLANG)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
#include <openssl/aes.h>
/* AES_CBC_PKCS5_Encrypt
* 入参:
* src:明文
* srcLen:明文长度
* key:密钥 长度只能是16/24/32字节 否则OPENSSL会对key进行截取或PKCS0填充
* keyLen:密钥长度
* outLen:密文长度
* 返回值:
* 密文 需要free
*/
unsigned char *AES_CBC_PKCS5_Encrypt(unsigned char *src, int srcLen, unsigned char *key, int keyLen, int *outLen)
{
int blockCount = 0;
int quotient = srcLen / AES_BLOCK_SIZE;
int mod = srcLen % AES_BLOCK_SIZE;
blockCount = quotient + 1;

int padding = AES_BLOCK_SIZE - mod;
char *in = (char *)malloc(AES_BLOCK_SIZE*blockCount);
memset(in, padding, AES_BLOCK_SIZE*blockCount);
memcpy(in, src, srcLen);

//out
char *out = (char *)malloc(AES_BLOCK_SIZE*blockCount);
memset(out, 0x00, AES_BLOCK_SIZE*blockCount);
*outLen = AES_BLOCK_SIZE*blockCount;

//初始向量为全0
unsigned char iv[AES_BLOCK_SIZE];
memset(iv, 0x00, AES_BLOCK_SIZE);

//开始加密
AES_KEY aes;
if (AES_set_encrypt_key((unsigned char*)key, keyLen*8, &aes) < 0)
{
printf("AES_set_encrypt_key error\n");
return NULL;
}
AES_cbc_encrypt((unsigned char*)in, (unsigned char*)out, AES_BLOCK_SIZE*blockCount, &aes, iv, AES_ENCRYPT);
free(in);

return (unsigned char*)out;
}

/* AES_CBC_PKCS5_Decrypt
* 入参:
* src:密文
* srcLen:密文长度
* key:密钥 长度只能是16/24/32字节 否则OPENSSL会对key进行截取或PKCS0填充
* keyLen:密钥长度
* outLen:明文长度
* 返回值:
* 明文 需要free
*/
unsigned char *AES_CBC_PKCS5_Decrypt(unsigned char *src, int srcLen, unsigned char *key, int keyLen, int *outLen)
{
//初始向量为全0
unsigned char iv[AES_BLOCK_SIZE];
memset(iv, 0x00, AES_BLOCK_SIZE);

//开始加密
AES_KEY aes;
if (AES_set_decrypt_key((unsigned char*)key, keyLen * 8, &aes) < 0)
{
return NULL;
}
char *tmp = (char *)malloc(srcLen);
memset(tmp, 0x00, srcLen);
AES_cbc_encrypt((unsigned char*)src, (unsigned char*)tmp, srcLen, &aes, iv, AES_DECRYPT);

//PKCS5 UNPADDING
int unpadding = tmp[srcLen - 1];
*outLen = srcLen - unpadding;
char *out = (char *)malloc(*outLen);
memcpy(out, tmp, *outLen);

free(tmp);
return (unsigned char*)out;
}
package crypto

import (
"crypto/aes"
"crypto/cipher"
"errors"
)

//AESDecrypt AES加密 初始向量16字节空 PKCS5 CBC
//入参:src 待加密[]byte
// key:密钥[]byte 16/24/32
// 返回:加密后[]byte
func AESEncrypt(src, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
src = pkcs5Padding(src, block.BlockSize())
srcLen := len(src)
if srcLen%block.BlockSize() != 0 {
return nil, errors.New("Need a multiple of the blocksize")
}
dst := make([]byte, srcLen)
iv := make([]byte, 16)
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(dst, src)
return dst, nil
}

//AESEncrypt AES解密 初始向量16字节空 PKCS5 CBC
//入参:src 已加密[]byte
// key:密钥[]byte 16/24/32
// 返回:解密后[]byte
func AESDecrypt(src, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
srcLen := len(src)
if srcLen%block.BlockSize() != 0 {
return nil, errors.New("crypto/cipher: input not full blocks")
}
dst := make([]byte, srcLen)
iv := make([]byte, 16)
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(dst, src)
return pkcs5UnPadding(dst), 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 pkcs5UnPadding(origData []byte) []byte {
length := len(origData)
// 去掉最后一个字节 unpadding 次
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Cg(CforGraphic)语言表达式与控制语句(转)发布时间:2022-07-13
下一篇:
C++读入神器——文操快读(oj也可以用)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap