0% found this document useful (0 votes)
1K views

C Code To Implement RSA Algorithm

This C code implements the RSA encryption and decryption algorithm. It takes in two prime numbers as inputs to generate the public and private keys. It then uses these keys to encrypt a message input by the user and decrypt the encrypted message back to the original plaintext. The key steps are: 1. Generate two prime numbers p and q. 2. Calculate n as p * q and phi as (p-1) * (q-1). 3. Choose a public key e such that e and phi are coprime. 4. Calculate private key d which satisfies ed = 1 mod phi. 5. Encrypt the message by computing ciphertext as message^e mod n. 6. Dec

Uploaded by

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

C Code To Implement RSA Algorithm

This C code implements the RSA encryption and decryption algorithm. It takes in two prime numbers as inputs to generate the public and private keys. It then uses these keys to encrypt a message input by the user and decrypt the encrypted message back to the original plaintext. The key steps are: 1. Generate two prime numbers p and q. 2. Calculate n as p * q and phi as (p-1) * (q-1). 3. Choose a public key e such that e and phi are coprime. 4. Calculate private key d which satisfies ed = 1 mod phi. 5. Encrypt the message by computing ciphertext as message^e mod n. 6. Dec

Uploaded by

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

C code to implement RSA Algorithm(Encryption and

Decryption)
C program to implement RSA algorithm. The given program will Encrypt and Decrypt a message using
RSA Algorithm.
#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",&p);
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]);
}

Output ff the above program:-

RSA Inputs

RSA Result

import java.math.BigInteger ;
import java.util.Random ;
import java.io.* ;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;

public class RSA


{
/**
* Bit length of each prime number.
*/
int primeSize ;
/**
* Two distinct large prime numbers p and q.
*/
BigInteger p, q ;
/**
* Modulus N.
*/
BigInteger N ;
/**
*r=(p1)*(q1)
*/
BigInteger r ;
/**
* Public exponent E and Private exponent D
*/
BigInteger E, D ;
String nt,dt,et;
/**
* Constructor.
*
* @param primeSize Bit length of each prime number.
*/

String publicKey;
String privateKey;
String randomNumber;
BigInteger[] ciphertext;
int m[] = new int[1000];
String st[] = new String[1000];
String str = "";
String sarray1[] = new String[100000];
StringBuffer sb1 = new StringBuffer();
String inputMessage,encryptedData,decryptedMessage;
public RSA( int primeSize )
{
this.primeSize = primeSize ;
// Generate two distinct large prime numbers p and q.
generatePrimeNumbers() ;
// Generate Public and Private Keys.
generatePublicPrivateKeys() ;
BigInteger publicKeyB = getE();
BigInteger privateKeyB = getD();
BigInteger randomNumberB = getN();
publicKey = publicKeyB.toString();
privateKey = privateKeyB.toString();
randomNumber = randomNumberB.toString();
System.out.println("Public Key (E,N): "+publicKey+","+randomNumber);
System.out.println("Private Key (D,N): "+privateKey+","+randomNumber);
//Encrypt data
inputMessage=JOptionPane.showInputDialog(null,"Enter message to encrypt");

encryptedData=RSAencrypt(inputMessage);
System.out.println("Encrypted message"+encryptedData);
JOptionPane.showMessageDialog(null,"Encrypted Data "+"\n"+encryptedData);
//Decrypt data
decryptedMessage=RSAdecrypt();
JOptionPane.showMessageDialog(null,"Decrypted Data "+"\n"+decryptedMessage);
}
/**
* Generate two distinct large prime numbers p and q.
*/
public void generatePrimeNumbers()
{
p = new BigInteger( primeSize, 10, new Random() ) ;
do
{
q = new BigInteger( primeSize, 10, new Random() ) ;
}
while( q.compareTo( p ) == 0 ) ;
}
/**
* Generate Public and Private Keys.
*/
public void generatePublicPrivateKeys()
{
// N = p * q
N = p.multiply( q ) ;

// r = ( p 1 ) * ( q 1 )
r = p.subtract( BigInteger.valueOf( 1 ) ) ;
r = r.multiply( q.subtract( BigInteger.valueOf( 1 ) ) ) ; //(p-1)(q-1)
// Choose E, coprime to and less than r
do
{
E = new BigInteger( 2 * primeSize, new Random() ) ;
}
while( ( E.compareTo( r ) != -1 ) || ( E.gcd( r ).compareTo( BigInteger.valueOf( 1 ) ) != 0 ) ) ;
// Compute D, the inverse of E mod r
D = E.modInverse( r ) ;
}
/**
* Get prime number p.
*
* @return Prime number p.
*/
public BigInteger getp()
{
return( p ) ;
}
/**
* Get prime number q.
*
* @return Prime number q.
*/
public BigInteger getq()
{

return( q ) ;
}
/**
* Get r.
*
* @return r.
*/
public BigInteger getr()
{
return( r ) ;
}
/**
* Get modulus N.
*
* @return Modulus N.
*/
public BigInteger getN()
{
return( N ) ;
}
/**
* Get Public exponent E.
*
* @return Public exponent E.
*/
public BigInteger getE()
{
return( E ) ;
}

/**
* Get Private exponent D.
*
* @return Private exponent D.
*/
public BigInteger getD()
{
return( D ) ;
}
/** Encryption */
public String RSAencrypt(String info) {
E = new BigInteger(publicKey);
N = new BigInteger(randomNumber);
try {
ciphertext = encrypt( info ) ;
for( int i = 0 ; i < ciphertext.length ; i++ )
{
m[i] = ciphertext[i].intValue();
st[i] = String.valueOf(m[i]);
sb1.append(st[i]);
sb1.append(" ");
str = sb1.toString();
}
}
catch (Exception e) {
System.out.println(e);
}
return str;
}

public BigInteger[] encrypt( String message )


{
int i ;
byte[] temp = new byte[1] ;
byte[] digits = new byte[8];
try {
digits = message.getBytes() ;
String ds = new String(digits);
System.out.println("ds="+ds);
}
catch (Exception e) {
System.out.println(e);
}
BigInteger[] bigdigits = new BigInteger[digits.length] ;
for( i = 0 ; i < bigdigits.length ; i++ )
{
temp[0] = digits[i] ;
bigdigits[i] = new BigInteger( temp ) ;
}
BigInteger[] encrypted = new BigInteger[bigdigits.length] ;
for( i = 0 ; i < bigdigits.length ; i++ )
encrypted[i] = bigdigits[i].modPow( E, N ) ;
return( encrypted ) ;
}
/** Decrption */
public String RSAdecrypt() {
D = new BigInteger(privateKey);
N = new BigInteger(randomNumber);

System.out.println("D = " + D);


System.out.println("N = " + N);
int k1= 0;
StringTokenizer st = new StringTokenizer(encryptedData);
while (st.hasMoreTokens()) {
sarray1[k1] = st.nextToken(" ");
k1++;
}
BigInteger[] ciphertext1 = new BigInteger[100000];
for( int i = 0 ; i ciphertext1[i] = new BigInteger(sarray1[i]);
}
String recoveredPlaintext = decrypt( ciphertext1,D,N,k1) ;
System.out.println(recoveredPlaintext);
return recoveredPlaintext;
}
public String decrypt( BigInteger[] encrypted,BigInteger D,BigInteger N,int size )
{
int i ;
String rs="";
BigInteger[] decrypted = new BigInteger[size] ;
for( i = 0 ; i < decrypted.length ; i++ ) {
decrypted[i] = encrypted[i].modPow( D, N ) ;
}
char[] charArray = new char[decrypted.length] ;
byte[] byteArray = new byte[decrypted.length] ;
for( i = 0 ; i < charArray.length ; i++ ) {
charArray[i] = (char) ( decrypted[i].intValue() ) ;
Integer iv = new Integer(0);
iv=decrypted[i].intValue() ;
byteArray[i] = iv.byteValue();
}

try {
rs=new String( byteArray );
}
catch (Exception e) {
System.out.println(e);
}
return(rs) ;
}
/**
* KeyGeneration Main program for Unit Testing.
*/
public static void main( String[] args ) throws IOException
{
RSA akg = new RSA(8);
}
}

You might also like