RSA encrypt/decrypt c program

From: Frank (frankmcwong_at_hotmail.com)
Date: 11/08/04

  • Next message: Nathan Mates: "Re: tetrii rotation"
    Date: Mon, 8 Nov 2004 22:13:32 +0800
    
    

    RSA program

    ----------------------------------------------------------------------------

    ----
    Dear fellows,
    I am new to C.My tutor ask us to write a C program with limitations,for
    decoding and encoding similiar to RSA.
    The question are as follows:
    ----------------------------------------------------------------------------
    -------------------
    To encode a message,one needs n and e.The value n is a product of any two
    prime numbers p and q.The value e is any number less than n that cannot be
    evenly divided into y(i.e. y/e would have a reminder),where
    y=(p-1)*(q-1).The values n and e can be published in a newspaper or posted
    on the internet,so anybody can encrypt messages.The original character is
    than encoded to a numerical value c using the formula:
    c=m^e mod n
    where m is a numerical representation of the original
    character(65=A,66=B..etc)
    Now,to decode a messages,one needs d.The value d is a number that statisfies
    the formula:
    (e*d) mod y ==1
    where e and y are the values defined in the encoding step.The original
    character m can be derived from the encrypted character c by using the
    formula:
    m=c^d mod n
    To write a program that encodes and decodes message using this system,you
    will need to generate the oublic and secret key pairs:
    Public key (KU)=(e,n) Secret key KR=(d,n)
    Limitations:
    1.Only functions is allowed
    2.Simple Calculations(+-*/) is allowed.No power (x^y) is allowed
    3.For the calculations,((a mod n)*(b mod n)) mod n == (a* mod n
    fwongmc Posted: Nov 5 2004, 05:17 PM
    Newbie
    Group: Members
    Posts: 9
    Member No.: 683
    Joined: 5-November 04
    (cont'd)
    3.Hints(a mod n)*(b mod n)) mod n == (a*b)mod n
    ----------------------------------------------------------------------------
    -------------------
    Sample run:
    Do you want to start? (Y/N) y
    Please enter the public key e:5
    Please enter the common key n:119
    Please enter the private key e:77
    Encrypt or Decrypt? (E/D) e
    Please enter 5 characters.
    abcde
    The encrypted message in number format is 20 98 29 52 35
    Do you want to play again? (y/n)y
    Encrypt or Decrypt? (E/D) d
    Please enter 5 numbers.
    20 98 29 53 33
    The decrypted message char format is a b c d e
    Do you want to play again? (y/n)n
    Thank you for using this program
    ====================================================
    My code:
    #include <stdio.h>
    int prime=1;
    int i,number;
    int num1,num2,num3,num4,num5;
    int e,n,d,y,p,q;
    int is_prime(int n){
     for (i=2;i<n;i++){
      if(n%i==0)
       return 0;
       }
       return 1;
       }
    int decrypt(int num){
    int a,f=1;
    int b,c,i,j;
    {
     b=check_div(e);
     c=e/b;
     for (i=1;i<=b;i++){
      a=(num%n)*a;
      }
     for (j=1;j<=c;j++){
      f=(num%n)*f;}
      number=((a*f)%n);
      return (num);
      }
      return 0;
      }
    int encrypt(int num){
    int a,f=1;
    int b,c,i,j;
    {
     b=check_div(d);
     c=d/b;
     for (i=1;i<=b;i++){
      a=(num%n)*a;
     }
     for (j=1;j<=c;j++){
      f=(num%n)*f;}
      number=((a*f)%n);
      return (num);
      }
      return 0;
      }
    int check_div(int x){
    int i=2;
    int a=0;
    {
    do {
      a=(n%i);
      i++;  }while (n%i>0);
      return (i);
      }
      return 0;
       }
    int main (void){
    char choice,choice2;
    char choice3='y';
    printf ("Do you want to start?");
     scanf("%c",&choice);
    if ((choice=='n')||(choice=='N')){
     printf ("Thank you for using this program.");}
    if ((choice=='y')||(choice=='Y')){
     printf ("Please enter the Public Key e:");
      scanf ("%d",&e);
     printf ("Please enter the common key n:");
      scanf ("%d",&n);
     printf ("Please enter the private key:");
     scanf ("%d",&d);
    /*To check if p and q are prime numbers*/
    if ((is_prime(p)==1)&&(is_prime(q)==1)){
     y=(p-1)*(q-1);}
    if ((is_prime(p)==0)||(is_prime(q)==0)){
     printf ("Check your common key.");}
    /*Prompt user to select encrypt nor decrypt*/
    do {
    printf ("Encrypt or Decrypt? (E/D)");
    fflush(stdin);
    scanf ("%c",&choice2);}
    /*To find the factors of n QUESTION 2*/
    if (check_div(n)>0){
     p=check_div(n);
     q=n/p;}
    if ((choice2=='e')||(choice2=='E')){
      printf ("Please enter 5 characters.");
      scanf ("%d %d %d %d %d",&num1,&num2,&num3,&num4,&num5);
      printf ("The encrypted message in number format is %d %d %d %d
    %d",decrypt(num1),decrypt(num2),decrypt(num3),decrypt(num4),decrypt(num5));
      printf ("Do you want to play again ?(Y/N)");
      fflush(stdin);
      scanf ("%c",&choice3);}
     if ((choice2=='d')||(choice2=='D')){
      printf ("Please enter 5 numbers.");
      scanf ("%d %d %d %d %d",&num1,&num2,&num3,&num4,&num5);
       if ((e*d)%y==1){
        printf ("The decrypted message char format is %c %c %c %c
    %c",encrypt(num1),encrypt(num2),encrypt(num3),encrypt(num4),encrypt(num5));}
        printf ("Do you want to play again ?(Y/N)");
        fflush(stdin);
        scanf ("%c",&choice3);}
     }  while ((choice3=='y')||(choice3=='Y'));
    printf ("Thank you for using this program.");
    }
    The above is my new code.I get stuck with the calculations.Here is little
    discreption of the three functions(excluding is_prime)
    1.For check_div,is to check the two factors of the user input 'n'.I used a
    for loop to count on n%i and stops if the result are 0.But I don't know if I
    wrote it right.
    2.For the function decryption,is counting on (m^e)%n,/For the function
    encryptionmis counting on (c^d)%n,using of power function is not allowed in
    this program by out tutor,thus I use a for loop again to accumlate e times
    for (m mod n).But since the output may out of range which C can store,thus I
    use the check_div function to find the two factors of e,and calculation it
    seperatly.(i.e.: ((a mod n)*(b mod n))mod n==(a*b) mod n
    3.Since i used the check_div function in the decrypt/encrypt,but the Vc++
    told me that it is a warning,why this happen,and what should I do for it?
    4.I get stuck with the do-while loop.The question want us to continue to ask
    the user for encryption/decryption and do unless if the user enter 'n'.But
    whether I enter,for example,if I enter e for encryption,therefore if I enter
    yes,it will prompt to ask for thee same type only..
    I did tried one night and I cannot figure out what's the problem,can you
    help me to debug my program? I will be very pleased if you do so.
    [
    

  • Next message: Nathan Mates: "Re: tetrii rotation"

    Relevant Pages

    • Re: Can somebody help...
      ... a number of unsafe constructs and non-standard extensions. ... That value is too large to portably store in an int. ... This results in undefined behavior because you lied to printf about what ...
      (comp.lang.cpp)
    • Re: Is this correct..??
      ... long int i; ... Almost everthing is defined in assembler. ... Easily defeated by compiler optimisations. ... printf and that the first assignment to i is never used and decides to ...
      (comp.lang.c)
    • Semaphores Block when they Shouldnt and Dont when they Should
      ... took the votes (encoded in the first character of the transmitted mes- ... int socketAccess; ... write(voteSocket, voteBuffer, 2); ... {printf("Couldn't make a socket.\n"); ...
      (comp.unix.programmer)
    • Re: Usefulness of %hd in printf
      ... and the corresponding argument was of type 'short int'? ... Think about the scanf() family of functions. ... I'm asking about the printf() family. ... Are you saying that there is no difference in printf, ...
      (comp.lang.c)
    • Re: Inconsistent Program Results
      ... and adding ifabove the printf ... int main ... passing to strchr. ... Well, OK, if you know that char * and void * have the same internal ...
      (comp.lang.c)