Question

C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt...

C Programming - RSA Encryption

I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so

Can someone please help me modify the following the code? I want to be able to just pass it in a string to encrypt and want the values of x and y (the prime numbers) to be the same. Eg 3 and 7. Basically making the RSA code simpler. Please don't change or add to the used libraries.

#include

#include

#include

#include

int x, y, n, t, i, flag;

long int e[50], d[50], temp[50], j, m[50], en[50];

char msg[100];

int prime(long int);

void encryption_key();

long int cd(long int);

void encrypt();

void decrypt();

int main()

{

printf("\nENTER FIRST PRIME NUMBER\n");

scanf("%d", &x);

flag = prime(x);

if (flag == 0)

{

printf("\nINVALID INPUT\n");

exit(0);

}

printf("\nENTER SECOND PRIME NUMBER\n");

scanf("%d", &y);

flag = prime(y);

if (flag == 0 || x == y)

{

printf("\nINVALID INPUT\n");

exit(0);

}

printf("\nENTER MESSAGE OR STRING TO ENCRYPT\n");

scanf("%s", msg);

for (i = 0; msg[i] != '\0'; i++)

m[i] = msg[i];

n = x * y;

t = (x - 1) * (y - 1);

encryption_key();

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();

return 0;

}

int prime(long int pr)

{

int i;

j = sqrt(pr);

for (i = 2; i <= j; i++)

{

if (pr % i == 0)

return 0;

}

return 1;

}

//function to generate encryption key

void encryption_key()

{

int k;

k = 0;

for (i = 2; i < t; i++)

{

if (t % i == 0)

continue;

flag = prime(i);

if (flag == 1 && i != x && i != y)

{

e[k] = i;

flag = cd(e[k]);

if (flag > 0)

{

d[k] = flag;

k++;

}

if (k == 99)

break;

}

}

}

long int cd(long int a)

{

long int k = 1;

while (1)

{

k = k + t;

if (k % a == 0)

return (k / a);

}

}

//function to encrypt the message

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("\n\nTHE ENCRYPTED MESSAGE IS\n");

for (i = 0; en[i] != -1; i++)

printf("%c", en[i]);

}

//function to decrypt the message

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("\n\nTHE DECRYPTED MESSAGE IS\n");

for (i = 0; m[i] != -1; i++)

printf("%c", m[i]);

printf("\n");

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

assuming you only want me to make this code such that you only have to input string and rest program will do itself
code:


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int x, y, n, t, i, flag;

long int e[50], d[50], temp[50], j, m[50], en[50];

char msg[100];

int prime(long int);

void encryption_key();

long int cd(long int);

void encrypt();

void decrypt();

int main()
{
x = 3;
flag = prime(x);
y = 7;
flag = prime(y);
printf("\nENTER MESSAGE OR STRING TO ENCRYPT\n");
scanf("%s", msg);
for (i = 0; msg[i] != '\0'; i++)
m[i] = msg[i];
n = x * y;
t = (x - 1) * (y - 1);
encryption_key();
encrypt();
decrypt();
return 0;
}

int prime(long int pr)
{
int i;
j = sqrt(pr);
for (i = 2; i <= j; i++)
{

if (pr % i == 0)

return 0;
}
return 1;
}

//function to generate encryption key

void encryption_key()

{
int k;
k = 0;
for (i = 2; i < t; i++)
{

if (t % i == 0)

continue;

flag = prime(i);

if (flag == 1 && i != x && i != y)

{

e[k] = i;

flag = cd(e[k]);

if (flag > 0)

{

d[k] = flag;

k++;
}

if (k == 99)

break;
}
}
}

long int cd(long int a)

{
long int k = 1;
while (1)
{

k = k + t;

if (k % a == 0)

return (k / a);
}
}

//function to encrypt the message

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]);
}

//function to decrypt the message

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("\n\nTHE DECRYPTED MESSAGE IS\n");
for (i = 0; m[i] != -1; i++)

printf("%c", m[i]);
printf("\n");
}

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT