C++使用Openssl进行RSA加密解密及签名验签功能SHA256


使用Openssl实现RSA的加密和解密过程;使用Openssl实现签名和验签过程;SHA256WithRSA签名验签过程;封装很好,一看就懂,直接使用!
资源截图
代码片段和文件信息
#include “stdafx.h“
#include “RSAEncode.h“

//***生成公钥和私钥文件***
void GenerateKey()
{
/* 生成公钥 */
RSA* rsa = RSA_generate_key(1024 RSA_F4 NULL NULL);
BIO *bp = BIO_new(BIO_s_file());
BIO_write_filename(bp PUBLIC_KEY_FILE);
PEM_write_bio_RSAPublicKey(bp rsa);
BIO_free_all(bp);

/* 生成私钥 */
bp = BIO_new(BIO_s_file());
BIO_write_filename(bp PRIVATE_KEY_FILE);
PEM_write_bio_RSAPrivateKey(bp rsa NULL NULL 0 NULL NULL);
CRYPTO_cleanup_all_ex_data();
BIO_free_all(bp);
RSA_free(rsa);
}

//***通过公钥加密数据***
string EncodeByBioPublicKey(string data)
{
string strRet;

OpenSSL_add_all_algorithms();
BIO* bp = BIO_new(BIO_s_file());
BIO_read_filename(bp PUBLIC_KEY_FILE);
RSA* rsaK = PEM_read_bio_RSAPublicKey(bp NULL NULL NULL);
if (NULL == rsaK)
{
unsigned long ulErr = ERR_get_error();
char szErrMsg[1024] = { 0 };
char *pTmp = NULL;
pTmp = ERR_error_string(ulErr szErrMsg);
printf(“bio_read_publicKey %s
“ szErrMsg);
return strRet;
}

int nLen = RSA_size(rsaK);
char *pEncode = new char[nLen + 1];
int ret = RSA_public_encrypt(data.length() (const unsigned char*)data.c_str()
(unsigned char *)pEncode rsaK RSA_PKCS1_PADDING);
if (ret >= 0)
{
strRet = string(pEncode ret);
}

delete[] pEncode;
CRYPTO_cleanup_all_ex_data();
BIO_free_all(bp);
RSA_free(rsaK);

return strRet;
}

//***通过私钥解密数据***
string DecodeByBioPrivateKey(string data)
{
string strRet;
OpenSSL_add_all_algorithms();
BIO* bp = BIO_new(BIO_s_file());
BIO_read_filename(bp PRIVATE_KEY_FILE);

RSA* rsaK;
rsaK = PEM_read_bio_RSAPrivateKey(bp NULL NULL NULL);
if (NULL == rsaK)
{
unsigned long ulErr = ERR_get_error();
char szErrMsg[1024] = { 0 };
char *pTmp = NULL;
pTmp = ERR_error_string(ulErr szErrMsg);
printf(“%s
“ szErrMsg);
}

int nLen = RSA_size(rsaK);
char *pEncode = new char[nLen + 1];
int nRet = RSA_private_decrypt(data.length() (const unsigned char*)data.c_str() (unsigned char *)pEncode rsaK RSA_PKCS1_PADDING);

if (nRet >= 0)
{
strRet = string(pEncode nRet);
}

delete[] pEncode;
CRYPTO_cleanup_all_ex_data();
BIO_free_all(bp);
RSA_free(rsaK);

return strRet;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     44032  2019-09-19 17:37  RSAWithOpenSSL.vsRSAWithOpenSSLv14.suo

     文件    2104320  2019-09-19 00:08  RSAWithOpenSSLReleaselibcrypto-1_1.dll

     文件     503808  2019-09-19 00:08  RSAWithOpenSSLReleaselibssl-1_1.dll

     文件      15360  2019-09-19 17:25  RSAWithOpenSSLReleaseRSAWithOpenSSL.exe

     文件       3349  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslaes.h

     文件       3508  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslapplink.c

     文件      33627  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslasn1.h

     文件      14599  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslasn1err.h

     文件      32940  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslasn1t.h

     文件        395  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslasn1_mac.h

     文件       2398  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslasync.h

     文件       1326  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslasyncerr.h

     文件      34868  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslio.h

     文件       6400  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslioerr.h

     文件       1847  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopenssllowfish.h

     文件      22135  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopenssln.h

     文件       4907  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslnerr.h

     文件       1600  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopenssluffer.h

     文件        820  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopenssluffererr.h

     文件       3179  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslcamellia.h

     文件       1674  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslcast.h

     文件       1064  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslcmac.h

     文件      16379  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslcms.h

     文件      11160  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslcmserr.h

     文件       1328  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslcomp.h

     文件       1212  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslcomperr.h

     文件       5601  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslconf.h

     文件       3429  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslconferr.h

     文件       1300  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslconf_api.h

     文件      17239  2019-09-19 00:08  RSAWithOpenSSLRSAWithOpenSSLincludeopensslcrypto.h

............此处省略112个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)