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