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