· 5 years ago · Feb 19, 2020, 07:50 PM
1DES
2
3import javax.swing.*;
4import java.security.SecureRandom;
5import javax.crypto.Cipher;
6import javax.crypto.KeyGenerator;
7import javax.crypto.SecretKey;
8import javax.crypto.spec.SecretKeySpec;
9import java.util.Random ;
10class DES {
11byte[] skey = new byte[1000];
12String skeyString;
13static byte[] raw;
14String inputMessage,encryptedData,decryptedMessage;
15
16public DES() {
17try {
18generateSymmetricKey();
19
20inputMessage=JOptionPane.showInputDialog(null,"Enter message to encrypt");
21byte[] ibyte = inputMessage.getBytes();
22byte[] ebyte=encrypt(raw, ibyte);
23String encryptedData = new String(ebyte);
24System.out.println("Encrypted message "+encryptedData);
25JOptionPane.showMessageDialog(null,"Encrypted Data "+"\n"+encryptedData);
26
27byte[] dbyte= decrypt(raw,ebyte);
28String decryptedMessage = new String(dbyte);
29System.out.println("Decrypted message "+decryptedMessage);
30
31JOptionPane.showMessageDialog(null,"Decrypted Data "+"\n"+decryptedMessage);
32}
33catch(Exception e) {
34System.out.println(e);
35}
36
37}
38void generateSymmetricKey() {
39try {
40Random r = new Random();
41int num = r.nextInt(10000);
42String knum = String.valueOf(num);
43byte[] knumb = knum.getBytes();
44skey=getRawKey(knumb);
45skeyString = new String(skey);
46System.out.println("DES Symmetric key = "+skeyString);
47}
48catch(Exception e) {
49System.out.println(e);
50}
51}
52private static byte[] getRawKey(byte[] seed) throws Exception {
53KeyGenerator kgen = KeyGenerator.getInstance("DES");
54SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
55sr.setSeed(seed);
56kgen.init(56, sr);
57SecretKey skey = kgen.generateKey();
58raw = skey.getEncoded();
59return raw;
60}
61private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
62SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
63Cipher cipher = Cipher.getInstance("DES");
64cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
65byte[] encrypted = cipher.doFinal(clear);
66return encrypted;
67}
68
69private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
70SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
71Cipher cipher = Cipher.getInstance("DES");
72cipher.init(Cipher.DECRYPT_MODE, skeySpec);
73byte[] decrypted = cipher.doFinal(encrypted);
74return decrypted;
75}
76public static void main(String args[]) {
77DES des = new DES();
78}
79}
80
81
82caesar cipher
83
84
85import java.util.Scanner;
86
87public class CaesarCipher
88{
89 public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
90
91 public static String encrypt(String plainText, int shiftKey)
92 {
93 plainText = plainText.toLowerCase();
94 String cipherText = "";
95 for (int i = 0; i < plainText.length(); i++)
96 {
97 int charPosition = ALPHABET.indexOf(plainText.charAt(i));
98 int keyVal = (shiftKey + charPosition) % 26;
99 char replaceVal = ALPHABET.charAt(keyVal);
100 cipherText += replaceVal;
101 }
102 return cipherText;
103 }
104
105 public static String decrypt(String cipherText, int shiftKey)
106 {
107 cipherText = cipherText.toLowerCase();
108 String plainText = "";
109 for (int i = 0; i < cipherText.length(); i++)
110 {
111 int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
112 int keyVal = (charPosition - shiftKey) % 26;
113 if (keyVal < 0)
114 {
115 keyVal = ALPHABET.length() + keyVal;
116 }
117 char replaceVal = ALPHABET.charAt(keyVal);
118 plainText += replaceVal;
119 }
120 return plainText;
121 }
122
123 public static void main(String[] args)
124 {
125 Scanner sc = new Scanner(System.in);
126 System.out.println("Enter the String for Encryption: ");
127 String message = new String();
128 message = sc.next();
129 System.out.println(encrypt(message, 3));
130 System.out.println(decrypt(encrypt(message, 3), 3));
131 sc.close();
132 }
133}
134
135
136
137hill cipher
138
139public class HillCipherLogic {
140 static int[][] decrypt = new int[3][1];
141 static int[][] a = new int[3][3];
142 static int[][] b = new int[3][3];
143 static int[][] mes = new int[3][1];
144 static int[][] res = new int[3][1];
145 static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
146 static Scanner sc = new Scanner(System.in);
147
148 public static void main(String[] args) throws IOException {
149 getKeys();
150 for (int i = 0; i < 3; i++)
151 for (int j = 0; j < 1; j++)
152 for (int k = 0; k < 3; k++) {
153 res[i][j] = res[i][j] + a[i][k] * mes[k][j];
154 }
155 System.out.print("\nEncrypted string is : ");
156 for (int i = 0; i < 3; i++) {
157
158
159 System.out.print((char) (res[i][0] % 26 + 97));
160 res[i][0] = res[i][0];
161 }
162 getInverse();
163 for (int i = 0; i < 3; i++)
164 for (int j = 0; j < 1; j++)
165 for (int k = 0; k < 3; k++) {
166 decrypt[i][j] = decrypt[i][j] + b[i][k] * res[k][j];
167 }
168 System.out.print("\nDecrypted string is : ");
169 for (int i = 0; i < 3; i++) {
170
171 System.out.print((char) (decrypt[i][0] % 26 + 97));
172 }
173 System.out.print("\n");
174 }
175 public static void getKeys() throws IOException {
176 System.out
177 .println("Enter 3x3 matrix for key (It should be inversible): ");
178 for (int i = 0; i < 3; i++)
179 for (int j = 0; j < 3; j++)
180 a[i][j] = sc.nextInt();
181 System.out.print("\nEnter a 3 letter string: ");
182 String msg = br.readLine();
183 for (int i = 0; i < 3; i++)
184 mes[i][0] = msg.charAt(i) - 97;
185 }
186
187
188 public static void getInverse() {
189
190 int p, q;
191 int[][] c = a;
192 for (int i = 0; i < 3; i++)
193 for (int j = 0; j < 3; j++) {
194 // a[i][j]=sc.nextFloat();
195 if (i == j)
196 b[i][j] = 1;
197 else
198 b[i][j] = 0;
199 }
200 for (int k = 0; k < 3; k++) {
201 for (int i = 0; i < 3; i++) {
202 p = c[i][k];
203 q = c[k][k];
204 for (int j = 0; j < 3; j++) {
205 if (i != k) {
206 c[i][j] = c[i][j] * q - p * c[k][j];
207 b[i][j] = b[i][j] * q - p * b[k][j];
208 }
209 }
210 }
211 }
212 for (int i = 0; i < 3; i++)
213 for (int j = 0; j < 3; j++) {
214 b[i][j] = b[i][j] / c[i][i];
215 }
216 System.out.println("");
217 System.out.println("\nInverse Matrix is : ");
218 for (int i = 0; i < 3; i++) {
219 for (int j = 0; j < 3; j++)
220 System.out.print(b[i][j] + " ");
221 System.out.print("\n");
222 }
223 }
224}
225
226
227xor
228
229#include<stdio.h>
230#include<conio.h>
231#include<string.h>
232#include<stdlib.h>
233void main()
234
235{
236
237elrscr();
238
239char str[]="HelloWorld";
240char str1[11];
241
242char str2[11];
243
244int i,len;
245
246len = strlen(str);
247for(i=0;i<len;i++)
248
249{
250
251strl[i]=str[i] & 127;
252printf("%oc",str1 [i]);
253}
254
255printf("\n");
256for(i=0;i<len;i++)
257
258{
259
260str2[i]=str[i]*127;
261printf("%oc",str2[i]);
262}
263
264printf("\n");
265getch();
266
267}
268
269
270
271and xor
272
273#include<stdio.h>
274#include<conio.h>
275#include<string.h>
276#include<stdlib.h>
277void main()
278
279{
280
281clrser():
282
283char str[]="Hello World";
284char str1[11];
285
286char str2[11];
287
288int i,len;
289
290len = strlen(str):
291for(i=0;i<len:i++)
292
293{
294
295str1[iJ=str[i] & 127:
296printf("%oc",str1[i]):
297}
298
299printf("n");
300for(i=0;i<len:i++)
301
302{
303
304str2[iJ=str[i]*127:
305printf("%oc",str2[i]):
306}
307
308printf("n");
309getch();
310
311}
312
313substitution cipher
314
315mport java.io.*;
316
317import java.util.*;
318
319public class SubstitutionCipher {
320
321static Scanner sc=new Scanner(System.in);
322
323static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
324public static void main(String[]args) throws IOException {
325//TODO code application logic here
326
327String a="abcdefghijklmnopqrstuvwxyz";
328
329String b="zyxwvutsrqponmlkjihgfedcba’";
330System.out.print("Enter any string:");
331
332String str=brreadLine();
333
334String decrypt="";
335
336char c;
337
338for(int i=0;i<strlength0;i++)
339
340{
341
342c=str.charAt(i);
343
344int j=a.indexOf(c);
345
346decrypt=decrypt+b.charAt(j);
347
348}
349
350System.out.printin("The encrypted data is:"+decrypt);
351}
352
353}
354
355
356Blow fish
357
358import java.io.FilelnputStream;
359
360import java.io.FileOutputStream;
361
362import java.security. Key:
363
364import java.util.Base64:
365
366import javax.crypto.Cipher;
367
368import javax.crypto.CipherOutputStream;
369
370import javax.crypto.KeyGenerator:
371
372public class BlowFish {
373
374public static void main(String[] args) throws Exception {
375KeyGenerator keyGenerator= KeyGenerator.getInstance("Blowfish”):
376keyGenerator.init(128).
377Key secretKey = keyGenerator.generateKey():
378Cipher cipherOut = Cipher.getInstance("Blowfish/CFB/NoPadding").
379cipherOut.init(Cipher ENCRYPT_MODE, secretKey);
380Base64.Encoder encoder = Base64.getEncoder();
381byte iv[] = cipherOut.getlV();
382if(ivl=null}{
383System.out.printin(Initialization vector of the Cipher:" + encoder.encodeToString(iv));
384}
385FilelnputStream fin = new FilelnputStream("inputFile. txt"):
386FileOutputStream fout = new FileOutputStream("“outputFile. txt");
387CipherOutputStream cout = new CipherOutputStream(fout,cipherOut);
388int input;
389while((input = fin.read()) != -1) {
390cout. write(input);
391}
392fin.close();
393cout.close();
394}
395}