· 7 years ago · Oct 04, 2018, 12:48 AM
1PRACTICAL NO.1
2 IMPLEMENTING SUBSTITUTION CIPHER
3 A] CEASER CIPHER
4import java.io.*;
5import java.util.*;
6class CaesarCipherdemo
7{
8private final String ALPHABET="abcdefghijklmnopqrstuvwxyz";
9public String encrypt(String plainText,int shiftkey)
10{
11plainText=plainText.toLowerCase();
12String cipherText="";
13for(int i=0;i<plainText.length();i++)
14{
15int charPosition=ALPHABET.indexOf(plainText.charAt(i));
16//System.out.println("charAt"+i);
17int keyval=(shiftkey+charPosition)%26;
18//System.out.println(keyval"+keyval);
19char replaceval=this.ALPHABET.charAt(keyval);
20cipherText +=replaceval;
21}
22return cipherText;
23}
24public String decrypt(String cipherText, int shiftkey)
25{
26cipherText=cipherText.toLowerCase();
27String plainText="";
28 for (int i=0;i<cipherText.length();i++)
29{
30int charposition=this.ALPHABET.indexOf(cipherText.charAt(i));
31//System.out.println("charAt"+i);
32int keyval=(charposition-shiftkey)%26;
33//System.out.println(keyval"+keyval);
34if (keyval<0)
35{
36keyval=this.ALPHABET.length()+keyval;
37}
38char replaceval=this.ALPHABET.charAt(keyval);
39plainText +=replaceval;
40}
41return plainText;
42}
43}
44class CaesarCipher
45{
46public static void main (String args[])
47{
48String plainText="studentitzone";
49int shiftkey=4;
50CaesarCipherdemo cc =new CaesarCipherdemo();
51String CipherText=cc.encrypt(plainText,shiftkey);
52System.out.println("your plaintext::::"+ plainText);
53System.out.println("your Ciphertext::::"+ CipherText);
54String CplainText=cc.decrypt(CipherText,shiftkey);
55System.out.println("your Cplaintext::::"+ CplainText);
56}
57}
58
59 B] MODIFIED CEASER CIPHER
60import java.io.*;
61import java.util.*;
62public class caesarcipherm
63{
64public String encrypt(int shift, String line)
65{
66String result="";
67int offset;
68for(int i=0;i<line.length();i++)
69{
70offset=((int)line.charAt(i)+shift)%256;
71result+=(char)(offset);
72}
73return result;
74}
75public String decrypt(int shift, String line)
76{
77String result="";
78int offset;
79for(int i=0;i<line.length();i++)
80{
81offset=((int)line.charAt(i)-shift)%256;
82if(offset<0);
83offset+=256;
84result+=(char)(offset);
85}
86return result;
87}
88public static void main(String args[])throws IOException
89{
90caesarcipherm obj=new caesarcipherm();
91BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
92int choice;
93System.out.println("Menu:\n1:Encryption\n2:Decryption");
94choice=Integer.parseInt(in.readLine());
95System.out.println("enter the shift:");
96int shift=Integer.parseInt(in.readLine());
97System.out.println("enter the line:");
98String line=in.readLine();
99System.out.println("result:");
100switch(choice)
101{
102case 1:System.out.println(obj.encrypt(shift,line));
103break;
104case 2:System.out.println(obj.decrypt(shift,line));
105break;
106default:
107System.out.println("invalid input");
108break;
109}
110}
111}
112
113C] MONO-ALPHABETIC
114import java.util.*;
115import java.net.*;
116import java.io.*;
117class Monoalphabeticsubstitutioncipher
118{
119public char p[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
120public char
121ch[]={'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'
122};
123public String doEncryption(String s)
124{
125char c[]=new char[(s.length())];
126for(int i=0;i<s.length();i++)
127{
128for(int j=0;j<26;j++)
129{
130if(p[j]==s.charAt(i))
131{
132c[i]=ch[j];
133break;
134}
135}
136System.out.println(s.charAt(i)+" "+c[i]);
137}
138System.out.println("Cipher Text: "+c);
139return(new String(c));
140}
141public String doDecryption(String s)
142{
143System.out.println(s+" Len Of s:"+s.length());
144char p1[]=new char[(s.length())];
145for(int i=0;i<s.length();i++)
146{
147for(int j=0;j<26;j++)
148{
149if(ch[j]==s.charAt(i))
150{
151p1[i]=p[j];
152break;
153}
154}
155}
156System.out.println("Plain Text: "+new String(p1));
157return(new String(p1));
158}
159}
160public class receiver{
161public static void main(String args[])throws Exception{
162String myString="LittleMaster";
163Monoalphabeticsubstitutioncipher obj=new Monoalphabeticsubstitutioncipher();
164String encryptedString=obj.doEncryption(myString);
165System.out.println("\nEncrypted String: "+encryptedString);
166String decryptedString=obj.doDecryption(encryptedString);
167System.out.println("\nDecrypted String: "+decryptedString);
168}
169}
170
171D] POLY-ALPHABETIC
172import java.util.*;
173public class PolyCipher
174{
175public static void main(String args[])
176{
177int[]j = new int[100];
178int[]s= new int[100];
179String test="";
180try
181{
182Scanner in=new Scanner(System.in);
183System.out.println("Enter the plain text(STRING SHOULD BE IN UPPERCASE AND
184DONT GIVE SPACE BETWEEN WORDS)::");
185test=in.nextLine();
186for(int i=0;i<test.length();i++)
187{
188char c=test.charAt(i);
189s[i]=(int)c-65;
190}
191for(int i=0;i<test.length()-1;i++)
192{
193j[i+1]=s[i];
194}
195System.out.println("enter the key::");
196int k=Integer.parseInt(in.nextLine());
197j[0]=k;
198System.out.println();
199System.out.println("the position of the character in the cipher test::");
200for(int i=0;i<test.length();i++)
201{
202j[i]=j[i]+s[i];
203j[i]=j[i]%26;
204System.out.print(j[i]);
205}
206System.out.println();
207System.out.println("the cipher text::");
208for(int i=0;i<test.length();i++)
209{
210char c=(char) (j[i]+65);
211System.out.print(c);
212}
213System.out.println();
214}
215catch(Exception er)
216{
217System.out.println("--YOU HAVE TYPE INVALID DATA--");
218}
219}
220}
221
222
223PRACTICAL NO.2
224IMPLEMENTING TRANSPOSITION CIPHER
225 A] RAILFENCE TRANSPOSITION CIPHER
226public class railfence
227{
228public static void main (String srgs[])
229{
230String input="inputstring";
231String output="";
232int len=input.length(),flag=0;
233System.out.println("Input String;"+input);
234for(int i=0;i<len;i+=2)
235{
236output += input.charAt(i);
237}
238for(int i=1;i<len;i+=2)
239{
240output += input.charAt(i);
241}
242System.out.println("Ciphered Text:"+output);
243}
244}
245
246B] SIMPLE COLUMNAR
247import java.awt.event.*;
248import java.util.*;
249public class transpositionCipher
250{
251public static void main(String args[])
252{
253String key;
254String message;
255String encryptedMessage;
256int x=0;
257int y=0;
258key="tape";
259message="CRYPTOGRAPHYDEMO";
260encryptedMessage="";
261char temp[][]=new char [key.length()][message.length()];
262char msg[]=message.toCharArray();
263x=0;
264y=0;
265for(int i=0;i<msg.length;i++)
266{
267temp[x][y]=msg[i];
268if(x==(key.length()-1))
269{
270x=0;
271y=y+1;
272}
273else
274{
275x++;
276}
277}
278char t[]=new char [key.length()];
279t=key.toCharArray();
280Arrays.sort(t);
281for(int j=0;j<y;j++)
282{
283for(int i=0;i<key.length();i++)
284{
285System.out.println(temp[i][j]);
286}
287System.out.println();
288}
289System.out.println();
290for(int j=0;j<y;j++)
291{
292for(int i=0;i<key.length();i++)
293{
294int pos=0;
295for(pos=0;pos<t.length;pos++)
296{
297if (key.charAt(i)==t[pos])
298{
299break;
300}
301}
302System.out.print(temp[pos][j]);
303encryptedMessage+=temp[pos][j];
304}
305System.out.println();
306}
307System.out.println(encryptedMessage);
308System.exit(0);
309}
310}
311
312C] VERNAM CIPHER
313import java.lang.Math;
314public class xor
315{
316public static void main(String args[])
317{
318String text=new String("hello");
319char[] arText=text.toCharArray();
320String cipher=new String("XYZHG");
321char[] arCipher=cipher.toCharArray();
322char[] encoded=new char[5];
323System.out.println("Encoded"+text+"to be..");
324for(int i=0;i<arText.length;i++)
325{
326encoded[i]=(char) (arText[i]^arCipher[i]);
327System.out.print(encoded[i]);
328}
329System.out.println("\n Decoded to be...");
330for(int i=0;i<encoded.length;i++)
331{
332char temp=(char) (encoded[i] ^ arCipher[i]);
333System.out.print(temp);
334}
335}
336}
337
338
339PRACTICAL NO.3
340 A] IMPLEMENTING DIFFIE-HELLMAN KEY EXCHANGE
341ALGORITHM
342import java.util.*;
343import java.math.BigInteger;
344public class DiffieHellman{
345final static BigInteger one=new BigInteger("1");
346public static void main(String args[]){
347Scanner stdin=new Scanner(System.in);
348BigInteger n;
349System.out.println("Enter the first prime no:");
350String ans=stdin.next();
351n=getNextPrime(ans);
352System.out.println("First prime is:"+n+".");
353System.out.println("Enter the second prime no(between 2 and n-1):");
354BigInteger g=new BigInteger(stdin.next());
355System.out.println("Person A:enter your secret number now.i.e any random no(x)");
356BigInteger a=new BigInteger(stdin.next());
357BigInteger resulta=g.modPow(a,n);
358System.out.println("Person A sends"+ resulta +"to person B.");
359System.out.println("Person B:enter your secter number now.i.e any random no(y)");
360BigInteger b=new BigInteger(stdin.next());
361BigInteger resultb=g.modPow(b,n);
362System.out.println("Person B sends"+resultb+"to person A.");
363BigInteger KeyACalculates=resultb.modPow(a,n);
364BigInteger KeyBCalculates=resulta.modPow(b,n);
365System.out.println("A takes"+ resultb +"raises it to the power"+ a +"mod"+n);
366System.out.println("The Key A calculates is"+ KeyACalculates +".");
367System.out.println("B takes"+ resulta +"raises it to the power"+ b +"mod"+n);
368System.out.println("The Key B calculates is"+ KeyBCalculates +".");
369}
370public static BigInteger getNextPrime(String ans){
371BigInteger test=new BigInteger(ans);
372while(!test.isProbablePrime(99))
373test=test.add(one);
374return test;
375}
376}
377
378PRACTICAL NO.3
379 A] IMPLEMENTING DIFFIE-HELLMAN KEY EXCHANGE
380ALGORITHM
381import java.util.*;
382import java.math.BigInteger;
383public class DiffieHellman{
384final static BigInteger one=new BigInteger("1");
385public static void main(String args[]){
386Scanner stdin=new Scanner(System.in);
387BigInteger n;
388System.out.println("Enter the first prime no:");
389String ans=stdin.next();
390n=getNextPrime(ans);
391System.out.println("First prime is:"+n+".");
392System.out.println("Enter the second prime no(between 2 and n-1):");
393BigInteger g=new BigInteger(stdin.next());
394System.out.println("Person A:enter your secret number now.i.e any random no(x)");
395BigInteger a=new BigInteger(stdin.next());
396BigInteger resulta=g.modPow(a,n);
397System.out.println("Person A sends"+ resulta +"to person B.");
398System.out.println("Person B:enter your secter number now.i.e any random no(y)");
399BigInteger b=new BigInteger(stdin.next());
400BigInteger resultb=g.modPow(b,n);
401System.out.println("Person B sends"+resultb+"to person A.");
402BigInteger KeyACalculates=resultb.modPow(a,n);
403BigInteger KeyBCalculates=resulta.modPow(b,n);
404System.out.println("A takes"+ resultb +"raises it to the power"+ a +"mod"+n);
405System.out.println("The Key A calculates is"+ KeyACalculates +".");
406System.out.println("B takes"+ resulta +"raises it to the power"+ b +"mod"+n);
407System.out.println("The Key B calculates is"+ KeyBCalculates +".");
408}
409public static BigInteger getNextPrime(String ans){
410BigInteger test=new BigInteger(ans);
411while(!test.isProbablePrime(99))
412test=test.add(one);
413return test;
414}
415}
416
417B] DIFFIE-HELLMAN KEY EXCHANCE ALGORITHM
418import java.io.*;
419import java.lang.*;
420import java.util.*;
421class Dhalgorithm{
422public static void main(String args[]){
423double n,g,x,y;
424Scanner sc=new Scanner(System.in);
425System.out.println("enter two prime numbers");
426n=sc.nextInt();
427g=sc.nextInt();
428System.out.println("Prime Number"+n+""+g+"\n");
429System.out.println("enter any random number");
430x=sc.nextInt();
431y=sc.nextInt();
432System.out.println(" random number"+x+""+y);
433double A=(Math.pow(g,x)%n);
434System.out.println("Alias send"+A+"Bob");
435double B=(Math.pow(g,y)%n);
436System.out.println("Bob send"+B+"Alias");
437double k1=(Math.pow(B,x)%n);
438System.out.println(k1);
439double k2=(Math.pow(A,y)%n);
440System.out.println(k2);
441}
442}
443
444PRACTICAL NO.4
445IMPLEMENTING DES ALGORITHM
446import javax.crypto.*;
447import java.io.*;
448import java.security.InvalidAlgorithmParameterException;
449import java.security.spec.*;
450import javax.crypto.spec.IvParameterSpec;
451import java.lang.*;
452public class DesEncrypter
453{
454Cipher ecipher;
455Cipher dcipher;
456DesEncrypter(SecretKey key)
457{
458try{
459ecipher=Cipher.getInstance("DES");
460dcipher=Cipher.getInstance("DES");
461ecipher.init(Cipher.ENCRYPT_MODE,key);
462dcipher.init(Cipher.DECRYPT_MODE,key);
463}
464catch(javax.crypto.NoSuchPaddingException e){}
465catch(java.security.NoSuchAlgorithmException e){}
466catch(java.security.InvalidKeyException e){}
467}
468public String encrypt(String str)
469{
470try{
471byte[] utf8=str.getBytes("UTF8");
472byte[] enc=ecipher.doFinal(utf8);
473return new sun.misc.BASE64Encoder().encode(enc);
474}
475catch(javax.crypto.BadPaddingException e){}
476catch(IllegalBlockSizeException e){}
477catch(UnsupportedEncodingException e){}
478catch(java.io.IOException e){}
479return null;
480}
481public String decrypt(String str)
482{
483try
484{
485byte[] dec=new sun.misc.BASE64Decoder().decodeBuffer(str);
486byte[] utf8=dcipher.doFinal(dec);
487return new String(utf8,"UTF8");
488}
489catch(javax.crypto.BadPaddingException e){}
490catch(IllegalBlockSizeException e){}
491catch(UnsupportedEncodingException e){}
492catch(java.io.IOException e){}
493return null;
494}
495public static void main(String args[])
496{
497System.out.println();
498System.out.println("Encoding string using DES");
499System.out.println();
500try{
501SecretKey key= KeyGenerator.getInstance("DES").generateKey();
502DesEncrypter encrypter=new DesEncrypter(key);
503String s="Don't tell Anybody";
504String d="hello";
505String encrypted= encrypter.encrypt(s);
506String decrypted=encrypter.decrypt(encrypted);
507System.out.println("Original string is:-"+s);
508System.out.println("Encrypted string is:-"+encrypted);
509System.out.println("Decrypted string is:-"+decrypted);
510}
511catch(Exception e)
512{}
513}
514}
515
516PRACTICAL NO. 5
517 IMPLEMENTING AES
518import java.security.*;
519import javax.crypto.*;
520import java.io.*;
521public class AES_StringEncrypter
522{
523Cipher ecipher;
524Cipher dcipher;
525 AES_StringEncrypter(SecretKey key)
526{
527try
528{
529ecipher=Cipher.getInstance("AES");
530dcipher=Cipher.getInstance("AES");
531ecipher.init(Cipher.ENCRYPT_MODE,key);
532dcipher.init(Cipher.DECRYPT_MODE,key);
533}
534catch(Exception e){}
535}
536public String encrypt(String str)
537{
538try
539{
540byte[] utf8=str.getBytes("UTF8");
541byte[] enc=ecipher.doFinal(utf8);
542return new sun.misc.BASE64Encoder().encode(enc);
543}
544catch(Exception e){}
545return null;
546}
547public String decrypt(String str)
548{
549try
550{
551byte[]dec=new sun.misc.BASE64Decoder().decodeBuffer(str);
552byte[]utf8=dcipher.doFinal(dec);
553return new String(utf8,"UTF8");
554}
555catch(Exception e){}
556return null;
557}
558public static void main(String args[])
559{
560SecretKey key=null;
561try
562{
563KeyGenerator keyGen= KeyGenerator.getInstance("AES");
564key=keyGen.generateKey();
565}
566catch(Exception e)
567{
568e.printStackTrace();
569}
570AES_StringEncrypter dese=new AES_StringEncrypter(key);
571String o="Welcome to NETWORK SECURITY...";
572String en=dese.encrypt(o);
573String de=dese.decrypt(en);
574System.out.println("Original text:"+o);
575System.out.println("encrypted text:"+en);
576System.out.println("decrypted text:"+de);
577}
578}
579