openssl学习篇之base64编码、解码;md5 摘要;sha1摘要;3des加密,解密;rsa算法

来源:互联网 发布:淘宝模版350 编辑:程序博客网 时间:2024/06/01 09:20

openssl学习篇之base64编码、解码 完善在上一篇 “openssl学习篇之base64编码、解码中有写出实现的代码,

经实际的运行中,发现如果字符串长超过47会产生BUG,返回空,所以修正了算法,并给出代码示范:

代码包下载


/******************************************************OpensslWrapper.h****************************************************************/

////////////////////////////////////////////////////////////////////////////////
//
// 公共定义
//
////////////////////////////////////////////////////////////////////////////////


#define IN
#define OUT


#define SUCCESS 0
#define INPUT_ERROR -1
#define INPUT_OVERFLOW -10
#define OUTPUT_BUFFER_NOT_READY -20
#define BASE64_ENCODE_ERROR -30
#define BASE64_DECODE_ERROR -40


#define BUFFER_SIZE 2048


////////////////////////////////////////////////////////////////////////////////
//
// DES定义
//
////////////////////////////////////////////////////////////////////////////////
#define LEN_OF_DES_KEY 24 


////////////////////////////////////////////////////////////////////////////////
//
// RSA定义
//
////////////////////////////////////////////////////////////////////////////////
#define MAX_RSA_KEY_LENGTH 1024
#define ERROR_OF_GENRSAKEY -100
#define ERROR_OF_WRITE_PBK -101
#define ERROR_OF_WRITE_PVK -102
#define ERROR_OF_READ_PBK -201
#define ERROR_OF_READ_PVK -202
#define ERROR_OF_ENC_DATA -300
#define ERROR_OF_DEC_DATA -400


////////////////////////////////////////////////////////////////////////////////
//
//  base64编码
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char * base64encode(
    IN const unsigned char *in,//输入字串
    IN int inl,//输入字串的长度
    OUT int *outl //输出字符串的长度
    );


////////////////////////////////////////////////////////////////////////////////
//
//  base64解码
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char * base64decode(
    IN const unsigned char *in,
    IN int inl,
    OUT int *outl
    );


////////////////////////////////////////////////////////////////////////////////
//
//  MD5
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char * md5(
    IN const unsigned char *in,//输入字串
    IN int inl,//输入字串的长度
    OUT int *outl //输出字符串的长度
    );
////////////////////////////////////////////////////////////////////////////////
//
//  SHA1
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char * sha1(
    IN const unsigned char *in,
    IN int inl,
    OUT int *outl
    );
////////////////////////////////////////////////////////////////////////////////
//
//  des3加密
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char * des3encrypt(
    IN const unsigned char *in,
    IN int inl,
    IN char *key,//原始密钥,只能使用字母和数字下划线
    IN OUT int *outl//输出字符串的长度
    );
////////////////////////////////////////////////////////////////////////////////
//
//  des3解密
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char * des3decrypt(
    IN const unsigned char *in,
    IN int inl,
    IN char *key,
    IN OUT int *outl
    );


////////////////////////////////////////////////////////////////////////////////
//
//  rsa生成公私钥对并保存在文件中
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) int RSAGenerateKey(
    IN  char *privateKey,//私钥文件的全路径
    IN  char *publicKey//公钥文件的全路径
    );


////////////////////////////////////////////////////////////////////////////////
//
//  rsa读取私钥字串
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char * getRSAPrivateKey(
    IN char *privateKey//私钥文件的全路径
    );


////////////////////////////////////////////////////////////////////////////////
//
//  rsa读取公钥字串
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char * getRSAPublicKey(
    IN char *publicKey
    );
////////////////////////////////////////////////////////////////////////////////
//
//  rsa私钥完成加密
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char *RSAEncryptByPrivateKey(
    IN const char *privateKey,//完整路径
    IN const unsigned char *in,//输入的字符串
    IN int inl,//输入字符串的长度
    OUT int * outl
    );
////////////////////////////////////////////////////////////////////////////////
//
//  rsa私钥完成解密
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char *RSADecryptByPrivateKey(
    IN const char *privateKey,//完整路径
    IN const unsigned char *in,//输入的字符串
    IN int inl,//输入字符串的长度
    OUT int * outl
    );
////////////////////////////////////////////////////////////////////////////////
//
//  rsa公钥完成加密
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char *RSAEncryptByPublicKey(
    IN const char *publicKey,//公钥的完整路径
    IN const unsigned char *in,
    IN int inl,//输入字符串的长度
    OUT int *outl
    );
////////////////////////////////////////////////////////////////////////////////
//
//  rsa公钥完成解密
//
////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) unsigned char *RSADecryptByPublicKey(
    IN const char *publicKey,
    IN const unsigned char *in,
    IN int inl,
    OUT int * outl
    );



/*************************************************OpensslWrapper.cpp********************************************************************/

// OpensslWrapper.cpp : 定义 DLL 应用程序的导出函数。
//


#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/des.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include "OpensslWrapper.h"


////////////////////////////////////////////////////////////////////////////////
//
// base64编码
//
////////////////////////////////////////////////////////////////////////////////
unsigned char * base64encode(
                             IN const unsigned char *in,
                             IN int inl,
                             OUT int *outl
                             )
{
    *outl=0;
    int total=0;
    if(in==NULL)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    if(inl<=0)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    EVP_ENCODE_CTX ctx;
    int __base64_len= (((inl+2)/3)*4)+1;//Base 64 text length
    int __pem_len=__base64_len+__base64_len/64;// PEM adds a newline every 64 bytes
    unsigned char* base64=new unsigned char[__pem_len];
    EVP_EncodeInit(&ctx);
    EVP_EncodeUpdate(&ctx,base64,outl,in ,inl);
    total+=*outl;
    EVP_EncodeFinal(&ctx,base64+total,outl);
    total+=*outl;
    *outl=total;
    return base64;
}




////////////////////////////////////////////////////////////////////////////////
//
// base64解码
//
////////////////////////////////////////////////////////////////////////////////
unsigned char * base64decode(
                             IN const unsigned char *in,
                             IN int inl,
                             OUT int *outl
                             )
{
    *outl=0;
    int total=0;
    if(in==NULL)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    if(inl<=0)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    EVP_ENCODE_CTX ctx;
    int __org_len = (((inl+2)/4)*3) + 1;
    unsigned char* __org_buffer=new unsigned char[__org_len];
    int __ret,__tmp_len;
    EVP_DecodeInit(&ctx);
    __ret=EVP_DecodeUpdate(&ctx,__org_buffer,outl,in,inl);
    if(__ret<0)
    {
        *outl=BASE64_DECODE_ERROR;
        return NULL ;
    }
    total+=*outl;
    EVP_DecodeFinal(&ctx,__org_buffer,&__tmp_len);
    total+=__tmp_len;
    *outl=total;
    return __org_buffer;
}
////////////////////////////////////////////////////////////////////////////////
//
// MD5摘要
//
////////////////////////////////////////////////////////////////////////////////
unsigned char * md5(
                    IN const unsigned char *in,
                    IN int inl,
                    OUT int *outl
                    )
{
    *outl=0;
    if(in==NULL)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    if(inl<=0)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    const int DIGESTLENGTH=MD5_DIGEST_LENGTH;
    const int OUTLEN=33;
    const int TMPLEN=3;
    unsigned char *md=new unsigned char[DIGESTLENGTH];
    unsigned char *out=new unsigned char[OUTLEN];
    char *buf=new char[OUTLEN];
    char *tmp=new char[TMPLEN];
    int i;
    memset(buf, 0, OUTLEN);
    memset(tmp, 0, TMPLEN);
    MD5_CTX ctx;
    MD5_Init(&ctx);
    MD5_Update(&ctx,in,inl);
    MD5_Final(md,&ctx);
    for( i=0; i<DIGESTLENGTH; i++ ){
        sprintf(tmp,"%02X",md[i]);
        //sprintf_s(tmp,sizeof(tmp),"%02X",md[i]);//非常奇怪,换成安全函数在debug配置下编译,运行时就会引发HEAP CORRUPTION
        strcat(buf,tmp);
    }
    memset(out, 0, OUTLEN);
    memcpy(out,buf,OUTLEN);
    if(buf!=NULL)
    {
        delete [] buf;
        buf=NULL;
    }
    if(tmp!=NULL)
    {
        delete [] tmp;
        tmp=NULL;
    }
    if(md!=NULL)
    {
        delete [] md;
        md=NULL;
    }
    *outl=OUTLEN;
    return out;
}


////////////////////////////////////////////////////////////////////////////////
//
// SHA1摘要
//
////////////////////////////////////////////////////////////////////////////////
unsigned char * sha1(
                     IN const unsigned char *in,
                     IN int inl,
                     OUT int *outl
                     )
{
    *outl=0;
    if(in==NULL)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    if(inl<=0)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    SHA_CTX s;
    unsigned char *md=new unsigned char[SHA_DIGEST_LENGTH];
    SHA1_Init(&s);
    SHA1_Update(&s,in,inl);
    SHA1_Final(md,&s);
    //memset(out, 0, SHA_DIGEST_LENGTH);
    //memcpy(out,md,SHA_DIGEST_LENGTH);
    *outl=SHA_DIGEST_LENGTH;
    return md;
}






////////////////////////////////////////////////////////////////////////////////
//
// Des3加密
//
////////////////////////////////////////////////////////////////////////////////
unsigned char * des3encrypt(
                            IN const unsigned char *in,
                            IN int inl,
                            IN char *key,
                            OUT int *outl
                            )
{
    *outl=0;
    if(in==NULL)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    if(inl<=0)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }


    unsigned char ch; 
    unsigned char *out=NULL;
    unsigned char *src_cmpt = NULL; /* 补齐后的明文 */ 
    unsigned char *key_cmpt=new unsigned char[LEN_OF_DES_KEY];
    /* 构造补齐后的密钥 */ 
    int key_len;
    key_len =strlen(key); 
    memcpy(key_cmpt, key, key_len); 
    memset(key_cmpt + key_len, 0x00, LEN_OF_DES_KEY - key_len); 
    /* 分析补齐明文所需空间及补齐填充数据 */ 
    int data_rest=inl % 8; 
    int len = inl + (8 - data_rest); 
    ch = 8 - data_rest; 
    src_cmpt = (unsigned char *)malloc(len); 
    if (NULL == src_cmpt) 
    {
        *outl= OUTPUT_BUFFER_NOT_READY;
        return NULL;
    }
    /* 构造补齐后的加密内容 */ 
    memset(src_cmpt, 0, len); 
    memcpy(src_cmpt, in, inl); 
    memset(src_cmpt + inl, ch, 8 - data_rest); 
    /* 密钥置换 */ 
    unsigned char *block_key=new unsigned char[9]; 
    DES_key_schedule ks,ks2,ks3;
    memset(block_key, 0, sizeof(block_key)); 
    memcpy(block_key, key_cmpt + 0, 8); 
    DES_set_key_unchecked((const_DES_cblock*)block_key, &ks); 
    memcpy(block_key, key_cmpt + 8, 8); 
    DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2); 
    memcpy(block_key, key_cmpt+ 16, 8); 
    DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3); 
    /* 循环加密/解密,每8字节一次 */ 
    int count; 
    int i; 
    unsigned char *tmpin=new unsigned char[8];
    unsigned char *tmpout=new unsigned char[8];
    out=(unsigned char*)malloc(len);
    memset(out,0,len);
    count = len/8;
    for (i = 0; i < count; i++) 
    {
        memset(tmpin,0,8);
        memset(tmpout, 0, 8); 
        memcpy(tmpin, src_cmpt + 8 * i, 8); 
        /* 加密 */ 
        DES_ecb3_encrypt((const_DES_cblock*)tmpin, (DES_cblock*)tmpout, &ks, &ks2, &ks3, DES_ENCRYPT);
        /* copy */ 
        memcpy(out + 8 * i, tmpout, 8); 


    }
    /*release resource*/
    if(NULL!=ch)
    {
        ch=NULL;
    }
    if(src_cmpt!=NULL)
    {
        delete [] src_cmpt;
        src_cmpt=NULL;
    }
    if(key_cmpt!=NULL)
    {
        delete [] key_cmpt;
        key_cmpt=NULL;
    }
    if(block_key!=NULL)
    {
        delete [] block_key;
        block_key=NULL;
    }
    if(tmpin!=NULL)
    {
        delete [] tmpin;
        tmpin=NULL;
    }
    if(tmpout!=NULL)
    {
        delete [] tmpout;
        tmpout=NULL;
    }
    *outl=len;
    return out;
}


////////////////////////////////////////////////////////////////////////////////
//
// Des3解密
//
////////////////////////////////////////////////////////////////////////////////
unsigned char * des3decrypt(
                            IN const unsigned char *in,
                            IN int inl,
                            IN char *key,
                            OUT int *outl
                            )
{
    *outl=0;
    if(in==NULL)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    if(inl<=0)
    {
        *outl=INPUT_ERROR;
        return NULL;
    }
    unsigned char *out=(unsigned char *)malloc(inl);
    /* 构造补齐后的密钥 */ 
    unsigned char *key_cmpt=new unsigned char[LEN_OF_DES_KEY];
    int key_len;
    key_len =strlen(key); 
    memcpy(key_cmpt, key, key_len); 
    memset(key_cmpt + key_len, 0x00, LEN_OF_DES_KEY - key_len); 
    /* 密钥置换 */ 
    unsigned char *block_key=new unsigned char[9]; 
    DES_key_schedule ks,ks2,ks3;
    memset(block_key, 0, sizeof(block_key)); 
    memcpy(block_key, key_cmpt + 0, 8); 
    DES_set_key_unchecked((const_DES_cblock*)block_key, &ks); 
    memcpy(block_key, key_cmpt + 8, 8); 
    DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2); 
    memcpy(block_key, key_cmpt+ 16, 8); 
    DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3); 
    /* 循环加密/解密,每8字节一次 */ 
    int count; 
    int i; 
    unsigned char *tmpin=new unsigned char[8];
    unsigned char *tmpout=new unsigned char[8];
    memset(out,0,inl);
    count =inl/8;
    for (i = 0; i < count; i++) 
    {
        memset(tmpin,0,8);
        memset(tmpout, 0, 8); 
        memcpy(tmpin, in + 8 * i, 8); 
        /* 解密 */ 
        //DES_ecb3_encrypt((const_DES_cblock*)tmpin, (DES_cblock*)tmpout, &ks, &ks2, &ks3, DES_ENCRYPT);
        DES_ecb3_encrypt((const_DES_cblock*)tmpin, (DES_cblock*)tmpout, &ks, &ks2, &ks3, DES_DECRYPT);
        /* copy */ 
        memcpy(out + 8 * i, tmpout, 8); 


    }
    /*release resource*/
    if(key_cmpt!=NULL)
    {
        delete [] key_cmpt;
        key_cmpt=NULL;
    }
    if(block_key!=NULL)
    {
        delete [] block_key;
        block_key=NULL;
    }
    if(tmpin!=NULL)
    {
        delete [] tmpin;
        tmpin=NULL;
    }
    if(tmpout!=NULL)
    {
        delete [] tmpout;
        tmpout=NULL;
    }
    *outl=inl;
    return out;
}


//////////////////////////////////////////////////////////////////////////
// 产生RSA公钥和密钥,并保存在文件中
// 密钥的长度为 1024
//////////////////////////////////////////////////////////////////////////
int RSAGenerateKey(
                   char *privateKey,//私钥的完整路径
                   char *publicKey //公钥的完整路径
                   )
{
    if(NULL==privateKey)
        return INPUT_ERROR;
    if(NULL==publicKey)
        return INPUT_ERROR;
    RSA *rsa=NULL;
    unsigned char protectCode[]="20121121";
    rsa = RSA_generate_key(MAX_RSA_KEY_LENGTH,RSA_F4,NULL,NULL);
    if(NULL==rsa)
        return ERROR_OF_GENRSAKEY;
    // 公钥
    BIO *bp = BIO_new(BIO_s_file());
    if (0 >= BIO_write_filename(bp,publicKey))
    {
        return ERROR_OF_WRITE_PBK ;
    }
    if (1 != PEM_write_bio_RSAPublicKey(bp, rsa))
    {
        return ERROR_OF_WRITE_PBK;
    }  
    BIO_free_all(bp);


    // 私钥
    bp = BIO_new(BIO_s_file());
    if ( 0 >= BIO_write_filename(bp, privateKey ))
    {
        return ERROR_OF_WRITE_PVK;
    }
    //if ( 1 != PEM_write_bio_RSAPrivateKey(bp, rsa, NULL/*EVP_des_ede3()*/, NULL/*(unsigned char*)passwd*/, 0/*5*/, NULL, NULL))
    if ( 1 != PEM_write_bio_RSAPrivateKey(bp, rsa, EVP_des_ede3(), protectCode,8, NULL, NULL))
    {
        return ERROR_OF_WRITE_PVK;
    }
    BIO_free_all(bp);
    RSA_free(rsa);
    return SUCCESS;
}






////////////////////////////////////////////////////////////////////////////////
//
//  rsa读取私钥字串
//
////////////////////////////////////////////////////////////////////////////////
unsigned char * getRSAPrivateKey(
                                 IN char *privateKey//私钥文件的全路径
                                 )
{
    return NULL;
}


////////////////////////////////////////////////////////////////////////////////
//
//  rsa读取公钥字串
//
////////////////////////////////////////////////////////////////////////////////
unsigned char * getRSAPublicKey(
                                IN char *publicKey
                                )
{
    return NULL;
}


////////////////////////////////////////////////////////////////////////////////
//
//  rsa私钥完成加密
//
////////////////////////////////////////////////////////////////////////////////
unsigned char *RSAEncryptByPrivateKey(
                                      IN const char *privateKey,//完整路径
                                      IN const unsigned char *in,//输入的字符串
                                      IN int inl,
                                      OUT int * outl
                                      )
{
    *outl=0;
    if(NULL==privateKey)
    {
        *outl=ERROR_OF_READ_PVK;
        return NULL;
    }
    if(NULL==in)
    {
        *outl=ERROR_OF_ENC_DATA;
        return NULL;
    }
    if(0>inl)
    {
        *outl=ERROR_OF_ENC_DATA;
        return NULL;
    }
    if(117<inl)//要加密的字符串长度不得大于117
    {
        *outl=INPUT_OVERFLOW;
        return NULL;
    }
    BIO *pBio = NULL;
    RSA *pRsa=RSA_new();
    OpenSSL_add_all_algorithms();
    pBio = BIO_new_file(privateKey, "r");
    if (NULL == pBio)
    {
        *outl=ERROR_OF_READ_PVK;
        return NULL;
    }
    char protectCode[]="20121121";


    pRsa = PEM_read_bio_RSAPrivateKey(pBio, &pRsa, NULL,protectCode);
    BIO_free_all(pBio);
    if (NULL == pRsa)
    {
        *outl=ERROR_OF_READ_PVK;
        return NULL;
    }


    unsigned char *out=(unsigned char *)malloc(MAX_RSA_KEY_LENGTH);
    memset(out,0,MAX_RSA_KEY_LENGTH);
    *outl=RSA_private_encrypt(RSA_size(pRsa)-11,
        in,//reinterpret_cast<unsigned char*>(pFrom),
        out,
        pRsa,
        RSA_PKCS1_PADDING);
    RSA_free(pRsa);
    CRYPTO_cleanup_all_ex_data();
    return out;
}




////////////////////////////////////////////////////////////////////////////////
//
//  rsa私钥完成解密
//
////////////////////////////////////////////////////////////////////////////////
unsigned char *RSADecryptByPrivateKey(
                                      IN const char *privateKey,//完整路径
                                      IN const unsigned char *in,//输入的字符串
                                      IN int inl,//输入字符串的长度
                                      OUT int * outl
                                      )
{
    *outl=0;
    if(NULL==privateKey)
    {
        *outl=ERROR_OF_READ_PVK;
        return NULL;
    }
    if(NULL==in)
    {
        *outl=ERROR_OF_ENC_DATA;
        return NULL;
    }
    if(0>inl)
    {
        *outl=ERROR_OF_ENC_DATA;
        return NULL;
    }
    if(MAX_RSA_KEY_LENGTH<inl)//要解密的字符串长度不得
    {
        *outl=INPUT_OVERFLOW;
        return NULL;
    }
    BIO *pBio = NULL;
    RSA *pRsa=RSA_new();
    OpenSSL_add_all_algorithms();
    pBio = BIO_new_file(privateKey, "r");
    if (NULL == pBio)
    {
        *outl=ERROR_OF_READ_PVK;
        return NULL;
    }
    char protectCode[]="20121121";


    pRsa = PEM_read_bio_RSAPrivateKey(pBio, &pRsa, NULL,protectCode);
    BIO_free_all(pBio);
    if (NULL == pRsa)
    {
        *outl=ERROR_OF_READ_PVK;
        return NULL;
    }


    unsigned char *out=(unsigned char *)malloc(MAX_RSA_KEY_LENGTH);
    memset(out,0,MAX_RSA_KEY_LENGTH);
    *outl=RSA_private_decrypt(
        inl,
        in,//reinterpret_cast<unsigned char*>(pFrom),
        out,
        pRsa,
        RSA_PKCS1_PADDING);
    RSA_free(pRsa);
    CRYPTO_cleanup_all_ex_data();
    return out;
}
////////////////////////////////////////////////////////////////////////////////
//
//  rsa公钥完成加密
//
////////////////////////////////////////////////////////////////////////////////
unsigned char *RSAEncryptByPublicKey(
                                     IN const char *publicKey,//公钥的完整路径
                                     IN const unsigned char *in,
                                     IN int inl,//输入字符串的长度
                                     OUT int *outl
                                     )
{
    *outl=0;
    if(NULL==publicKey)
    {
        *outl=ERROR_OF_READ_PBK;
        return NULL;
    }
    if(NULL==in)
    {
        *outl=ERROR_OF_DEC_DATA;
        return NULL;
    }
    if(0>inl)
    {
        *outl=ERROR_OF_DEC_DATA;
        return NULL;
    }
    if(117<inl)//要解密的字符串长度不得大于117
    {
        *outl=INPUT_OVERFLOW;
        return NULL;
    }
    BIO *pBio = NULL;
    RSA *pRsa=RSA_new();
    OpenSSL_add_all_algorithms();
    pBio = BIO_new_file(publicKey, "r");
    if (NULL == pBio)
    {
        *outl=ERROR_OF_READ_PBK;
        return NULL;
    }
    pRsa = PEM_read_bio_RSAPublicKey(pBio,&pRsa,NULL,NULL);
    BIO_free_all(pBio);
    if (NULL == pRsa)
    {
        *outl=ERROR_OF_READ_PBK;
        return NULL;
    }
    unsigned char *out=(unsigned char *)malloc(sizeof(unsigned char)*MAX_RSA_KEY_LENGTH);
    memset(out,0,MAX_RSA_KEY_LENGTH);


    *outl=RSA_public_encrypt(
        RSA_size(pRsa)-11,
        in,
        out,
        pRsa,
        RSA_PKCS1_PADDING );
    RSA_free(pRsa);
    CRYPTO_cleanup_all_ex_data();
    return out;
}


////////////////////////////////////////////////////////////////////////////////
//
//  rsa公钥完成解密
//
////////////////////////////////////////////////////////////////////////////////
unsigned char *RSADecryptByPublicKey(
                                     IN const char *publicKey,//完整路径
                                     IN const unsigned char *in,//输入的字符串
                                     IN int inl,
                                     OUT int * outl
                                     )
{
    *outl=0;
    if(NULL==publicKey)
    {
        *outl=ERROR_OF_READ_PBK;
        return NULL;
    }
    if(NULL==in)
    {
        *outl=ERROR_OF_ENC_DATA ;
        return NULL;
    }
    if(MAX_RSA_KEY_LENGTH<inl)
    {
        *outl=INPUT_OVERFLOW;
        return NULL;
    }


    BIO *pBio = NULL;
    RSA *pRsa=RSA_new();


    pBio = BIO_new_file(publicKey, "r");
    if (NULL == pBio)
    {
        *outl=ERROR_OF_READ_PBK;
        return NULL;
    }


    pRsa = PEM_read_bio_RSAPublicKey(pBio,&pRsa,NULL,NULL);
    BIO_free_all(pBio);
    if (NULL == pRsa)
    {
        *outl=ERROR_OF_READ_PBK;
        return NULL;
    }
    unsigned char *out=(unsigned char *)malloc(sizeof(unsigned char)*MAX_RSA_KEY_LENGTH);
    memset(out,0,MAX_RSA_KEY_LENGTH);


    *outl=RSA_public_decrypt(
        inl,
        in,
        out,
        pRsa,
        RSA_PKCS1_PADDING );
    RSA_free(pRsa);
    CRYPTO_cleanup_all_ex_data();
    return out;
}

0 0
原创粉丝点击