00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _CRT_SECURE_NO_DEPRECATE
00025 #define _CRT_SECURE_NO_DEPRECATE 1
00026 #endif
00027
00028 #include <stdio.h>
00029
00030 #include "xyssl/bignum.h"
00031
00032 int main( void )
00033 {
00034 mpi E, P, Q, N, H, D, X, Y, Z;
00035
00036 mpi_init( &E, &P, &Q, &N, &H,
00037 &D, &X, &Y, &Z, NULL );
00038
00039 mpi_read_string( &P, 10, "2789" );
00040 mpi_read_string( &Q, 10, "3203" );
00041 mpi_read_string( &E, 10, "257" );
00042 mpi_mul_mpi( &N, &P, &Q );
00043
00044 printf( "\n Public key:\n\n" );
00045 mpi_write_file( " N = ", &N, 10, NULL );
00046 mpi_write_file( " E = ", &E, 10, NULL );
00047
00048 printf( "\n Private key:\n\n" );
00049 mpi_write_file( " P = ", &P, 10, NULL );
00050 mpi_write_file( " Q = ", &Q, 10, NULL );
00051
00052 mpi_sub_int( &P, &P, 1 );
00053 mpi_sub_int( &Q, &Q, 1 );
00054 mpi_mul_mpi( &H, &P, &Q );
00055 mpi_inv_mod( &D, &E, &H );
00056
00057 mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
00058 &D, 10, NULL );
00059
00060 mpi_read_string( &X, 10, "55555" );
00061 mpi_exp_mod( &Y, &X, &E, &N, NULL );
00062 mpi_exp_mod( &Z, &Y, &D, &N, NULL );
00063
00064 printf( "\n RSA operation:\n\n" );
00065 mpi_write_file( " X (plaintext) = ", &X, 10, NULL );
00066 mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL );
00067 mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL );
00068 printf( "\n" );
00069
00070 mpi_free( &Z, &Y, &X, &D, &H,
00071 &N, &Q, &P, &E, NULL );
00072
00073 #ifdef WIN32
00074 printf( " Press Enter to exit this program.\n" );
00075 fflush( stdout ); getchar();
00076 #endif
00077
00078 return( 0 );
00079 }