Created
March 2, 2011 13:33
-
-
Save clyfe/850935 to your computer and use it in GitHub Desktop.
rsa crypto
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
/* Copyright(C) by William Estrada Jul 16, 2008, All rights reserved * | |
* [email protected] */ | |
#define _GNU_SOURCE | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <openssl/rsa.h> | |
#include <openssl/bio.h> | |
#include <openssl/rand.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <openssl/blowfish.h> | |
#define K_Size 128 | |
#define B_Size 1024 | |
#define Say(M) \ | |
printf( "%3d %s \tIn: %3d: '%s'\n\t\tOut: %3d: '%s'\n", \ | |
__LINE__, M, strlen(In), In, strlen(Out), Out ); | |
#define ON_ERR(Test,M, Action) \ | |
if( Test ) { printf( "%3d %s\n", __LINE__,M ); Action; } | |
#define DUMP() \ | |
printf("Out:"); \ | |
for( _P = Out, I=0; I < 32; I++, _P++ ) { \ | |
if( I%4 == 0 ) printf(" "); \ | |
printf( "%2.2X", *_P ); } \ | |
printf("\n"); | |
#define REFORMAT(T) \ | |
\ | |
ERR_load_crypto_strings(); \ | |
Code = ERR_get_error(); \ | |
ERR_error_string( Code, Error ); \ | |
L = strlen(Error); _P = Error; \ | |
for(I=0,B=0;I<L;I++, ++_P ) { \ | |
if( *_P == ':' ) { \ | |
if( ++B%2 == 0 ) { \ | |
*_P = '\n'; } } } \ | |
printf( "\n%.3d %s failed:\nCode: %d\n%s\n\n", \ | |
__LINE__, T, Code, Error ); | |
char Scale [] = " ---------|---------|---------|" | |
"---------|---------|---------"; | |
RSA *My_RSA, *Pub_RSA, *New_RSA; | |
BIO *BP; | |
char Public_Key[B_Size]; | |
char Error[200]; unsigned long Code; | |
unsigned | |
char In[ B_Size], Out[ B_Size], Work[B_Size], msg[100], *Key, *Ptr, | |
Sym_Data[K_Size], *_P; | |
int RC, Size, Len, Run, Key_Size; | |
BF_KEY Sym_Key; | |
BUF_MEM *Pbuf; | |
int Number, Done, I, B, L; | |
int | |
main( ) { | |
// Genterate random seed | |
Number = open( "/dev/urandom", O_RDONLY ); | |
ON_ERR( Number < 0, "Random device failed open: ", return 0; ); | |
RC = read( Number, Sym_Data, K_Size ); | |
RAND_add( &Sym_Data, K_Size, K_Size ); | |
RAND_bytes( Sym_Data, K_Size ); | |
// Create Symmetric Key | |
BF_set_key( &Sym_Key, K_Size, Sym_Data ); | |
// Test Sym Encryption and Decryption --------------------------------- */ | |
printf( "Begin Sym test\n" ); | |
for( Run = 1; Run; ) { | |
printf( "%s\nEnter text string: ", Scale );fflush(stdout); | |
fgets( In, 80, stdin ); | |
if( !strcmp( "q\n", In ) ) return; | |
if( In[0] == '\n' ) break; | |
Len = strlen(In)-1; | |
In[Len] = Out[0] = '\0'; | |
Say("Before"); | |
Sym_Encrypt( In, Work, Len ); | |
Sym_Decrypt( Work, Out, Len ); | |
Say("After"); } | |
BIO *bio = BIO_new(BIO_s_mem()); | |
// Create RSA Public/Private Keys | |
My_RSA = RSA_generate_key( 1024, 65537, NULL, NULL ); | |
if( !RSA_check_key( My_RSA )) { printf( " RSA failed\n" ); return 1; } | |
// Extract Public Key from RSA | |
Key = Ptr = malloc( B_Size ); | |
Key_Size = i2d_RSAPublicKey( My_RSA, &Ptr ); | |
Pub_RSA = d2i_RSAPublicKey( NULL, (const unsigned char **) &Key, Key_Size ); | |
ON_ERR( Pub_RSA == NULL, " Pub_RSA failed NULL\n", return 1; ); | |
RC = PEM_write_bio_RSAPublicKey( bio, Pub_RSA ); | |
Size = BIO_get_mem_data( bio, &Pbuf ); | |
printf("%3d Size: %d\n%*s", __LINE__, Size, Size, Pbuf ); | |
if( Size < B_Size ) { sprintf( Public_Key, "%.*s", Size, Pbuf ); } | |
else { printf( "Public Key too large\n" ); return 0; } | |
// Create Public RSA for Sym encryption. | |
BIO_reset(bio); | |
RC = BIO_write( bio, &Public_Key, Size ); | |
New_RSA = (RSA*) PEM_read_bio_RSAPublicKey( bio, NULL, NULL, NULL ); | |
// Test RSA Encryption and Decryption --------------------------------- */ | |
printf( "Begin RSA test\n" ); | |
for( Run = 1; Run; ) { | |
printf( "%s\nEnter text string: ", Scale );fflush(stdout); | |
fgets( In, 80, stdin ); | |
if( !strcmp( "q\n", In ) ) return; | |
if( In[0] == '\n' ) break; | |
Len = strlen(In)-1; | |
In[Len] = Out[0] = '\0'; | |
Say("Before"); | |
bzero(Work,B_Size); | |
Size = RSA_Encrypt( In, Work, Len ); | |
if( !Size ) continue; | |
bzero(Out, B_Size); | |
RC = RSA_Decrypt( Work, Out, Size ); | |
Say("After"); } | |
/* RSA Encrypt the Symmetric Key -------------------------------------- */ | |
printf( "Testing RSA encryption of Symmertic key\n" ); | |
bzero(Work, B_Size); | |
Size = RSA_Encrypt( (unsigned char*) &Sym_Key, Work, K_Size ); | |
ON_ERR( Size < 1, "Symmetric key encryption failed", ; ); | |
return 1; } | |
/* ---------------------------------------------------------------------- */ | |
int | |
RSA_Encrypt( unsigned char *In, unsigned char *Out, unsigned int Bytes ) { | |
int Size; | |
printf("%.3d Encrypt In bytes: %4d, ", __LINE__, Bytes ); | |
Size = RSA_public_encrypt( Bytes, In, Out, New_RSA, RSA_PKCS1_PADDING ); | |
if( Size < 0 ) { REFORMAT("Encript"); } | |
printf( "Out bytes: %d\n", Size ); | |
DUMP(); | |
return Size; } | |
/* ------------------------------------------------------------------ */ | |
int | |
RSA_Decrypt( unsigned char *In, unsigned char *Out, unsigned int Bytes ) { | |
int Size; | |
printf("%.3d Decrypt in bytes: %4d, ", __LINE__, Bytes ); | |
Size = RSA_private_decrypt( Bytes, In, Out, My_RSA, RSA_PKCS1_PADDING ); | |
if( Size < 0 ) { REFORMAT("Decript"); } | |
return 0; } | |
/* ------------------------------------------------------------------ */ | |
int | |
Sym_Decrypt( unsigned char *In, unsigned char *Out, unsigned int Bytes ) { | |
int Count = 0; | |
unsigned char Vector[8]; | |
memset( Vector, '\0', 8 ); | |
memset( Out, '\0', B_Size ); | |
BF_cfb64_encrypt( In, Out, Bytes, &Sym_Key, Vector, &Count, BF_DECRYPT ); | |
return Count; } | |
/* ------------------------------------------------------------------ */ | |
int | |
Sym_Encrypt( unsigned char *In, unsigned char *Out, unsigned int Bytes ) { | |
int Count = 0; | |
unsigned char Vector[8]; | |
memset( Vector, '\0', 8 ); | |
memset( Out, '\0', B_Size ); | |
BF_cfb64_encrypt( In, Out, Bytes, &Sym_Key, Vector, &Count, BF_ENCRYPT ); | |
return Count; } | |
/* ------------------------------------------------------------------ */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment