From d246ed30bd0d96866c9172d5882ee6f09c0df3b4 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Thu, 6 Oct 2011 13:18:27 +0000 Subject: [PATCH] - Fixed rsa_encrypt and rsa_decrypt example programs to use public key for encryption and private key for decryption (Fixes ticket #34) --- ChangeLog | 2 ++ programs/pkey/rsa_decrypt.c | 18 ++++++++++++------ programs/pkey/rsa_encrypt.c | 18 ++++++------------ 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 83d6f0a72..541c86de0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,8 @@ Features Changes * Documentation for AES and Camellia in modes CTR and CFB128 clarified. + * Fixed rsa_encrypt and rsa_decrypt examples to use public key for + encryption and private key for decryption. (Closes ticket #34) = Version 1.0.0 released on 2011-07-27 Features diff --git a/programs/pkey/rsa_decrypt.c b/programs/pkey/rsa_decrypt.c index d00cdaae7..384ae926c 100644 --- a/programs/pkey/rsa_decrypt.c +++ b/programs/pkey/rsa_decrypt.c @@ -65,20 +65,26 @@ int main( int argc, char *argv[] ) goto exit; } - printf( "\n . Reading public key from rsa_pub.txt" ); + printf( "\n . Reading private key from rsa_priv.txt" ); fflush( stdout ); - if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) + if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) { - printf( " failed\n ! Could not open rsa_pub.txt\n" \ + printf( " failed\n ! Could not open rsa_priv.txt\n" \ " ! Please run rsa_genkey first\n\n" ); goto exit; } rsa_init( &rsa, RSA_PKCS_V15, 0 ); - if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || - ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) + if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || + ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || + ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || + ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 || + ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 || + ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 || + ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || + ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) { printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); goto exit; @@ -119,7 +125,7 @@ int main( int argc, char *argv[] ) printf( "\n . Decrypting the encrypted data" ); fflush( stdout ); - if( ( ret = rsa_pkcs1_decrypt( &rsa, RSA_PUBLIC, &i, buf, result, + if( ( ret = rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &i, buf, result, 1024 ) ) != 0 ) { printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret ); diff --git a/programs/pkey/rsa_encrypt.c b/programs/pkey/rsa_encrypt.c index 71d7ea869..97243ccbd 100644 --- a/programs/pkey/rsa_encrypt.c +++ b/programs/pkey/rsa_encrypt.c @@ -69,27 +69,21 @@ int main( int argc, char *argv[] ) goto exit; } - printf( "\n . Reading private key from rsa_priv.txt" ); + printf( "\n . Reading public key from rsa_pub.txt" ); fflush( stdout ); - if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) + if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) { ret = 1; - printf( " failed\n ! Could not open rsa_priv.txt\n" \ + printf( " failed\n ! Could not open rsa_pub.txt\n" \ " ! Please run rsa_genkey first\n\n" ); goto exit; } rsa_init( &rsa, RSA_PKCS_V15, 0 ); - if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || - ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || - ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || - ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 || - ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 || - ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 || - ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || - ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) + if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || + ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) { printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); goto exit; @@ -113,7 +107,7 @@ int main( int argc, char *argv[] ) printf( "\n . Generating the RSA encrypted value" ); fflush( stdout ); - if( ( ret = rsa_pkcs1_encrypt( &rsa, havege_rand, &hs, RSA_PRIVATE, strlen( argv[1] ), input, buf ) ) != 0 ) + if( ( ret = rsa_pkcs1_encrypt( &rsa, havege_rand, &hs, RSA_PUBLIC, strlen( argv[1] ), input, buf ) ) != 0 ) { printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret ); goto exit;