#include <openssl/evp.h>
#include <openssl/ossl_typ.h>
#include <openssl/md5.h>
#include <string.h>
int Enclicense(unsigned char* in, int inlen, unsigned char* _out)
{
int ret = 0;
unsigned char out[1024] = { 0 };
int outlen = 1024;
int _updatelen;
int tmplen;
EVP_CIPHER_CTX *evp_cipher_ctx = NULL;
evp_cipher_ctx = EVP_CIPHER_CTX_new();
if (!evp_cipher_ctx) {
ret = -1;
return ret;
}
ret = EVP_CIPHER_CTX_set_padding(evp_cipher_ctx, 1);
//密钥
unsigned char _key[]= "86C63180C2806ED1";
ret = EVP_CipherInit(evp_cipher_ctx, EVP_sm4_ecb(), _key, NULL, 1);
if (1 != ret)
return ret;
_updatelen = outlen;
//数据
ret = EVP_CipherUpdate(evp_cipher_ctx, out, &_updatelen, in, inlen);
if (1 != ret)
return 0;
tmplen = outlen - _updatelen;
//获取加密结果
ret = EVP_CipherFinal(evp_cipher_ctx, &out[_updatelen], &tmplen);
outlen = _updatelen + tmplen;
//转换成字符串
CCodeChange::GetInst().HexToStr(out, outlen, (char*)_out);
//md5加密
/*
unsigned char tmpoutdata[128] = {0};
CCodeChange::GetInst().HexToStr(tmpoutdata, outlen, (char*)_out);
MD5_CTX ctx;
unsigned char outmd[16];
int i=0;
memset(outmd,0,sizeof(outmd));
//初始化
MD5_Init(&ctx);
//数据
MD5_Update(&ctx,tmpoutdata,outlen);
//获取加密结果
MD5_Final(outmd,&ctx);
char data[33] = {0};
memset(data,0,33);
for(i=0;i<16;i++)
{
sprintf((char*)out+2*i,"%02X",outmd[i]);
}
*/
return ret;
}
使用openssl进行sm4加密(附带MD5)
最新推荐文章于 2024-08-07 16:26:28 发布