Issue of generating RSA key in Zz:openssl and reading PEM file

Source: Internet
Author: User
Tags error handling openssl openssl rsa

The original moved, even from here to turn the bar:

ZZ from:http://blog.csdn.net/lazyclough/article/details/7646696


command to generate OpenSSL RSA key:

OpenSSL genrsa-out Private.key 1024
OpenSSL rsa-in private.key-pubout-out public.key BIO * key = NULL;
RSA * r = NULL;
Key = Bio_new (Bio_s_file ());
Bio_read_filename (Key, "C:\\private.key");
R = Pem_read_bio_rsaprivatekey (key, NULL, NULL, NULL);
Bio_free_all (key);

Read private key read Normal, R does not return empty
Read the public key BIO * key = NULL using the same method;
RSA * r = NULL;
Key = Bio_new (Bio_s_file ());
Bio_read_filename (Key, "C:\\public.key");
R = Pem_read_bio_rsapublickey (key, NULL, NULL, NULL);
Bio_free_all (key);

Not normal, R always returns NULL, change the function Pem_read_bio_rsa_pubkey
R1 = Pem_read_bio_rsa_pubkey (Key1, null,null,null);
Openssl_to_keys (R, 1024, PRIV, pub);
R1 Read succeeded

Report:

OpenSSL 8---Pem object read-write IO function (ii)
---According to OpenSSL source code, Ssleay documents and other related materials written
(Author: dragonking, Mail:wzhah@263.net, posted in: http://openssl.126.com OpenSSL Professional Forum, version: openssl-0.9.7)
This article continues to introduce the read-write IO function of the Pem object, see "OpenSSL's Pem Series 7" To better understand this article.
"The IO that conforms to the PKCS#8 and Pkcs#5 v2.0 standard private Key Object Pkcs8privatekey"
int Pem_write_bio_pkcs8privatekey (bio *bp, Evp_pkey *x, const-Evp_cipher *enc,char *kstr, int klen,pem_password_cb *CB, VO ID *u);
int Pem_write_pkcs8privatekey (FILE *fp, Evp_pkey *x, const evp_cipher *enc,char, int *kstr klen,pem_password_cb, void *U);
These two functions use the PKCS#8 standard to save the private key in the Evp_pkey to the file or the bio, and use the pkcs#5
v2.0 the standard cryptographic private key. The ENC parameter defines the cryptographic algorithm used. Unlike other PEM IO functions, the encryption of this function is based on the pkcs#8 level rather than on the PEM information field, so these two functions are also implemented separately, not macro-defined functions. If the ENC parameter is NULL, the encryption operation is not performed and only the Pkcs#8 private key information is used
Structure. Successful execution returns a number greater than 0, otherwise it returns 0.
A Pem object saved using these two functions can be read using the Pem_read_bio_privatekey or Pem_read_privatekey described in the previous article.
The following is an example of "Hello", a password that saves the private key as a pkcs#8 format and encrypts it using the 3DES algorithm.
if (! Pem_write_bio_pkcs8privatekey (BP, Key, EVP_DES_EDE3_CBC (), NULL, 0, 0, "Hello")
{
/* ERROR Handling code * *
}
"Io that conforms to the pkcs#8 and pkcs#5 v1.5 or pkcs#12 standard private key Object Pkcs8privatekey"
int Pem_write_bio_pkcs8privatekey_nid (bio *bp, Evp_pkey *x, int nid,char *kstr, int klen,pem_password_cb *cb, void *u);
int Pem_write_pkcs8privatekey_nid (FILE *fp, Evp_pkey *x, int nid,char *kstr, int klen,pem_password_cb *cb, void *u);
These two functions are also functions that are implemented separately, not macro-defined functions. They also save the private key in pkcs#8 format, but in a way that pkcs#5 v1.5 or pkcs#12 to encrypt the private key. The NID parameter specifies the corresponding encryption algorithm whose value should be the NID of the corresponding object. Successful execution returns a number greater than 0, otherwise it returns 0.
A Pem object saved using these two functions can be read using the Pem_read_bio_privatekey or Pem_read_privatekey described in the previous article.
"Public-Key object PubKey io"
Evp_pkey *pem_read_bio_pubkey (bio *bp, Evp_pkey **x,pem_password_cb *cb, void *u);
Evp_pkey *pem_read_pubkey (FILE *fp, Evp_pkey **x,pem_password_cb *cb, void *u);
int Pem_write_bio_pubkey (bio *bp, Evp_pkey *x);
int Pem_write_pubkey (FILE *fp, Evp_pkey *x);
These four functions read and write the public key of the EVP_PKEY structure in PEM format. The public key is encoded as a subjectpublickeyinfo storage structure.
"RSA private Key object Rsaprivatekey io"
RSA *pem_read_bio_rsaprivatekey (Bio *BP, RSA **X,PEM_PASSWORD_CB *cb, void *u);
RSA *pem_read_rsaprivatekey (FILE *FP, RSA **X,PEM_PASSWORD_CB *cb, void *u);
int Pem_write_bio_rsaprivatekey (bio *bp, RSA *x, const evp_cipher *enc,unsigned char *kstr, int KLEN,PEM_PASSWORD_CB *CB, void *u);
int Pem_write_rsaprivatekey (FILE *fp, RSA *x, const evp_cipher *enc,unsigned char *kstr, int klen,pem_password_cb *CB, voi D *u);
These four functions perform read-write processing of the RSA private key for the RSA structure in PEM format. It is processed using the same functions as Privatekey, but if the private key type is not RSA, an error message is returned.
"RSA public-Key object Rsapublickey io"
RSA *pem_read_bio_rsapublickey (Bio *BP, RSA **X,PEM_PASSWORD_CB *cb, void *u);
RSA *pem_read_rsapublickey (FILE *FP, RSA **X,PEM_PASSWORD_CB *cb, void *u);
int Pem_write_bio_rsapublickey (bio *bp, RSA *x);
int Pem_write_rsapublickey (FILE *fp, RSA *x);
These four functions are read-write processing in PEM format for the public key of the RSA structure. This function encodes the RSA public key using the PKCS#1 Rsapublickey structure standard.
"RSA public-Key object Rsa_pubkey io"
RSA *pem_read_bio_rsa_pubkey (Bio *BP, RSA **X,PEM_PASSWORD_CB *cb, void *u);
RSA *pem_read_rsa_pubkey (FILE *FP, RSA **X,PEM_PASSWORD_CB *cb, void *u);
int Pem_write_bio_rsa_pubkey (bio *bp, RSA *x);
int Pem_write_rsa_pubkey (FILE *fp, RSA *x);
These four functions are also read-write processing in PEM format for the public key of the RSA structure. However, this function encodes the RSA public key using the SUBJECTPUBLICKEYINFO structure standard, and if the public key type is not RSA, an error returns the failure information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.