Crptography Miniproject
Crptography Miniproject
Crptography Miniproject
By K.Vamsi krishna(18BCN7125)
FACULTY : PROF BALU LAXMAN
PARNE SLOT : C1
without human intervention. Your program should produce possible plaintexts in rough
order of likelihood. It would be good if your user interface allowed the user to specify
CODE:
String s
="XLILSYWIMWRSAJSVWEPIJSVVQMPPMSRHSPPEVWMXMWASVXLQSVILYVV
CFIJSVIXLIWIPPIVVIGIMZIWQSVISJJIVW";
obj.main(s);
System.out.print(t);
System.out.println();
if(n < 0)
return(n+26)%26;
else
return n%26;
}
class Frequency
double[] frequencyDistribution;
char[] alpha;
int t = 0 ;
char c = s.charAt(i);
if(c == US[j])
found = true;
if(found)
continue;
else
US[t] = c ;
t++;
//finding frequency
{
for(int j = 0 ; j < s.length() ; j++)
if(s.charAt(j) == US[i])
numerical[i]++;
for(double x : numerical)
x = (x / s.length())*100;
bubbleSort(numerical , US);
frequencyDistribution = numerical;
alpha = US;
int n = arr.length;
arr[j] = arr[j+1];
arr[j+1] = temp;
char t = array[j];
array[j] = array[j+1];
array[j+1] = t;
OUTPUT:
2. Write a program that can perform a letter frequency attack on any monoalphabetic
substitution cipher without human intervention. Your program should produce possible
plaintexts in rough order of likelihood. It would be good if your user interface allowed
CODE:
String s
="BZSZYMQSKQNYIDYFQARSDYFFCKRRKYNHYRRAFQKBKQIYFBZCYFSZMFF
ELSDYFSBZSQSRRSFFSWSKXSQCYFSYDDSFQ";
int invkey;
if(found(key))
else
continue;
System.out.print(t);
}
System.out.println();
if(n < 0)
return(n+26)%26;
else
return n%26;
for(int x : key)
if(x == k)
return true;
return false;
int s1 = 1 ;
int s2 = 0 ;
int t1 = 0 ;
int t2 = 1 ;
int q = a/b;
int r = a%b ;
int t = 0 ;
while(r > 0)
a = b;
b = r; t = s1 ;
s1 = s2; s2 = t - q*s2; t = t1 ;
t1 = t2; t2 = t - q*t2;
r = a %b;
q = a /b ;
return t2;
class Frequency2
double[] frequencyDistribution;
char[] alpha;
int t = 0 ;
for(int i = 0 ; i < s.length() ; i++)
char c = s.charAt(i);
if(c == US[j])
found = true;
if(found)
continue;
else
US[t] = c ;
t++;
}
}
//finding frequency
if(s.charAt(j) == US[i])
numerical[i]++;
for(double x : numerical)
x = (x / s.length())*100;
bubbleSort(numerical , US);
frequencyDistribution = numerical;
alpha = US;
}
int n = arr.length;
arr[j] = arr[j+1];
arr[j+1] = temp;
char t = array[j];
array[j] = array[j+1];
array[j+1] = t;
}
}
OUTPUT:
3. Create software that can encrypt and decrypt using a 3 x 3 Hill cipher.
CODE:
import java.io.*;
import java.util.*;
int keymatrix[][];
int linematrix[];
int resultmatrix[];
public void divide(String temp, int s)
perform(sub);
if (temp.length() == s)
perform(temp);
perform(temp);
}
public void perform(String line)
linetomatrix(line);
linemultiplykey(line.length());
result(line.length());
int c = 0;
resultmatrix[i] %= 26;
}
System.out.print(result);
keytomatrix(key, len);
d = d % 26;
if (d == 0)
return false;
else if (d % 2 == 0 || d % 13 == 0)
else
return true;
int res;
if (N == 1)
res = A[0][0];
else if (N == 2)
}
else
res = 0;
int j2 = 0;
if (j == j1)
continue;
j2++;
}
res += Math.pow(-1.0, 1.0 + j1 + 1.0) * A[0][j1]* determinant(m, N - 1);
return res;
b = new int[f][f];
int p, q, m, n, i, j;
m = 0;
n = 0;
b[i][j] = 0;
if (i != q && j != p)
b[m][n] = num[i][j];
if (n < (f - 2))
n++;
else
n = 0;
m++;
}
}
trans(fac, f);
int i, j;
b = new int[r][r];
mi %= 26;
if (mi < 0)
mi += 26;
b[i][j] = fac[j][i];
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);
r1 = 26;
r2 = d;
t1 = 0;
t2 = 1;
q = r1 / r2;
r = r1 % r2;
t = t1 - (t2 * q);
r1 = r2;
r2 = r;
t1 = t2;
t2 = t;
{
invkey += (char) (inv[i][j] + 97);
System.out.print(invkey);
int choice;
choice = sc.nextInt();
else
if (obj.check(key, s))
System.out.println("Result:");
obj.divide(line, s);
obj.cofact(obj.keymatrix, s);
OUTPUT:
4. Create a software that can encrypt and decrypt message using Playfair Cipher with
specific key (You can consider any key of your choice). If you can implement for the
generalized key means user can input the key at run time then more weightage will be
CODE:
import java.util.*;
String KeyWord;
String Key;
String keyfill="";
if (k.charAt(i) == keyfill.charAt(j))
flag = true;
if (flag == false)
flag = false;
}
KeyWord = keyfill;
char current;
Key = KeyWord;
if (current == 'j')
continue;
if (current == KeyWord.charAt(j))
{
flag = false;
break;
if (flag)
flag = true;
System.out.println(Key);
matrix();
int counter = 0;
matrix_arr[i][j] = Key.charAt(counter);
counter++;
System.out.println();
int i;
String text="";
if (old_text.charAt(tmp) == 'j')
else
len = text.length();
for (i=0;i<len;i=i+2)
if (text.charAt(i+1) == text.charAt(i))
return text;
}
if (size % 2 != 0)
size++;
int counter = 0;
counter = counter + 2;
}
return x;
if (letter == 'j')
letter = 'i';
if (matrix_arr[i][j] == letter)
key[0] = i;
key[1] = j;
break;
return key;
char one;
char two;
{
one = src_arr[i].charAt(0);
two = src_arr[i].charAt(1);
part1 = GetDiminsions(one);
part2 = GetDiminsions(two);
if (part1[0] == part2[0])
if (part1[1] < 4)
part1[1]++;
else
part1[1] = 0;
if (part2[1] < 4)
part2[1]++;
else
part2[1] = 0;
{
if (part1[0] < 4)
part1[0]++;
else
part1[0] = 0;
if (part2[0] < 4)
part2[0]++;
else
part2[0] = 0;
else
part1[1] = part2[1];
part2[1] = temp;
+ matrix_arr[part2[0]][part2[1]];
}
return Code;
char one;
char two;
one = src_arr[i].charAt(0);
two = src_arr[i].charAt(1);
part1 = GetDiminsions(one);
part2 = GetDiminsions(two);
if (part1[0] == part2[0])
if (part1[1] > 0)
part1[1]--;
else
part1[1] = 4;
if (part2[1] > 0)
part2[1]--;
else
part2[1] = 4;
if (part1[0] > 0)
part1[0]--;
else
part1[0] = 4;
if (part2[0] > 0)
part2[0]--;
else
part2[0] = 4;
else
part1[1] = part2[1];
part2[1] = temp;
+ matrix_arr[part2[0]][part2[1]];
return Original;
}
public static void main(String[] args)
System.out.println("Enter a keyword:");
x.setKey(keyword);
x.KeyGen();
if (key_input.length() % 2 == 0)
else
key_input=key_input+t;
System.out.println("After Decryption :
"+x.decryptMessage(x.encryptMessage(key_input)));
OUTPUT:
5. Write a program to implement Vigenere Cipher. You have to write a method for both
CODE:
import java.util.*;
int x = str.length();
if (x ==
i){ i = 0;
key+=(key.charAt(i));
}
return key;
String ct="";
ct=ct+(char)(x);
return ct;
String original="";
x =x+ 'A';
original=original+(char)(x);
return original;
System.out.println("Ciphertext : "+encryption(str,key));
OUTPUT:
Algorithm.
CODE:
import java.io.*;
import java.util.*;
class Inverse
{
int r1,r2,r=0,q,t1=0,t2=1,t,n,inv;
r1=sc.nextInt();
r2=sc.nextInt();
n=r2;
r=r1%r2;
while(r>0)
q=r1/r2;
r=r1%r2;
t=t1-(q*t2);
r1=r2;
r2=r;
t1=t2;
t2=t;
inv=t1;
if(r1==1)
if(inv>0)
else
inv=inv+n;
else
output:
CODE:
import java.util.*;
class Poly_Multiplication
int m, int n)
{
prod[i] = 0;
}
}
return prod;
System.out.print(poly[i]);
if (i != 0)
System.out.print("x^" + i);
if (i != n - 1)
System.out.print(" + ");
}
System.out.println();
// polynomial 1 + 2x + 4x^2
int m = A.length;
int n = B.length;
printPoly(A, m);
printPoly(B, n);
printPoly(prod, m + n - 1);
OUTPUT:
8. AES
CODE:
) import java.util.*;
String[] KeyExpansion(String
"00110000";
String w3 = XOR(w2.toString(),w1.toString());
String w4 = XOR(XORResult3,NibbleResult2);
String w5 = XOR(w4,w3);
String key2 = w4+w5;
return keys;
for(int i =
0;i<a.length();i++){ if(a.cha
rAt(i)==b.charAt(i))
c+="0";
else
c+="1";
return c;
}
static String SubNibble(String
a){ String b =
RotNibble(a);
String SBox[][] =
{{"1001","0100","1010","1011"},{"1101","0001","1000","0101"},{"0110","0010","0000","0
011"},{"1100"
,"1110","1111","0111"}};
switch(leftBit.substring(0,2).toString()){
case "00":leftBitRow=0;
break;
case "01":leftBitRow=1;
break;
case "10":leftBitRow=2;
break;
case "11":leftBitRow=3;
switch(leftBit.substring(2).toString()){
case "00":leftBitCol=0;
break;
case "01":leftBitCol=1;
break;
case "10":leftBitCol=2;
break;
case "11":leftBitCol=3;
switch(rightBit.substring(0,2).toString()){
case "00":rightBitRow=0;
break;
case "01":rightBitRow=1;
break;
case "10":rightBitRow=2;
break;
case "11":rightBitRow=3;
switch(rightBit.substring(2).toString()){
case "00":rightBitCol=0;
break;
case "01":rightBitCol=1;
break;
case "10":rightBitCol=2;
break;
case "11":rightBitCol=3;
result.append(SBox[leftBitRow][leftBitCol]);
result.append(SBox[rightBitRow][rightBitCol]);
return result.toString();
}
b.append(a.substring(0,4));
return b.toString();
System.out.println(keys[0]+"\n"+keys[1]);
}
OUTPUT:
CODE:
import java.util.*;
static int z;
{ Scanner sc=new
Scanner(System.in); int j;
String s=sc.next();
for(j=0;j<s.length();j++)
k[j]=s.charAt(j);
for(z=0;z<10;z++)
ktemp=keyGen(k);
System.out.print(ktemp);
System.out.println();
k=ktemp;
int i;
for(i=0;i<8;i++)
w0[i]=k[i];
for(i=0;i<8;i++)
w1[i]=k[8+i];
for(i=0;i<8;i++)
{
w2[i]=k[16+i];
for(i=0;i<8;i++)
w3[i]=k[24+i];
for(i=0;i<8;i++)
w4[i]=w3[i];
char c1=w3[0];
char c2=w3[1];
w3[0]=w3[2];
w3[1]=w3[3];
w3[2]=w3[4];
w3[3]=w3[5];
w3[4]=w3[6];
w3[5]=w3[7];
w3[6]=c1;
w3[7]=c2;
w3=subWord(w3);
w3=xor(w3,rConst());
a=xor(w0,w3);
a1=xor(a,w1);
a2=xor(a1,w2);
a3=xor(a2,w4);
for(i=0;i<8;i++)
a4[i]=a[i];
for(i=0;i<8;i++)
a4[i+8]=a1[i];
for(i=0;i<8;i++)
{
a4[i+16]=a2[i];
for(i=0;i<8;i++)
a4[i+24]=a3[i];
return a4;
int l=k.length;
String s="";
for(int i=0;i<l/4;i++)
if(k[4*i]==0)
s=s+"0";
else
s=s+"1";
if(k[4*i+1]==0)
s=s+"0";
else
s=s+"1";
if(k[4*i+2]==0)
s=s+"0";
else
s=s+"1";
if(k[4*i+3]==0)
s=s+"0";
else
s=s+"1";
if(s.equals("0000"))
a[i]='0';
else if(s.equals("0001"))
a[i]='1';
else if(s.equals("0010"))
a[i]='2';
else if(s.equals("0011"))
a[i]='3';
else if(s.equals("0100"))
a[i]='4';
else if(s.equals("0101"))
a[i]='5';
else if(s.equals("0110"))
a[i]='6';
else if(s.equals("0111"))
a[i]='7';
else if(s.equals("1000"))
a[i]='8';
else if(s.equals("1001"))
a[i]='9';
else if(s.equals("1010"))
a[i]='A';
else if(s.equals("1011"))
a[i]='B';
else if(s.equals("1100"))
a[i]='C';
else if(s.equals("1101"))
a[i]='D';
else if(s.equals("1110"))
a[i]='E';
else
a[i]='F';
s="";
return(a);
}
static int[] hexaToBin(char k[])
int l=k.length;
int i=0,j;
char c;
for(i=0;i<l;i++)
c=k[i];
if(c=='0')
a[4*i]=0;
a[4*i+1]=0;
a[4*i+2]=0;
a[4*i+3]=0;
else if(c=='1')
{
a[4*i]=0;
a[4*i+1]=0;
a[4*i+2]=0;
a[4*i+3]=1;
else if(c=='2')
a[4*i]=0;
a[4*i+1]=0;
a[4*i+2]=1;
a[4*i+3]=0;
else if(c=='3')
a[4*i]=0;
a[4*i+1]=0;
a[4*i+2]=1;
a[4*i+3]=1;
else if(c=='4')
a[4*i]=0;
a[4*i+1]=1;
a[4*i+2]=0;
a[4*i+3]=0;
else if(c=='5')
a[4*i]=0;
a[4*i+1]=1;
a[4*i+2]=0;
a[4*i+3]=1;
}
else if(c=='6')
a[4*i]=0;
a[4*i+1]=1;
a[4*i+2]=1;
a[4*i+3]=0;
else if(c=='7')
a[4*i]=0;
a[4*i+1]=1;
a[4*i+2]=1;
a[4*i+3]=1;
else if(c=='8')
a[4*i]=1;
a[4*i+1]=0;
a[4*i+2]=0;
a[4*i+3]=0;
else if(c=='9')
a[4*i]=1;
a[4*i+1]=0;
a[4*i+2]=0;
a[4*i+3]=1;
else if(c=='A')
a[4*i]=1;
a[4*i+1]=0;
a[4*i+2]=1;
a[4*i+3]=0;
}
else if(c=='B')
a[4*i]=1;
a[4*i+1]=0;
a[4*i+2]=1;
a[4*i+3]=1;
else if(c=='C')
a[4*i]=1;
a[4*i+1]=1;
a[4*i+2]=0;
a[4*i+3]=0;
else if(c=='D')
{
a[4*i]=1;
a[4*i+1]=1;
a[4*i+2]=0;
a[4*i+3]=1;
else if(c=='E')
a[4*i]=1;
a[4*i+1]=1;
a[4*i+2]=1;
a[4*i+3]=0;
else
a[4*i]=1;
a[4*i+1]=1;
a[4*i+2]=1;
a[4*i+3]=1;
return(a);
int[] a1=hexaToBin(k1);
int[] a2=hexaToBin(k2);
for(int i=0;i<a1.length;i++)
if(a1[i]==0&&a2[i]==0)
a3[i]=0;
else if(a1[i]==1&&a2[i]==0)
a3[i]=1;
else if(a1[i]==0&&a2[i]==1)
a3[i]=1;
else
a3[i]=0;
char a[]=binToHexa(a3);
return a;
char[] a10={'3','6','0','0','0','0','0','0'};
if(z==0)
return a1;
else if(z==1)
return a2;
else if(z==2)
return a3;
else if(z==3)
return a4;
else if(z==4)
return a5;
else if(z==5)
return a6;
else if(z==6)
return a7;
else if(z==7)
return a8;
else if(z==8)
return a9;
else
return a10;
String b[][]=
{{"63","7C","77","7B","F2","6B","6F","C5","30","01","67","2B","FE","D7","AB","76"},
{"CA","82","C9","7D","FA","59","47","F0","AD","D4","A2","AF","9C","A4","72","C0"},
{"B7","FD","93","26","36","3F","F7","CC","34","A5","E5","F1","71","D8","31","15"},
{"04","C7","23","C3","18","96","05","9A","07","12","80","E2","EB","27","B2","75"},
{"09","83","2C","1A","1B","6E","5A","A0","52","3B","D6","B3","29","E3","2F","84"},
{"53","D1","00","ED","20","FC","B1","5B","6A","CB","BE","39","4A","4C","58","CF"},
{"D0","EF","AA","FB","43","4D","33","85","45","F9","02","7F","50","3C","9F","AB"},
{"51","A3","40","8F","92","9D","38","F5","BC","B6","DA","21","10","FF","F3","D2"},
{"CD","0C","13","EC","5F","97","44","17","C4","A7","7E","3D","64","5D","19","73"},
{"60","81","4F","DC","22","2A","90","88","46","EE","B8","14","DE","5E","0B","DB"},
{"E0","32","3A","0A","49","06","24","5C","C2","D3","AC","62","91","95","E4","79"},
{"E7","CB","37","6D","8D","D5","4E","A9","6C","56","F4","EA","65","7A","AE","08"},
{"BA","78","25","2E","1C","A6","B4","C6","E8","DD","74","1F","4B","BD","8B","8A"},
{"70","3E","B5","66","48","03","F6","0E","61","35","57","B9","86","C1","1D","9E"},
{"E1","F8","98","11","69","D9","8E","94","9B","1E","87","E9","CE","55","28","DF"},
{"8C","A1","89","0D","BF","E6","42","68","41","99","2D","0F","B0","54","BB","16"}};
int i,j,k1=0,k2=0;
for(i=0;i<a.length;i=i+2)
for(j=0;j<b1.length;j++)
if(c[i]==b1[j])
k1=j;
for(j=0;j<b1.length;j++)
if(c[i+1]==b1[j])
k2=j;
}
a[i]=b[k1][k2].charAt(0);
a[i+1]=b[k1][k2].charAt(1);
return a;
CODE:
import java.util.*;
1,0,0,0,
0,0,1,0,
0,1,1,1,
0,0,1,1,
0,1,1,0,
1,1,0,0,
1,1,0,0,
1,1,0,1,
1,1,0,1};
int[] p
={57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,63,55,
47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4};
a[i] = key[p[i]-1];
}
reducedkey = a;
for(int x : a)
System.out.print(x);
System.out.println();
left[i] = reducedkey[i];
right[i] = reducedkey[i+28];
if(n == 1 || n == 2 || n == 9 || n == 16)
{
int b = a[0];
a[i] = a[i+1];
a[a.length-1] = b ;
return a;
else
int b0 = a[0];
int b1 = a[1];
a[i] = a[i+2];
a[26] = b0;
a[27] = b1;
return a;
}
}
reducedkey[i] = left[i];
reducedkey[i+28] = right[i];
int[] p =
{14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30
,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};
a[i] = reducedkey[p[i]-1];
return a;
System.out.println();
System.out.print(a[i-1][j]);
System.out.println();
{
int[][] keyfinal = new int[16][] ;
paritydropper();
separator();
right = leftshifter(right,i);
left = leftshifter(left,i);
combine();
keyfinal[i-1] = compressor();
display(keyfinal,i);
11. DES
CODE:
import java.util.*;
StringBuilder(key0);
//Apply P10
for(int i =
0;i<p10.length;i++){ Key1.setCharAt(i,key
0.charAt((p10[i])-1));
//Apply LS-1
lf1.append(rightBit.charAt(0));
lf2.append(leftBit.charAt(0));
lf1.append(lf2);
//Apply P8
String key1 = "";
for(int i =
0;i<p8.length;i++){ key1+=Character.toString(lf
1.charAt((p8[i])-1));
//Apply LS-2
lf11.append(lf1.charAt(0));lf11.append(lf1.charAt(1));
lf12.append(lf1.charAt(5));lf12.append(lf1.charAt(6));
lf11.append(lf12);
//Apply P8
for(int i =
0;i<p8.length;i++){ key2+=Character.toString(lf1
1.charAt((p8[i])-1));
}
//Returning Keys
return keys;
//Apply Ip
for(int i = 0;i<ip.length;i++){
Ip.setCharAt(i,pText.charAt((ip[i])-1));
//Left-Right(L&R)
//Apply Ep
Fkey1RightExpanded.append(Fkey1Right.charAt((ExpBox[i])-1));
//XOR with K1
//S0 and S1
StringBuilder(xorResult.substring(0,4));
StringBuilder(xorResult.substring(4));
String SBoxResult =
SBox(xorResultLeft.toString(),xorResultRight.toString());
//Swap Halves
return grandFinalResult;
//Left-Right(L&R)
//Apply Ep
for(int i = 0;i<Ep.length;i++){
Fkey2RightExpanded.append(Fkey2Right.charAt((Ep[i])-1));
}
//XOR with K1
//S0 and S1
StringBuilder(xorResult.substring(0,4));
StringBuilder(xorResult.substring(4));
String SBoxResult =
SBox(xorResultLeft.toString(),xorResultRight.toString());
//Concat with R
//Ip Inverse
for(int i = 0;i<IpInverse.length;i++){
finalResult1+=Character.toString(finalResult0.charAt((IpInverse[i])-1));
return finalResult1;
{{"01","00","11","10"},{"11","10","01","00"},{"00","10","01","11"},{"11","01","11","10"}}
String s1[][] =
{{"00","01","10","11"},{"10","00","01","11"},{"11","00","01","00"},{"10","01","00","11"}}
switch(Character.toString(a.charAt(0))+Character.toString(a.charAt(3))){
case "00":SZeroRow=0;
break;
case "01":SZeroRow=1;
break;
case "10":SZeroRow=2;
break;
case "11":SZeroRow=3;
switch(Character.toString(a.charAt(1))+Character.toString(a.charAt(2))){
case "00":SZeroCol=0;
break;
case "01":SZeroCol=1;
break;
case "10":SZeroCol=2;
break;
case "11":SZeroCol=3;
switch(Character.toString(b.charAt(0))+Character.toString(b.charAt(3))){
case "00":SOneRow=0;
break;
case "01":SOneRow=1;
break;
case "10":SOneRow=2;
break;
case "11":SOneRow=3;
}
switch(Character.toString(b.charAt(1))+Character.toString(b.charAt(2))){
case "00":SOneCol=0;
break;
case "01":SOneCol=1;
break;
case "10":SOneCol=2;
break;
case "11":SOneCol=3;
SZero = s0[SZeroRow][SZeroCol];
SOne = s1[SOneRow][SOneCol];
//System.out.println(SZero+" "+SOne);
return result;
String b = "";
for(int i =
0;i<p4.length;i++){ b+=Character.toString(
a.charAt((p4[i])-1));
return b;
c.append(b.substring(0,4));
return c.toString();
for(int i =
0;i<a.length();i++){ if(a.cha
rAt(i)==b.charAt(i))
c+="0";
else
c+="1";
return c;
OUTPUT: