🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: be.comp.programming
Article #1948

Urgent C programming problems

#1948
From: Frank
Date: Sun, 07 Nov 2004 16:36
192 lines
5854 bytes
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.


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

C / C++ 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=1;
int i;
{for (i=1;i<=e;i++){
    a=(num%n)*a;
    }
    num=(a%n);
    return (num);
    }
    return 0;
    }


int encrypt(int num){
int a=1;
int i;
{for (i=1;i<=d;i++){
    a=(num%n)*a;
    }
    num=(a%n);
    return (num);
    }
    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.i
Technical Details
Message-ID: <cmljt8$efo$1@nnews.pacific.net.hk>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!newshub.sdsu.edu!newsgate.cuhk.edu.hk!newsfeeder.ust.hk!nntp.pacific.net.hk!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: fcea4,ebbac7bccf48431c,start
X-Google-Attributes: gidfcea4,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!newshub.sdsu.edu!newsgate.cuhk.edu.hk!newsfeeder.ust.hk!nntp.pacific.net.hk!not-for-mail
From: "Frank" <frankmcwong@hotmail.com>
Newsgroups: be.comp.programming
Subject: Urgent C programming problems
Date: Mon, 8 Nov 2004 00:36:54 +0800
Organization: Pacific Supernet Limited
Lines: 194
Message-ID: <cmljt8$efo$1@nnews.pacific.net.hk>
NNTP-Posting-Host: 100.9.64.202.dyn.pacific.net.hk
X-Trace: nnews.pacific.net.hk 1099846377 14840 202.64.9.100 (7 Nov 2004 16:52:57 GMT)
X-Complaints-To: usenet@nnews.pacific.net.hk
NNTP-Posting-Date: Sun, 7 Nov 2004 16:52:57 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400