Sunday 17 March 2013

Implementing RSA Public Key Cryptographic Algorithm In Java:


import java.io.*;
import java.math.BigInteger;
import java.util.*;
class rsa
{
public static void main(String args[]) throws IOException
{
Random rng=new Random();
BigInteger p;
p=BigInteger.probablePrime(32,rng);
BigInteger q;
q=BigInteger.probablePrime(32,rng);
System.out.println("First prime number ::"+p);
System.out.println("Second prime number::"+q);
BigInteger z=new BigInteger("1");
BigInteger n=p.multiply(q);
BigInteger v=(p.subtract(z)).multiply(q.subtract(z));
BigInteger k=BigInteger.probablePrime(32,rng);
BigInteger d=k.modInverse(v);
System.out.println("\nEncryption KEY : " + k);
System.out.println("\nDecryption Key : " + d);
System.out.print("\nEnter Plain Text:: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Long nu=new Long(Long.parseLong(br.readLine()));
String num=nu.toString();
BigInteger m = new BigInteger(num);
System.out.println("\nEncrypted : "+m.modPow(k, n));
BigInteger l=m.modPow(k, n);
System.out.println("\nDecrypted : "+l.modPow(d, n));
}
}

Implementing Vernam Cipher Using Diffie-Hellman Key Exchange Protocol In Java:


import java.io.*;
import java.lang.Math.*;
public class vernamcipher
{
public static void main(String args[])throws IOException
{
String line,key,result1,result2;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter g:");
int g=Integer.parseInt(in.readLine());
System.out.println("Enter a:");
int a=Integer.parseInt(in.readLine());
System.out.println("Enter b:");
int b=Integer.parseInt(in.readLine());
System.out.println("Enter p:");
int p=Integer.parseInt(in.readLine());
int keyint=(int)(Math.pow(g,a*b))%p;
key=Integer.toString(keyint);
System.out.println("Key:");
System.out.println(key);
System.out.println("Enter the string:");
line=in.readLine();
System.out.println("Encrypted:");
result1="";
for(int i=0;i<line.length();i++)
{
result1+=(char)(line.charAt(i)^key.charAt(i%key.length()));
}
System.out.println(result1);
System.out.println("Decrypted:");
result2="";
for(int i=0;i<result1.length();i++)
{
result2+=(char)(result1.charAt(i)^key.charAt(i%key.length()));
}
System.out.println(result2);
}
}

Implementing Rail Fence Cipher Transposition In Java:


import java.io.*;
public class railfencecipher
{
public void encrypt(String line,int rail)
{
System.out.println("Result:");
int shift=0,p=0,itr;
for(int i=0;i<rail;i++)
{
p=i;
if(i==0||i==rail-1)
shift=((rail-2)*2)+2;
itr=1;
while(p<line.length())
{
System.out.print(line.charAt(p));
if(i!=0&&i!=rail-1)
{
shift=((rail*itr-itr)-p)*2;
}
p+=shift;
itr++;
}
}
}
public void decrypt(String line,int arr[])
{
System.out.println("Result:");
int ptr[]=new int[arr.length+1];
int p1=0,p2=0,p3=0,c=1;
boolean chk=true;
System.out.print(line.charAt(ptr[p3]+p1));
ptr[p3]++;
while(c<line.length())
{
if(chk)
{
p1+=arr[p2];
p2++;
p3++;
}
else
{
p1-=arr[p2];
p2--;
p3--;
}
System.out.print(line.charAt(ptr[p3]+p1));
c++;
ptr[p3]++;
if(p2==arr.length)
{
p2--;
chk=false;
}
else if(p2==-1)
{
p2++;
chk=true;
}
}
}
public static void main(String args[])throws IOException
{
railfencecipher obj=new railfencecipher();
String line;
int rail,arr[],temp;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("MENU:\n1) Encryption:\n2) Decryption:");
int choice=Integer.parseInt(in.readLine());
System.out.println("Enter the string:");
line=in.readLine();
System.out.println("Enter the no of rails:");
rail=Integer.parseInt(in.readLine());
switch(choice)
{
case 1:
temp=line.length()-rail;
int spaces;
if(temp%(rail-1)!=0)
{
spaces=(rail-1)-(temp%(rail-1));
if((temp/(rail-1))%2!=0)
{
spaces+=rail-1;
}
}
else
{
spaces=temp%(rail-1);
if((temp/(rail-1))%2==0)
{
spaces+=rail-1;
}
}
for(int g=0;g<spaces;g++)
line+=' ';
obj.encrypt(line,rail);
break;
case 2:
temp=line.length()-rail;
arr=new int[rail-1];
if((temp/(rail-1))%2==0)
arr[0]=1+(temp/(rail-1))/2;
else
arr[0]=1+((temp/(rail-1))+1)/2;
arr[1]=arr[0]*2-2;
for(int i=2;i<rail-1;i++)
arr[i]=arr[1];
obj.decrypt(line,arr);
break;
}
}
}

Implementing Columnar Cipher Transposition In Java:


import java.io.*;
public class columnar
{
char arr[][],encrypt[][],decrypt[][],keya[],keytemp[];
public void creatematrixE(String s,String key,int row,int column)
{
arr=new char[row][column];
int k=0;
keya=key.toCharArray();
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
if(k<s.length())
{
arr[i][j]=s.charAt(k);
k++;
}
else
{
arr[i][j]=' ';
}
}
}
}
public void createkey(String key,int column)
{
keytemp=key.toCharArray();
for(int i=0;i<column-1;i++)
{
for(int j=i+1;j<column;j++)
{
if(keytemp[i]>keytemp[j])
{
char temp=keytemp[i];
keytemp[i]=keytemp[j];
keytemp[j]=temp;
}
}
}
}
public void creatematrixD(String s,String key,int row,int column)
{
arr=new char[row][column];
int k=0;
keya=key.toCharArray();
for(int i=0;i<column;i++)
{
for(int j=0;j<row;j++)
{
if(k<s.length())
{
arr[j][i]=s.charAt(k);
k++;
}
else
{
arr[j][i]=' ';
}
}
}
}
public void encrypt(int row,int column)
{
encrypt=new char[row][column];
for(int i=0;i<column;i++)
{
for(int j=0;j<column;j++)
{
if(keya[i]==keytemp[j])
{
for(int k=0;k<row;k++)
{
encrypt[k][j]=arr[k][i];
}
keytemp[j]='?';
break;
}
}
}
}
public void decrypt(int row,int column)
{
decrypt=new char[row][column];
for(int i=0;i<column;i++)
{
for(int j=0;j<column;j++)
{
if(keya[j]==keytemp[i])
{
for(int k=0;k<row;k++)
{
decrypt[k][j]=arr[k][i];
}
keya[j]='?';
break;
}
}
}
}
public void resultE(int row,int column,char arr[][])
{
System.out.println("Result:");
for(int i=0;i<column;i++)
{
for(int j=0;j<row;j++)
{
System.out.print(arr[j][i]);
}
}
}
public void resultD(int row,int column,char arr[][])
{
System.out.println("Result:");
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
System.out.print(arr[i][j]);
}
}
}
public static void main(String args[])throws IOException
{
int row,column,choice;
columnar obj=new columnar();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Menu:\n1) Encryption\n2) Decryption");
choice=Integer.parseInt(in.readLine());
System.out.println("Enter the string:");
String s=in.readLine();
System.out.println("Enter the key:");
String key=in.readLine();
row=s.length()/key.length();
if(s.length()%key.length()!=0)
row++;
column=key.length();
switch(choice)
{
case 1: obj.creatematrixE(s,key,row,column);
obj.createkey(key,column);
obj.encrypt(row,column);
obj.resultE(row,column,obj.encrypt);
break;
case 2: obj.creatematrixD(s,key,row,column);
obj.createkey(key,column);
obj.decrypt(row,column);
obj.resultD(row,column,obj.decrypt);
break;
}
}
}

Implementing Hill Cipher Substitution In Java:


import java.io.*;
public class hillcipher
{
int keymatrix[][];
int linematrix[];
int resultmatrix[];
public void divide(String temp,int s)
{
while(temp.length()>s)
{
String sub=temp.substring(0,s);
temp=temp.substring(s,temp.length());
perform(sub);
}
if(temp.length()==s)
perform(temp);
else if(temp.length()<s)
{
for(int i=temp.length();i<s;i++)
temp=temp+'x';
perform(temp);
}
}
public void perform(String line)
{
linetomatrix(line);
linemultiplykey(line.length());
result(line.length());
}
public void keytomatrix(String key,int len)
{
keymatrix=new int[len][len];
int c=0;
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
keymatrix[i][j]=((int)key.charAt(c))-97;
c++;
}
}
}
public void linetomatrix(String line)
{
linematrix=new int[line.length()];
for(int i=0;i<line.length();i++)
{
linematrix[i]=((int)line.charAt(i))-97;
}
}
public void linemultiplykey(int len)
{
resultmatrix=new int[len];
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
resultmatrix[i]+=keymatrix[i][j]*linematrix[j];
}
resultmatrix[i]%=26;
}
}
public void result(int len)
{
String result="";
for(int i=0;i<len;i++)
{
result+=(char)(resultmatrix[i]+97);
}
System.out.print(result);
}
public boolean check(String key,int len)
{
keytomatrix(key,len);
int d=determinant(keymatrix,len);
d=d%26;
if(d==0)
{
System.out.println("Invalid key!!! Key is not invertible because determinant=0...");
return false;
}
else if(d%2==0||d%13==0)
{
System.out.println("Invalid key!!! Key is not invertible because determinant has common factor with 26...");
return false;
}
else
{
return true;
}
}
public int determinant(int A[][], int N){
int det=0;
int res;
if(N == 1)
res = A[0][0];
else if (N == 2){
res = A[0][0]*A[1][1] - A[1][0]*A[0][1];
}
else{
res=0;
for(int j1=0;j1<N;j1++){
int m[][]=new int[N-1][N-1];
for(int i=1;i<N;i++){
int j2=0;
for(int j=0;j<N;j++){
if(j==j1)
continue;
m[i-1][j2]=A[i][j];
j2++;
}
}
res+=Math.pow(-1.0,1.0+j1+1.0)* A[0][j1]*determinant(m,N-1);
}
}
return res;
}
public void cofact(int num[][],int f)
{
int b[][],fac[][];
b=new int[f][f];
fac=new int[f][f];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=(int)Math.pow(-1,q+p)*determinant(b,f-1);
}
}
trans(fac,f);
}
void trans(int fac[][],int r)
{
int i,j;
int b[][],inv[][];
b=new int[r][r];
inv=new int[r][r];
int d=determinant(keymatrix,r);
int mi=mi(d%26);
mi%=26;
if(mi<0)
mi+=26;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
inv[i][j]=b[i][j]%26;
if(inv[i][j]<0)
inv[i][j]+=26;
inv[i][j]*=mi;
inv[i][j]%=26;
}
}
System.out.println("\nInverse key:");
matrixtoinvkey(inv,r);
}
public int mi(int d)
{
int q,r1,r2,r,t1,t2,t;
r1=26;
r2=d;
t1=0;
t2=1;
while(r1!=1&&r2!=0)
{
q=r1/r2;
r=r1%r2;
t=t1-(t2*q);
r1=r2;
r2=r;
t1=t2;
t2=t;
}
return (t1+t2);
}
public void matrixtoinvkey(int inv[][],int n)
{
String invkey="";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
invkey+=(char)(inv[i][j]+97);
}
}
System.out.print(invkey);
}
public static void main(String args[])throws IOException
{
hillcipher obj=new hillcipher();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int choice;
System.out.println("Menu:\n1: Encryption\n2: Decryption");
choice=Integer.parseInt(in.readLine());
System.out.println("Enter the line: ");
String line=in.readLine();
System.out.println("Enter the key: ");
String key=in.readLine();
double sq=java.lang.Math.sqrt(key.length());
if(sq!=(long)sq)
System.out.println("Invalid key length!!! Does not form a square matrix...");
else
{
int s=(int)sq;
if(obj.check(key,s))
{
System.out.println("Result:");
obj.divide(line,s);
obj.cofact(obj.keymatrix,s);
}
}
}
}





Implementing Vigenere Cipher Poly-alphabetic Substitution In Java:



For only Alphabets (Capital and Small) :



import java.io.*;
public class vigenerecipher
{
public String encrypt(String keyword, String line)
{
String result="";
int offset;
int j=0,shift;
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)>='a'&&line.charAt(i)<='z')
{
shift=((int)keyword.charAt(j))-97;
j++;
j%=keyword.length();
offset=((int)(line.charAt(i)))-97;
offset=(offset+shift)%26;
result+=(char)(offset+97);
}
else if(line.charAt(i)>='A'&&line.charAt(i)<='Z')
{
shift=((int)keyword.charAt(j))-65;
j++;
j%=keyword.length();
offset=((int)(line.charAt(i)))-65;
offset=(offset+shift)%26;
result+=(char)(offset+65);
}
else
result=result+line.charAt(i);
}
return result;
}
public String decrypt(String keyword, String line)
{
String result="";
int offset;
int j=0,shift;
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)!=' ')
{
shift=((int)keyword.charAt(j))-97;
j++;
j%=keyword.length();
offset=((int)(line.charAt(i)))-97;
offset=(offset-shift)%26;
if(offset<0)
offset+=26;
result+=(char)(offset+97);
}
else
result=result+line.charAt(i);
}
return result;
}
public static void main(String args[])throws IOException
{
vigenerecipher obj=new vigenerecipher();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int choice;
System.out.println("Menu:\n1: Encryption\n2: Decryption");
choice=Integer.parseInt(in.readLine());
System.out.println("Enter the keyword: ");
String keyword=in.readLine();
System.out.println("Enter the line: ");
String line=in.readLine();
System.out.println("Result:");
switch(choice)
{
case 1:System.out.println(obj.encrypt(keyword,line));
break;
case 2:System.out.println(obj.decrypt(keyword,line));
break;
default:
System.out.println("Invalid input!");
break;
}
}
}


For all ASCII values from 0 to 255:



import java.io.*;
public class vigenerecipher
{
public String encrypt(String keyword, String line)
{
String result="";
int offset;
int j=0,shift;
for(int i=0;i<line.length();i++)
{
shift=((int)keyword.charAt(j))-97;
j++;
j%=keyword.length();
offset=((int)line.charAt(i)+shift)%256;
result+=(char)(offset);
}
return result;
}
public String decrypt(String keyword, String line)
{
String result="";
int offset;
int j=0,shift;
for(int i=0;i<line.length();i++)
{
shift=((int)keyword.charAt(j))-97;
j++;
j%=keyword.length();
offset=((int)line.charAt(i)-shift)%256;
if(offset<0)
offset+=256;
result+=(char)(offset);
}
return result;
}
public static void main(String args[])throws IOException
{
vigenerecipher obj=new vigenerecipher();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int choice;
System.out.println("Menu:\n1: Encryption\n2: Decryption");
choice=Integer.parseInt(in.readLine());
System.out.println("Enter the keyword: ");
String keyword=in.readLine();
System.out.println("Enter the line: ");
String line=in.readLine();
System.out.println("Result:");
switch(choice)
{
case 1:System.out.println(obj.encrypt(keyword,line));
break;
case 2:System.out.println(obj.decrypt(keyword,line));
break;
default:
System.out.println("Invalid input!");
break;
}
}
}


Implementing Caesar Cipher Mono-alphabetic Substitution In Java:


For only Alphabets (Capital and Small) :


import java.io.*;
public class caesarcipher
{
public String encrypt(int shift, String line)
{
String result="";
int offset;
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)>='a'&&line.charAt(i)<='z')
{
offset=((int)(line.charAt(i)))-97;
offset=(offset+shift)%26;
result+=(char)(offset+97);
}
else if(line.charAt(i)>='A'&&line.charAt(i)<='Z')
{
offset=((int)(line.charAt(i)))-65;
offset=(offset+shift)%26;
result+=(char)(offset+65);
}
else
result=result+line.charAt(i);
}
return result;
}
public String decrypt(int shift, String line)
{
String result="";
int offset;
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)!=' ')
{
offset=((int)(line.charAt(i)))-97;
offset=(offset-shift)%26;
if(offset<0)
offset+=26;
result+=(char)(offset+97);
}
else
result=result+line.charAt(i);
}
return result;
}
public static void main(String args[])throws IOException
{
caesarcipher obj=new caesarcipher();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int choice;
System.out.println("Menu:\n1: Encryption\n2: Decryption");
choice=Integer.parseInt(in.readLine());
System.out.println("Enter the shift: ");
int shift=Integer.parseInt(in.readLine());
System.out.println("Enter the line: ");
String line=in.readLine();
System.out.println("Result:");
switch(choice)
{
case 1:System.out.println(obj.encrypt(shift,line));
break;
case 2:System.out.println(obj.decrypt(shift,line));
break;
default:
System.out.println("Invalid input!");
break;
}
}
}

For all ASCII values from 0 to 255:


import java.io.*;
public class caesarcipher
{
public String encrypt(int shift, String line)
{
String result="";
int offset;
for(int i=0;i<line.length();i++)
{
offset=((int)line.charAt(i)+shift)%256;
result+=(char)(offset);
}
return result;
}
public String decrypt(int shift, String line)
{
String result="";
int offset;
for(int i=0;i<line.length();i++)
{
offset=((int)line.charAt(i)-shift)%256;
if(offset<0)
offset+=256;
result+=(char)(offset);
}
return result;
}
public static void main(String args[])throws IOException
{
caesarcipher obj=new caesarcipher();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int choice;
System.out.println("Menu:\n1: Encryption\n2: Decryption");
choice=Integer.parseInt(in.readLine());
System.out.println("Enter the shift: ");
int shift=Integer.parseInt(in.readLine());
System.out.println("Enter the line: ");
String line=in.readLine();
System.out.println("Result:");
switch(choice)
{
case 1:System.out.println(obj.encrypt(shift,line));
break;
case 2:System.out.println(obj.decrypt(shift,line));
break;
default:
System.out.println("Invalid input!");
break;
}
}
}