/* johnpfeiffer 2011-08 example rsa - it requires: gcc -o outputbinary inputfile.c -lm */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main( int argc , char* argv[] )
{
int primeA = 3 ,
primeB = 11 ,
privatekeyphi ,
publickeymodulo ,
publickey ,
privatekey ,
plaintext =7 ,
encryptedtext ;
/* the modulus for both public & private keys should have very few factors */
publickeymodulo = primeA * primeB;
privatekeyphi = ( primeA - 1 ) * ( primeB - 1 );
publickey = 3 ; /* picked such that: phi is coprime (not divisble by publickey), and 1 < publickey < phi */
printf( "%d is a secret prime \n" , primeA );
printf( "%d is a secret prime \n" , primeB );
printf( "%d is primeA * primeB = public modulus\n" , publickeymodulo ) ;
printf( "%d is (primeA - 1) *( primeB - 1 ) = secret phi \n" , privatekeyphi );
printf( "pick publickey such that it is coprime of phi and 1 < publickey < phi \n" );
printf( "%d is the Public Key (coprime to phi and the 'exponent') \n" , publickey );
/*
to create a remainder of 1 ...
(e * d) % (phi) = 1;
(7 * d ) % 20 = 1 ... e.g. d = 3
d = pow( e , -1 );
*/
privatekey=1;
while( ((publickey * privatekey) % privatekeyphi ) != 1 )
{
printf( "%d\n ", privatekey);
privatekey++ ;
}
printf( "%d is the Private Key \n" , privatekey );
printf( "sender uses encrypted = (plaintext) ^ e \n" );
encryptedtext = (int) ( pow( plaintext , publickey ) ) % publickeymodulo ;
printf( "%d encrypted becomes %d \n" , plaintext , encryptedtext );
plaintext = (int) ( pow( encryptedtext , privatekey ) ) % publickeymodulo ;
printf( " plaintext = (int) ( pow( encryptedtext , privatekey ) ) mod publickeymodulo ; \n");
printf( "%d decrypted becomes %d \n" , encryptedtext , plaintext );
return 0;
}/* end main */