使用openssl进行sm4加密(附带MD5)

#include 
#include 
#include 
#include 

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;
}

你可能感兴趣的:(算法,加密解密)