0% found this document useful (0 votes)
564 views8 pages

Program C++ RSA

This C program demonstrates the RSA asymmetric cryptographic algorithm using relatively small prime numbers for demonstration purposes. It includes functions for calculating the greatest common divisor, generating the public and private keys, encrypting a sample message using the public key, and decrypting it with the private key.

Uploaded by

Aulia Raffif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
564 views8 pages

Program C++ RSA

This C program demonstrates the RSA asymmetric cryptographic algorithm using relatively small prime numbers for demonstration purposes. It includes functions for calculating the greatest common divisor, generating the public and private keys, encrypting a sample message using the public key, and decrypting it with the private key.

Uploaded by

Aulia Raffif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Program RSA hadi

Program 1 RSA

// C program for RSA asymmetric cryptographic


// algorithm. For demonstration values are
// relatively small compared to practical
// application
#include<stdio.h>
#include<math.h>

// Returns gcd of a and b


int gcd(int a, int h)
{
int temp;
while (1)
{
temp = a%h;
if (temp == 0)
return h;
a = h;
h = temp;
}
}

// Code to demonstrate RSA algorithm


int main()
{
// Two random prime numbers
double p = 3;
double p = 3;
double q = 7;

// First part of public key:


double n = p*q;

// Finding other part of public key.


// e stands for encrypt
double e = 2;
double phi = (p-1)*(q-1);
while (e < phi)
{
// e must be co-prime to phi and
// smaller than phi.
if (gcd(e, phi)==1)
break;
else
e++;
}

// Private key (d stands for decrypt)


// choosing d such that it satisfies
// d*e = 1 + k * totient
int k = 2; // A constant value
double d = (1 + (k*phi))/e;

// Message to be encrypted
double msg = 20;

printf("Message data = %lf", msg);


// Encryption c = (msg ^ e) % n
double c = pow(msg, e);
c = fmod(c, n);
printf("\nEncrypted data = %lf", c);

// Decryption m = (c ^ d) % n
double m = pow(c, d);
m = fmod(m, n);
printf("\nOriginal Message Sent = %lf", m);

return 0;
}
// This code is contributed by Akash Sharan.

Program 2 RSA

#include<stdio.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);
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%ld",&p);
flag=prime(p);
if(flag==0)
{
printf("\nWRONG INPUT\n");
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%ld",&q);
flag=prime(q);
if(flag==0||p==q)
{
printf("\nWRONG INPUT\n");
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++)
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;
}

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]);
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=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;
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]);
}
Terakhir diubah: 17:36

You might also like