View Single Post
Old Nov 7th, 2004, 7:10 AM   #14
fwongmc
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 fwongmc is on a distinguished road
I rewrite my program with functions:
#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=1;
int i,number;
{for (i=1;i<=e;i++){
a=(num%n)*a;
}
number=(a%n);
return (number);
}
return 0;
}


int encrypt(int num){
int a=1;
int i,number;
{for (i=1;i<=d;i++){
a=(num%n)*a;
}
number=(a%n);
return (number);
}
return 0;
}

int check_div(int x){
int i=1;
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 find the factors of n QUESTION 2*/
if (check_div(n)>0){
p=check_div(n);
q=n/p;}

/*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*/
printf ("Encrypt or Decrypt? (E/D)");
scanf ("%c",&choice2);

do {
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)");
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)");
scanf ("%c",&choice3);}
} while ((choice3=='y')||(choice3=='Y'));
printf ("Thank you for using this program.");
}


but it seems that it can't works...it stop when asking user for decrypt/encrypt.I don't know wether is the condition statement wrong and/or the function wrong(can either be <decrypt>,<encrypt>,<check_div>).Can anyone help me to check check and tell me what the error was?URGENT!Thanks.
fwongmc is offline   Reply With Quote