openssl private key read from memory sign

Paralysis for a long time, disgusting!

#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "openssl/rsa.h"
#include "openssl/pem.h"
#include "Base64.h"
#ifdef WIN32
#pragma comment(lib,"User32.lib")
#pragma comment(lib,"Advapi32.lib")
#pragma comment(lib,"Gdi32.lib")
#pragma comment(lib,"libeay32.lib")
#pragma comment(lib,"ssleay32.lib")
#endif

// 私钥解密 
std::string rsa_pri_decrypt(const STD :: String & Ciphertext, const STD :: String & prikey) 
{ 
STD :: String strRet; 
the RSA * = RSA RSA_new (); 
BIO * keybio; 
keybio = BIO_new_mem_buf ((unsigned char *) priKey.c_str (), - 1 ); 

// there are three methods herein 
 // 1, memory reads the key pair generated, then the generated memory rsa 
 // 2, the disk reads the key pair generated in a text file, from the memory to generate rsa 
 // 3, generated directly from the reading file pointer RSA 
RSA = PEM_read_bio_RSAPrivateKey (keybio, & RSA, NULL, NULL); 

 int len = RSA_size (RSA); 
char *decryptedText = (char *)malloc(len + 1); 
memset(decryptedText, 0, len + 1); 

// 解密函数 
int ret = RSA_private_decrypt(cipherText.length(), (const unsigned char*)cipherText.c_str(), (unsigned char*)decryptedText, rsa, RSA_PKCS1_PADDING); 
if (ret >= 0) 
strRet = std::string(decryptedText, ret); 

// 释放内存 
free(decryptedText); 
BIO_free_all(keybio); 
RSA_free(rsa); 

return strRet; 
}

 Reference Address: https://www.cnblogs.com/yuandaozhe/p/10114948.html

Guess you like

Origin www.cnblogs.com/wainiwann/p/10986487.html