資源簡(jiǎn)介
使用Openssl實(shí)現(xiàn)RSA的加密和解密過(guò)程;使用Openssl實(shí)現(xiàn)簽名和驗(yàn)簽過(guò)程;SHA256WithRSA簽名驗(yàn)簽過(guò)程;封裝很好,一看就懂,直接使用!

代碼片段和文件信息
#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);
}
//***通過(guò)公鑰加密數(shù)據(jù)***
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;
}
//***通過(guò)私鑰解密數(shù)據(jù)***
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;
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
????..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\appli
?????文件??????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個(gè)文件信息
評(píng)論
共有 條評(píng)論