Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 5th, 2004, 11:25 PM   #11
fwongmc
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 fwongmc is on a distinguished road
To check if the factors of input number b the user

user input =n,p and q are two factors,tus n=p*q

for (i=1,i<=n,i++)
{ if (n%i==0)
p=i;
q(n/p);
if (n%i>0)
i++

}



No matter how I enters,even if the umber is divisible,it resturn 12XXXX(something like),where my errors?Can anyone point it out and tell me how to correct this?
fwongmc is offline   Reply With Quote
Old Nov 6th, 2004, 2:53 AM   #12
lostcauz
Hobbyist Programmer
 
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4 lostcauz is on a distinguished road
Quote:
Originally posted by fwongmc@Nov 6 2004, 05:17 AM
It can't reply me for any answers.It even cannot be run.Can you tell me what errors do I make and how to Irect this errors?
I haven't coded much C in a while but
I noticed some obvious errors with your code so
I gave some hints here. Watch your syntax. I tried
keeping this in your writing style as best as I could.

gcc -o ptest ptest.c
#include <stdio.h>

int is_prime(int n){ 
  int i,prime;
  for (i=2;i<n;i++){
   if(n%i==0){prime=0;}
   if (n==2){prime=1;}
  }
  if(prime){printf("%d is a prime number",n);}
  else{printf ("%d isn't a prime number",n);}
}

int main(){
  int a;
  printf ("Enter your input:");
  scanf ("%d",&a);
  is_prime(a);
  return 0;
}
__________________
-- lostcauz

Stepped in what?...
Behind whose barn?...
I didn't even know they had a cow!
lostcauz is offline   Reply With Quote
Old Nov 6th, 2004, 10:40 PM   #13
fwongmc
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 fwongmc is on a distinguished road
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

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.
fwongmc is offline   Reply With Quote
Old Nov 7th, 2004, 6: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
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC