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