Source Code
/*************************************************************************************/
/* C program to implement RSA cryptography algorithm.*/
/* Download more programs at http://sourcecode4u.com/ */
/*************************************************************************************/
#include <stdio.h>
#include <string.h>
long power(long a, long b)
{
long r = 1, i;
for (i = 1; i <= b; i++)
r = r * a;
return r;
}
void main()
{
long p, q, n, z, d, e;
long i, j, count = 0, sum, k;
char s[25], ch[25], c[25];
long v[25], res;
printf("Enter 2 prime numbers:");
scanf("%d%d", &p, &q);
n = p * q;
z = (p - 1)*(q - 1);
printf("The value of n is %d", n);
printf("\nThe value of z is %d", z);
for (i = p; i <= q; i++)
{
count = 0;
for (j = 1; j <= z; j++)
{
if (i % j == 0 && z % j == 0)
count++;
}
if (count == 1)
{
d = i;
break;
}
}
printf("\nThe values of d is %d", d);
sum = 1+z;
if (sum % d == 0)
{
e = sum / d;
k = 1;
}
else
{
k = 2;
while ((sum *k) % d != 0)
{
k++;
}
e = (sum *k) / d;
}
printf("\nThe e value is %d", e);
printf("\nEnter the string in Capital::");
scanf("%s", &s);
for (i = 0; s[i] != '\0'; i++)
v[i] = (s[i]) - 64;
printf("The string is %s", s);
printf("\nThe string in numbers is\n");
for (j = 0; j < i; j++)
printf("%d\t", v[j]);
printf("\n");
for (j = 0; j < i; j++)
{
res = power(v[j], e);
c[j] = res % n;
}
printf("The Cipher text is\n");
for (j = 0; j < i; j++)
printf("%d\t", c[j]);
printf("\n");
for (i = 0; i < j; i++)
{
ch[i] = c[j] + 64;
}
for (j = 0; j < i; j++)
{
res = power(c[j], d);
v[j] = res % n;
}
printf("The Original text is\n");
for (j = 0; j < i; j++)
printf("%d\t", v[j]);
printf("\n");
}
Out Put