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