john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

rsa encryption example in c

/* 2011-07-30 johnpfeiffer simple rsa encryption example , good math http://www.muppetlabs.com/~breadbox/txt/rsa.html  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main( int argc , char* argv[] )
{
    int p = 3 ,  
        q = 11 , 
        n , 
        phi ,
        e , 
        d ;

    n = p * q;  /* n is the modulus for both public & private keys , should have very few factors */
    phi = ( p - 1 ) * ( q - 1 );  /* phi of n */
    e = 3 ;  /* pick e such that: phi is not divisible by e, ergo coprime, and 1 < e < phi */

    printf( "%d is a secret prime 'p' \n" , p );
    printf( "%d is a secret prime 'q' \n" , q );
    printf( "%d is n = p * q = public modulus\n" , n) ;
    printf( "%d is (p-1)*(q-1) = secret phi \n" , phi );
    printf( "pick e such that: phi is not divisible by e, ergo coprime, and 1 < e < phi \n" );   
    printf( "%d is coprime to phi = Public Key (exponent) 'e' \n" , e );

    /*     
    to create a remainder of 1  ...
    (e * d) % (phi) = 1;    
    (7 * d ) % 20 = 1   ... e.g. d = 3

    d = pow( e , -1 );
    */

    d=1;
    while( ((e * d) % phi ) != 1 )
    {
        printf("%d\n", d);
        d++ ;
    }


    printf( "%d is d, the Private Key \n" , d );

    printf( "sender uses ?" )

    return 0;

}/* end main */

  • « javascript arrays
  • javascript servlet form textbox »

Published

Jul 30, 2011

Category

c

~190 words

Tags

  • c 95
  • encryption 3
  • rsa 2