本文整理汇总了C++中PEM_read_RSAPrivateKey函数的典型用法代码示例。如果您正苦于以下问题:C++ PEM_read_RSAPrivateKey函数的具体用法?C++ PEM_read_RSAPrivateKey怎么用?C++ PEM_read_RSAPrivateKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PEM_read_RSAPrivateKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: fopen
//-------------------------------------------------------------------------------------
bool KBE_RSA::loadPrivate(const std::string& keyname)
{
FILE *fp = NULL;
if(rsa_private == NULL)
{
fp = fopen(keyname.c_str(), "rb");
if (!fp) {
return false;
}
rsa_private = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
if(NULL == rsa_private)
{
ERR_load_crypto_strings();
char err[1024];
char* errret = ERR_error_string(ERR_get_error(), err);
ERROR_MSG(fmt::format("KBE_RSA::loadPrivate: PEM_read_RSAPrivateKey error({} : {})\n",
errret, err));
fclose(fp);
return false;
}
}
if(fp)
fclose(fp);
return rsa_private != NULL;
}
示例2: load_ca
/*
* Load CA certificate and private key from current dir
*/
static int load_ca(char * ca_name, identity * ca)
{
FILE * f ;
RSA * rsa ;
char filename[FIELD_SZ+1] ;
sprintf(filename, "%s.crt", ca_name);
if ((f=fopen(filename, "r"))==NULL) {
fprintf(stderr, "Cannot find: %s\n", filename);
return -1 ;
}
ca->cert = PEM_read_X509(f, NULL, NULL, NULL);
fclose(f);
sprintf(filename, "%s.key", ca_name);
if ((f=fopen(filename, "r"))==NULL) {
return -1 ;
}
rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL);
fclose(f);
ca->key = EVP_PKEY_new();
EVP_PKEY_assign_RSA(ca->key, rsa);
if (!X509_check_private_key(ca->cert, ca->key)) {
fprintf(stderr, "CA certificate and private key do not match\n");
return -1 ;
}
return 0;
}
示例3: rsa_decrypt
//使用私钥解密
int rsa_decrypt(char *in, const char *key_path, char* out)
{
RSA *p_rsa;
FILE *file;
int rsa_len;
if ((file=fopen(key_path, "r"))==NULL)
{
perror("open key file error");
return 0;
}
if ((p_rsa=PEM_read_RSAPrivateKey(file, NULL, NULL, NULL))==NULL)
{
ERR_print_errors_fp(stdout);
return 0;
}
rsa_len=RSA_size(p_rsa);
if (RSA_private_decrypt(rsa_len, (unsigned char*)in, (unsigned char*)out, p_rsa, RSA_NO_PADDING)<0)
{
return 0;
}
RSA_free(p_rsa);
fclose(file);
return 1;
}
示例4: svObject
svRSAKey::svRSAKey(const string &pem)
: svObject("svRSAKey"), type(svRSA_TYPE_NULL), key(NULL), mtime(0)
{
struct stat key_stat;
if (stat(pem.c_str(), &key_stat) == -1)
throw svExRSAKeyStat(pem, strerror(errno));
mtime = key_stat.st_mtime;
FILE *h_key = fopen(pem.c_str(), "r");
if (!h_key) throw svExRSAKeyOpen(pem, strerror(errno));
if ((key = PEM_read_RSA_PUBKEY(h_key, NULL, NULL, NULL)))
type = svRSA_TYPE_PUBLIC;
else {
rewind(h_key);
if ((key = PEM_read_RSAPrivateKey(h_key, NULL, NULL, NULL)))
type = svRSA_TYPE_PRIVATE;
else {
ERR_load_crypto_strings();
svError("%s: %s: %s", name.c_str(), pem.c_str(),
ERR_error_string(ERR_get_error(), NULL));
}
}
fclose(h_key);
if (type == svRSA_TYPE_NULL) throw svExRSAKeyInvalid(pem);
name = pem;
}
示例5: fopen
Key::Key(string publicKeyFile, string privateKeyFile)
{
rsa = NULL;
privateKey = NULL;
publicKey = NULL;
priName = privateKeyFile;
pubName = publicKeyFile;
if (!privateKeyFile.empty()) {
FILE *fp = fopen(privateKeyFile.c_str(), "r");
if (fp == NULL) {
cout << "Private Key File Error!" << endl;
return ;
}
privateKey = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
}
if (!publicKeyFile.empty()) {
FILE *fp = fopen(publicKeyFile.c_str(), "r");
if (fp == NULL) {
cout << "Public Key File Error!" << endl;
return ;
}
// rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
publicKey = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL);
fclose(fp);
return ;
}
cout << "Error Open Private Key or Public Key!" << endl;
}
示例6: decrypt
char * decrypt(char * p_ecypt,int len_ecypt,char * priv_key){
char * p_dcypt;
int rsa_len;
// open private key file
FILE* priv_fp=fopen(priv_key,"r");
if(priv_fp==NULL){
printf("failed to open priv_key file %s!\n", priv_key);
return NULL;
}
// read private key from private key file
RSA *rsa2=PEM_read_RSAPrivateKey(priv_fp,NULL,NULL,NULL);
if(rsa2==NULL){
printf("unable to read private key!\n");
return NULL;
}
rsa_len=RSA_size(rsa2);
p_dcypt=(char *)malloc(rsa_len+1);
memset(p_dcypt,0,rsa_len+1);
// decrypt by using private key
decrylen=RSA_private_decrypt(len_ecypt,(const unsigned char*)p_ecypt,(unsigned char*)p_dcypt,rsa2,RSA_PKCS1_PADDING);
if(decrylen==-1){
printf("failed to decrypt!\n");
return NULL;
}
fclose(priv_fp);
return p_dcypt;
}
示例7: sign
int
sign(char* private_key_path, unsigned char *msg, int msg_len,
unsigned char *sig, unsigned int *sig_len)
{
//Load private key
FILE *fp = fopen(private_key_path, "r");
if(!fp) {
DEBUGMSG(ERROR, "Could not find private key\n");
return 0;
}
RSA *rsa = (RSA *) PEM_read_RSAPrivateKey(fp,NULL,NULL,NULL);
fclose(fp);
if(!rsa) return 0;
unsigned char md[SHA256_DIGEST_LENGTH];
sha(msg, msg_len, md);
//Compute signatur
int err = RSA_sign(NID_sha256, md, SHA256_DIGEST_LENGTH, (unsigned char*)sig, (unsigned int*)sig_len, rsa);
if(!err){
printf("Error: %ul\n", (unsigned int)ERR_get_error());
}
RSA_free(rsa);
return err;
}
示例8: my_decrypt
char* my_decrypt(char* str, char* path_key) {
char* p_de;
RSA* p_rsa;
FILE *file;
int rsa_len;
if((file = fopen(path_key,"r")) == NULL) {
perror("open key file error");
return NULL;
}
if((p_rsa = PEM_read_RSAPrivateKey(file,NULL,NULL,NULL)) == NULL) {
ERR_print_errors_fp(stdout);
return NULL;
}
rsa_len = RSA_size(p_rsa);
p_de = (unsigned char*)malloc(rsa_len + 1);
memset(p_de,0,rsa_len+1);
if(RSA_private_decrypt(rsa_len,(unsigned char*)str,(unsigned char*)p_de,p_rsa,RSA_NO_PADDING) < 0) {
return NULL;
}
RSA_free(p_rsa);
fclose(file);
return p_de;
}
示例9: clearKey
int UtlCryptoKeyRsa::importFromFile(const char* pFilename)
{
// Make sure we don't leave any previous keys hanging around
clearKey();
FILE *pFile = fopen(pFilename, "rt");
if (!pFile)
return -1;
// First try to read a public key
mpRsa = PEM_read_RSA_PUBKEY(pFile, 0, 0, 0);
if (mpRsa)
setKeyType(KEY_PUBLIC);
else
{
// If that failed, try to read a private key
fseek(pFile, 0, SEEK_SET);
mpRsa = PEM_read_RSAPrivateKey(pFile, 0, 0, 0);
if (mpRsa)
setKeyType(KEY_PRIVATE);
}
fclose(pFile);
if (isValid())
return setLastError(0);
else
return -1;
}
示例10: rsa_encrypt_public
/*
* rsa public key encrypt
*/
char* rsa_encrypt_public(unsigned char*txt,int txt_len,char* public_key_str,int p_len,int* enc_len)
{
//char *public_key = "-----BEGIN RSA PUBLIC KEY-----\nMIGJAoGBAL331YpDOljAJznk4eNt0TfZJREYypIhWTN/gx0g1iUIaLPlFR7ydjaB\npd9V7G3GvvOf3mGijP+9LjKdgQ8p1pgDW7DeXZk2dTAeQ4hdY287/sw6NFKJxMXA\nFGoUdARObVespCZBdHSqo8kFMAjVGge6ZoH6nAjGzvIfijgsj+2jAgMBAAE=\n-----END RSA PUBLIC KEY-----\n";
//char * private_key = "-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQC999WKQzpYwCc55OHjbdE32SURGMqSIVkzf4MdINYlCGiz5RUe\n8nY2gaXfVextxr7zn95hooz/vS4ynYEPKdaYA1uw3l2ZNnUwHkOIXWNvO/7MOjRS\nicTFwBRqFHQETm1XrKQmQXR0qqPJBTAI1RoHumaB+pwIxs7yH4o4LI/towIDAQAB\nAoGBAI1ALF2EI2w+ZGxdzcBntXtLUI5n2qfReBwcogcUlWYv3Hp2yb+bFV7uA8IO\nh6AQeYd4xcffL+wwZJtqFb6Ko25XAei8Os3xjb9k5fCcyrmyY+5oeXdQHlcbd/f8\niy8/rOEHZTr4iBXe/8ADlQZlRUkYCblPZ4i4BgzBUB6HzhxhAkEA8wJRx/FjOo6F\noO1aTewbvFIv4Dckqq5j/pBu9fkv1AhMxSfdGnsYcuIn15Y1/RlnpxrmJNWgryvd\n+6LJGDgjWQJBAMgfoINe80YiPCdMoboMd/u1uf1BhwujbiJPSrS40lc3jfyPmHA4\n8hppo8QuELI4rXRE/im4c+zmyphxEyULpVsCQQDnD96JGin65MeE1AsYqpdYwmEJ\ndgVkUXt88wK+2ZizqMyubpAa/M6rdgTiRc7CASUAzF/myEYIKdLh0NAbOk3JAkAE\nxEQVfPh8bipSoU+k19EvzKdOcfYef9kKtirIXTKdYzRdlKoD2kdh+6wr6xD4vcLb\n5xzKr5sLRIAE24SiOEHLAkB1TBlvvvIltttSc9lOpq3UhmtHQJaS32lD2Lk2/zNx\nW6Jbsk+sCQXM0ww4GTCpHMISfokEPtqOPikPcVFs98Oj\n-----END RSA PRIVATE KEY-----\n";
RSA* rsa;
int rsa_len;
char *p_en;
#if 1
//public_key = rsa_key_seliaze(public_key_str);
BIO* p_bio = BIO_new_mem_buf(public_key_str, -1);
printf("rsa_encrypt is %p \n",p_bio);
rsa = PEM_read_bio_RSAPublicKey(p_bio, NULL, NULL, NULL); //PEM_read_bio_RSAPrivateKey
if ( rsa == NULL ) {
printf("RSA is NULL\n");
return NULL;
}
#else
FILE* file=fopen("/tmp/r_pub.key","r");
rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL);//PEM_read_RSAPrivateKey
#endif
rsa_len=RSA_size(rsa);
p_en=(unsigned char *)calloc(rsa_len+1,1);
printf("rsa length = %d\n",rsa_len);
int rc=0;
if((rc=RSA_public_encrypt(txt_len,(unsigned char *)txt,(unsigned char*)p_en,rsa,RSA_PKCS1_PADDING))<=0) {
int e=ERR_get_error();
printf("error code is:%s\n",ERR_error_string(e,NULL));
return NULL;
}
printf("rsa length = %d\n",strlen(p_en));
RSA_free(rsa);
*enc_len = rc;
return p_en;
}
示例11: get_privkey_from_file
void get_privkey_from_file(Octstr* s, RSA** priv_key, Octstr* passwd)
{
char *password;
char *filename;
/* Check errors!!!! */
FILE* fp;
filename = octstr_get_cstr(s);
password = passwd != NULL ? octstr_get_cstr(passwd) : NULL;
/* Open the file specified by "s" */
fp = fopen(filename,"r");
if (fp == NULL) warning(0,"Can't read private key %s", filename);
/* Load up that there certificate */
*priv_key = PEM_read_RSAPrivateKey(fp,NULL,NULL,password);
/* Close the file specified by "s" */
fclose(fp);
if (priv_key == NULL) {
ERR_print_errors_fp (stderr);
}
}
示例12: wi_socket_context_set_ssl_privkey
wi_boolean_t wi_socket_context_set_ssl_privkey(wi_socket_context_t *context, wi_string_t *path) {
#ifdef WI_SSL
FILE *fp;
fp = fopen(wi_string_cstring(path), "r");
if(!fp) {
wi_error_set_errno(errno);
return false;
}
context->priv_rsa = PEM_read_RSAPrivateKey(fp, NULL, 0, NULL);
if(!context->priv_rsa)
wi_error_set_ssl_error();
fclose(fp);
return (context->priv_rsa != NULL);
#else
wi_error_set_lib_error(WI_ERROR_SOCKET_NOSSL);
return false;
#endif
}
示例13: load_rsa_privkey
static RSA *
load_rsa_privkey(const char *path)
{
FILE *fp;
RSA *rsa = NULL;
const char *p;
char *passphrase = NULL;
if ((fp = fopen(path, "r")) == 0)
return NULL;
if ((rsa = RSA_new()) == NULL) {
fclose(fp);
return NULL;
}
p = getenv("XBPS_PASSPHRASE");
if (p) {
passphrase = strdup(p);
}
rsa = PEM_read_RSAPrivateKey(fp, 0, NULL, passphrase);
if (passphrase) {
free(passphrase);
passphrase = NULL;
}
fclose(fp);
return rsa;
}
示例14: rsa_encrypt_private
/*
* rsa private encrypt
*/
char* rsa_encrypt_private(unsigned char*txt,int txt_len,char* public_key_str,int p_len,int* enc_len)
{
RSA* rsa;
int rsa_len;
char *p_en;
#if 1
//public_key = rsa_key_seliaze(public_key_str);
BIO* p_bio = BIO_new_mem_buf(public_key_str, -1);
printf("rsa_encrypt is %p \n",p_bio);
rsa = PEM_read_bio_RSAPrivateKey(p_bio, NULL, NULL, NULL); //PEM_read_bio_RSAPrivateKey
if ( rsa == NULL ) {
printf("RSA is NULL\n");
return NULL;
}
#else
FILE* file=fopen("/tmp/r_pub.key","r");
rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL);//PEM_read_RSAPrivateKey
#endif
rsa_len=RSA_size(rsa);
p_en=(unsigned char *)calloc(rsa_len+1,1);
//printf("rsa length = %d\n",rsa_len);
int rc=0;
if((rc=RSA_private_encrypt(txt_len,(unsigned char *)txt,(unsigned char*)p_en,rsa,RSA_PKCS1_PADDING))<=0) {
int e=ERR_get_error();
printf("error code is:%s\n",ERR_error_string(e,NULL));
return NULL;
}
RSA_free(rsa);
*enc_len = rc;
return p_en;
}
示例15: wi_rsa_init_with_pem_file
wi_rsa_t * wi_rsa_init_with_pem_file(wi_rsa_t *rsa, wi_string_t *path) {
FILE *fp;
fp = fopen(wi_string_cstring(path), "r");
if(!fp) {
wi_error_set_errno(errno);
wi_release(rsa);
return NULL;
}
rsa->rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
if(!rsa->rsa) {
wi_error_set_openssl_error();
wi_release(rsa);
return NULL;
}
return rsa;
}