- Fixed rsa_encrypt and rsa_decrypt example programs to use public key for encryption and private key for decryption (Fixes ticket #34)
diff --git a/ChangeLog b/ChangeLog
index 83d6f0a..541c86d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,8 @@
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 d00cdaa..384ae92 100644
--- a/programs/pkey/rsa_decrypt.c
+++ b/programs/pkey/rsa_decrypt.c
@@ -65,20 +65,26 @@
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 @@
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 71d7ea8..97243cc 100644
--- a/programs/pkey/rsa_encrypt.c
+++ b/programs/pkey/rsa_encrypt.c
@@ -69,27 +69,21 @@
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 @@
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;