00001 /* 00002 * Simple MPI demonstration program 00003 * 00004 * Copyright (C) 2006-2007 Christophe Devine 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 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 }