· 7 years ago · Nov 24, 2018, 08:38 PM
1
2import javax.crypto.*;
3import javax.crypto.spec.IvParameterSpec;
4import javax.crypto.spec.SecretKeySpec;
5import java.nio.ByteBuffer;
6import java.security.InvalidKeyException;
7import java.security.NoSuchAlgorithmException;
8import java.security.SecureRandom;
9import java.util.Arrays;
10import java.util.HashMap;
11import java.util.Map;
12
13public class Mitm {
14
15 private static Map<Integer, byte[]> mapEncrypted = new HashMap<>();
16 private static Map<Integer, byte[]> mapDecrypted = new HashMap<>();
17 private static Map<Integer, byte[]> mapPair = new HashMap<>();
18 private static byte[] textPlain = "nooooooo".getBytes();
19 private static byte[] textCipher1;
20 private static byte[] textCipher2;
21 private static int total = (int) Math.pow(2,3);
22
23
24 private static void cryptDES() {
25 try {
26 Cipher ecipher = Cipher.getInstance("DES/ECB/NoPadding");
27 byte[] bytes;
28 SecretKey key;
29 byte[] textEncrypted;
30
31 for (int i = 0; i < total; i++) {
32
33 bytes = ByteBuffer.allocate(8).putLong(i).array();
34 key = new SecretKeySpec(bytes, 0, bytes.length, "DES");
35 ecipher.init(Cipher.ENCRYPT_MODE, key);
36 textEncrypted = ecipher.doFinal(textPlain);
37 mapEncrypted.put(i, textEncrypted);
38 }
39
40 for (Map.Entry<Integer, byte[]> entry : mapEncrypted.entrySet())
41 {
42 System.out.println("Key : " + entry.getKey() + " Encrypted Text : " + Arrays.toString(entry.getValue()));
43 }
44
45
46 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
47 e.printStackTrace();
48 }
49
50 }
51
52
53 private static void decryptDES() {
54 try {
55 Cipher dcipher = Cipher.getInstance("DES/ECB/NoPadding");
56 byte[] bytes;
57 SecretKey key;
58 byte[] textDecrypted;
59
60 for (int i = 0; i < total; i++) {
61 bytes = ByteBuffer.allocate(8).putLong(i).array();
62 key = new SecretKeySpec(bytes, 0, bytes.length, "DES");
63 dcipher.init(Cipher.DECRYPT_MODE, key);
64 textDecrypted = dcipher.doFinal(textCipher2);
65 mapDecrypted.put(i, textDecrypted);
66 }
67
68
69 for (Map.Entry<Integer, byte[]> entry : mapDecrypted.entrySet())
70 {
71 System.out.println("Key : " + entry.getKey() + " Decrypted Text : " + Arrays.toString(entry.getValue()));
72 //System.out.println("Size : " + entry.getValue().length);
73 }
74
75
76
77 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
78 e.printStackTrace();
79 }
80
81 }
82
83
84 private static void doubleDES() {
85 try {
86
87 Cipher ecipher1 = Cipher.getInstance("DES/ECB/NoPadding");
88 Cipher ecipher2 = Cipher.getInstance("DES/ECB/NoPadding");
89
90
91 byte[] bytes1 = ByteBuffer.allocate(8).putLong(4).array();
92 byte[] bytes2 = ByteBuffer.allocate(8).putLong(2).array();
93
94 SecretKey key1 = new SecretKeySpec(bytes1, 0, bytes1.length, "DES");
95 SecretKey key2 = new SecretKeySpec(bytes2, 0, bytes2.length, "DES");
96
97
98 ecipher1.init(Cipher.ENCRYPT_MODE, key1);
99 ecipher2.init(Cipher.ENCRYPT_MODE, key2);
100
101 textCipher1 = ecipher1.doFinal(textPlain);
102 textCipher2 = ecipher2.doFinal(textCipher1);
103
104 mapPair.put(0,textPlain);
105 mapPair.put(1,textCipher1);
106
107 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
108 e.printStackTrace();
109 }
110
111 }
112
113
114 public static void main(String[] args) {
115 doubleDES();
116 cryptDES();
117 decryptDES();
118
119
120
121 for (int i=0; i < 10; i++) {
122 System.out.println(Arrays.toString(new boolean[]{Arrays.equals(textCipher1, mapEncrypted.get(i))}));
123
124 }
125
126 }
127}