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