-
-
Save ahnan4arch/5b7ddb5e7d8e74640070badf226683c4 to your computer and use it in GitHub Desktop.
key.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| example of c equivalent to php | |
| <?php | |
| // Create the keypair | |
| $res=openssl_pkey_new(); | |
| // Get private key | |
| openssl_pkey_export($res, $privkey); | |
| // Get public key | |
| $pubkey=openssl_pkey_get_details($res); | |
| $pubkey=$pubkey["key"]; | |
| ?> | |
| */ | |
| #include<stdio.h> | |
| #include<openssl/rsa.h> | |
| #include<openssl/evp.h> | |
| EVP_PKEY *pubkey_a,*privkey_a; | |
| generate_keys () | |
| { | |
| RSA *rsa, *pub_rsa, *priv_rsa; | |
| int len; | |
| unsigned char *buf, *p; | |
| rsa = RSA_generate_key (1024, RSA_F4, NULL, (char *) stdout); | |
| buf = (char *) malloc (2048); | |
| p = buf; | |
| len = i2d_RSAPublicKey (rsa, &p); | |
| len += i2d_RSAPrivateKey (rsa, &p); | |
| RSA_free (rsa); | |
| p = buf; | |
| pub_rsa = d2i_RSAPublicKey (NULL, &p, (long) len); | |
| len -= (p - buf); | |
| priv_rsa = d2i_RSAPrivateKey (NULL, &p, (long) len); | |
| if ((pub_rsa == NULL) || (priv_rsa == NULL)) | |
| ERR_print_errors_fp (stderr); | |
| pubkey_a = EVP_PKEY_new (); | |
| privkey_a = EVP_PKEY_new (); | |
| EVP_PKEY_set1_RSA (pubkey_a, pub_rsa); | |
| EVP_PKEY_set1_RSA (privkey_a, priv_rsa); | |
| RSA_free (pub_rsa); | |
| RSA_free (priv_rsa); | |
| } | |
| main() | |
| { | |
| EVP_PKEY *pubkey,*privkey; | |
| generate_keys (); | |
| pubkey=pubkey_a; | |
| privkey=privkey_a; | |
| PEM_write_PUBKEY(stdout, pubkey); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment