Click here to Skip to main content
15,896,344 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As I am using RSA for encryption and decryption in C language but in this case its working fine but the text given to encrypt its only encrypting the starting word not the full sentence which is given by user to encode.
EXAMPLE: Ram is a good boy
Encoded text: Rhq
Decoded text: Ram
unable to sort out the problem please help. The sample code is given below:

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

long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
void main()
{
clrscr();
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(p);
if(flag==0)
{
    printf("\nWRONG INPUT\n");
    getch();
    exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q)
{
    printf("\nWRONG INPUT\n");
    getch();
    exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for(i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for(i=0;i<j-1;i++)>
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
getch();
}
int prime(long int pr)
{
int i;
j=sqrt(pr);
for(i=2;i<=j;i++)
{
    if(pr%i==0)
    return 0;
}
return 1;
}
void ce()
{
int k;
k=0;
for(i=2;i<t;i++)>
{
    if(t%i==0)
    continue;
    flag=prime(i);
    if(flag==1&&i!=p&&i!=q)
    {
        e[k]=i;
        flag=cd(e[k]);
        if(flag>0)
        {
            d[k]=flag;
            k++;
        }
        if(k==99)
        break;
    }
}
}
long int cd(long int x)
{
long int k=1;
while(1)
{
    k=k+t;
    if(k%x==0)
    return(k/x);
}
}
void encrypt()
{
long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);
while(i!=len)
{
    pt=m[i];
    pt=pt-96;
    k=1;
    for(j=0;j<key;j++)>
    {
        k=k*pt;
        k=k%n;
    }
    temp[i]=k;
    ct=k+96;
    en[i]=ct;
    i++;
}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for(i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}
void decrypt()
{
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1)
{
    ct=temp[i];
    k=1;
    for(j=0;j<key;j++)>
    {
        k=k*ct;
        k=k%n;
    }
    pt=k+96;
    m[i]=pt;
    i++;
}
m[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for(i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}

Please help me out. Thank in advance...
Posted
Updated 29-Apr-14 11:34am
v3
Comments
Sergey Alexandrovich Kryukov 29-Apr-14 17:13pm    
Encryption is not encoding, encoding is not encryption. So far, you even failed to HTML-encode your "#include" statements, so we cannot see what's included.
Please explain the problem properly and provide good and well-formatted (use "pre lang="c++" tags) code sample.
—SA
Richard MacCutchan 30-Apr-14 3:01am    
scanf stops at the first whitespace character, so you only encode the first word of input. You should also use meaningful variable names in your code, and add spacing and indentation for readability. As it stands it's almost impossible to read the above.

1 solution

don't use scanf , you can correct like this :
printf("\nENTER MESSAGE\n");
fflush(stdin);
fgets( msg, sizeof(msg),stdin);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900