• 大小: 1.2MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-08-18
  • 语言: C/C++
  • 标签: RSA加密  

资源简介

使用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\n“ 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\n“ 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\.vs\RSAWithOpenSSL\v14\.suo

     文件    2104320  2019-09-19 00:08  RSAWithOpenSSL\Release\libcrypto-1_1.dll

     文件     503808  2019-09-19 00:08  RSAWithOpenSSL\Release\libssl-1_1.dll

     文件      15360  2019-09-19 17:25  RSAWithOpenSSL\Release\RSAWithOpenSSL.exe

     文件       3349  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\aes.h

     文件       3508  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\applink.c

     文件      33627  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\asn1.h

     文件      14599  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\asn1err.h

     文件      32940  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\asn1t.h

     文件        395  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\asn1_mac.h

     文件       2398  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\async.h

     文件       1326  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\asyncerr.h

     文件      34868  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\bio.h

     文件       6400  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\bioerr.h

     文件       1847  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\blowfish.h

     文件      22135  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\bn.h

     文件       4907  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\bnerr.h

     文件       1600  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\buffer.h

     文件        820  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\buffererr.h

     文件       3179  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\camellia.h

     文件       1674  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\cast.h

     文件       1064  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\cmac.h

     文件      16379  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\cms.h

     文件      11160  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\cmserr.h

     文件       1328  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\comp.h

     文件       1212  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\comperr.h

     文件       5601  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\conf.h

     文件       3429  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\conferr.h

     文件       1300  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\conf_api.h

     文件      17239  2019-09-19 00:08  RSAWithOpenSSL\RSAWithOpenSSL\include\openssl\crypto.h

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

评论

共有 条评论