I am working with meet-in-the-middle attack on 2DES. I have implemented the DES encryption/decryption and it is working. The way I am trying to achieve this is by storing, within a for loop, the intermediate ciphers as the key of a HashMap and the possible keys as the values of the HashMap. Both as integers - Started with byte[], but figured out that a key cannot be an array. However, within this for loop I also want to make sure that the possible keys are unique i.e. I have a while loop, which makes sure that the size of the HashMap is 2^20 (only 20 bits of the DES key are effective). Aftwards, I try to find the keys, which have matching intermediate cipher text by iterating through the HashMap with a foreach and compare each intermediate cipher text from the encryptions with the intermediate cipher text from the decryptions.
However, I am unable to find the match as it takes too long to finish. I have been waiting for like 20 mins without any result.
while (intermediateCipher.size() < Math.pow(2, 20)) {
byte[] key = generateDesKey();
intermediateCipher.put(ByteBuffer.wrap(encrypt(key, plainText)).getInt() , ByteBuffer.wrap(key).getInt());
}
int count = 0;
for (Entry<Integer, Integer> arr : intermediateCipher.entrySet()) {
byte[] k2 = ByteBuffer.allocate(8).putInt(arr.getValue()).array();
int temp = ByteBuffer.wrap(decrypt(k2, cipherText2)).getInt();
if (intermediateCipher.containsKey(temp)) {
count++;
byte[] k1 = ByteBuffer.allocate(8).putInt(intermediateCipher.get(temp)).array();
if (encrypt(k2, encrypt(k1, plainText)) == cipherText2) {
System.out.println("Key is " + k1 + " " + k2);
}
}
}
intermediateCipher is my HashMap .
PlainText is a byte[] of 8 bytes, cipherText2 is the encryption result of 2DES on the plainText and generateDesKey is the method which generates 64 bits, where the parity bits are skipped in order to make sure that the 20 bits are effective, and convert the bits to a byte[] as DES requires that