Skip to content

Instantly share code, notes, and snippets.

@fictorial
Created September 10, 2009 03:59
Show Gist options
  • Save fictorial/184283 to your computer and use it in GitHub Desktop.
Save fictorial/184283 to your computer and use it in GitHub Desktop.
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAvlaG3xvlRyrdeL3QDXE7F514jx87ed5kh80BLoOntY2qESvM/2iT
NDCmmWqPvloIogdBRRmeU6UcdOKmbXyMcTzk0d5Aj1LagKeZsC8J+eWD4Hbw0lMU
w17WlwATmgQnpuh1Yb+FKwg8sM4SVZSUjMfkiLfob7yiofQnhkMrMj/f3OQl8eVs
f0ciDvLGFLdPJ5omwM6MfAcwyNAtUAW3hRwCgTYhbSqzA+cOYyjV3GmgojRvwfy2
H84L2c/IBSOwe3ZtyADTw7NVx29GQqFhVD6SnWFnfoOpOxddfM5qtgId+1+w7/+n
DZjzMzBwyfXSiTd3uh2Dwnyz3FevWZH4ywIDAQAB
-----END RSA PUBLIC KEY-----
/*
* This is a test of using OpenSSL's RSA encryption functions.
*
* - it generates a 2048 bit RSA keypair.
* - it writes the public key to a file (public.pem).
* - it writes the private key to a PEM file (private.pem).
* - it encrypts some text with the public key.
* - it decrypts the encrypted text and verifies that the original text can be recovered.
*
* See http://www.openssl.org/docs/crypto/RSA_public_encrypt.html
*
* Brian Hammond, Fictorial, Wed Sep 09 23:57:04 EDT 2009
*
* gcc -o rsa_test rsa_test.c -lcrypto
* ./rsa_test
*/
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/pem.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <strings.h>
static void fatal_error(const char *context) {
char err_msg[120];
unsigned long err_code;
while ((err_code = ERR_get_error()) != 0)
fprintf(stderr, "%s: %s\n", context, ERR_error_string(err_code, err_msg));
exit(1);
}
int main(int argc, char **argv) {
FILE *fp;
RSA *rsa;
srand(time(0)); // Not sure.
fprintf(stderr, "generating 2048-bit RSA keypair\n");
rsa = RSA_generate_key(2048, 65537, NULL, NULL);
if (!rsa) fatal_error("RSA_generate_key");
fprintf(stderr, "saving public key to public.pem\n");
if ((fp = fopen("public.pem", "wb")) == NULL) {
perror("fopen public.pem");
exit(1);
}
if (!PEM_write_RSAPublicKey(fp, rsa))
fatal_error("PEM_write_RSAPublicKey");
fclose(fp);
fprintf(stderr, "saving private key to private.pem\n");
if ((fp = fopen("private.pem", "wb")) == NULL) {
perror("fopen private.pem");
exit(1);
}
if (!PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL))
fatal_error("PEM_write_RSAPrivateKey");
fclose(fp);
fprintf(stderr, "testing encryption\n");
char *source_text = "hello there, world";
fprintf(stderr, "source_text = %s\n", source_text);
int size = RSA_size(rsa) + 1; // For NULL.
unsigned char *encrypted_text = (unsigned char *)malloc(size);
bzero(encrypted_text, size);
int encrypted_text_size = RSA_public_encrypt(strlen(source_text),
(unsigned char *)source_text,
encrypted_text,
rsa,
RSA_PKCS1_OAEP_PADDING);
if (encrypted_text_size == -1)
fatal_error("RSA_public_encrypt");
fprintf(stderr, "encrypted_text_size = %d\n", encrypted_text_size);
unsigned char *decrypted_text = (unsigned char *)malloc(size);
bzero(decrypted_text, size);
int decrypted_text_size = RSA_private_decrypt(encrypted_text_size,
encrypted_text,
decrypted_text,
rsa,
RSA_PKCS1_OAEP_PADDING);
if (decrypted_text_size == -1)
fatal_error("RSA_private_decrypt");
fprintf(stderr, "decrypted_text_size = %d\n", decrypted_text_size);
fprintf(stderr, "decrypted_text = %s\n", decrypted_text);
if (strncmp(source_text, (char *)decrypted_text, decrypted_text_size))
fprintf(stderr, "FAIL\n");
else
fprintf(stderr, "PASS\n");
free(encrypted_text);
free(decrypted_text);
RSA_free(rsa);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment