- Fixed rsa_encrypt and rsa_decrypt example programs to use public key for encryption and private key for decryption (Fixes ticket #34)

This commit is contained in:
Paul Bakker 2011-10-06 13:18:27 +00:00
parent ca6f3e24a4
commit d246ed30bd
3 changed files with 20 additions and 18 deletions

View file

@ -11,6 +11,8 @@ Features
Changes Changes
* Documentation for AES and Camellia in modes CTR and CFB128 clarified. * 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 = Version 1.0.0 released on 2011-07-27
Features Features

View file

@ -65,20 +65,26 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
printf( "\n . Reading public key from rsa_pub.txt" ); printf( "\n . Reading private key from rsa_priv.txt" );
fflush( stdout ); 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" ); " ! Please run rsa_genkey first\n\n" );
goto exit; goto exit;
} }
rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_init( &rsa, RSA_PKCS_V15, 0 );
if( ( ret = mpi_read_file( &rsa.N, 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.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 ); printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
goto exit; goto exit;
@ -119,7 +125,7 @@ int main( int argc, char *argv[] )
printf( "\n . Decrypting the encrypted data" ); printf( "\n . Decrypting the encrypted data" );
fflush( stdout ); 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 ) 1024 ) ) != 0 )
{ {
printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret ); printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );

View file

@ -69,27 +69,21 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
printf( "\n . Reading private key from rsa_priv.txt" ); printf( "\n . Reading public key from rsa_pub.txt" );
fflush( stdout ); fflush( stdout );
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
{ {
ret = 1; 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" ); " ! Please run rsa_genkey first\n\n" );
goto exit; goto exit;
} }
rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_init( &rsa, RSA_PKCS_V15, 0 );
if( ( ret = mpi_read_file( &rsa.N , 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.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 ); printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
goto exit; goto exit;
@ -113,7 +107,7 @@ int main( int argc, char *argv[] )
printf( "\n . Generating the RSA encrypted value" ); printf( "\n . Generating the RSA encrypted value" );
fflush( stdout ); 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 ); printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
goto exit; goto exit;