This is the eighth challenge of Set 1 in The Cryptopals Crypto Challenges website. Previously, I spoke about these challenges and provided walkthroughs for the previous challenges, if you haven't read them, here are the links:

For this challenge, you are given a file, which contains a bunch of ciphertexts that has been encrypted using AES-128 Cipher but only one of them has an ECB (Electronic Codebook) mode. Find the string the string that has the ECB mode.

In the previous post, I had explained that ECB is a cipher mode that is used to repeat the key until it covers the entire plaintext i.e. the same 16 byte plaintext will have the same 16 byte ciphertext.

Well, the solution is pretty much trivial, so here's the solution:

Implementation of the method(s):

//Detect ECB Mode in AES Cipher
bool CryptoLib::detect_ecb_mode(string str, int keyLength)
{
    //Divide into equal amount of blocks
    int blocks = str.size() / keyLength;

    /*
        Theory: the problem with ECB as I had mentioned
        in the previous post, it uses the exact number of bytes of the ciphertext
        to encrypt the plaintext repeatedly.

        In that case, just do the reverse.

        Divide it into equal amount of blocks, in this case, we
        know the key is "YELLOW SUBMARINE", which is 16 bytes.

        Then, all you have to do is take two substrings of a string and compare,
        if they have the same string, we found it!
    */
    for(int i=0; i<blocks; i++)
    {
        //Take a substring of x number of bytes
        string strA = str.substr(i*keyLength, keyLength);

        for(int j=i+1; j<blocks; j++)
        {
            //Take another substring of x number of bytes
            string strB = str.substr(j*keyLength, keyLength);
            if(strA == strB)
            {
                //Found
                return true;
            }
        }
    }
    return false;
}

Final code:

//The Cryptopals Crypto Challenges - Set 1 Challenge 8
#include "crypto.h"

int main()
{
    CryptoLib crypt;

    string str;

    ifstream infile;
    infile.open("8.txt");

    int count = 0;
    while(!infile.eof())
    {
        getline(infile, str);

        //Check if this string has ECB mode
        if(crypt.detect_ecb_mode(str, 16) == true)
        {
            cout << "FOUND AT LINE " << count << " => " << str << endl;
            break;
        }
        count++;
    }
}

Note: This solution and the library named crypto.h was built using the C++ programming language. The source code for this solution can be found on Github.

Well, this challenge marks the end of Set 1 from The Cryptopals Crypto Challenges. However, I do intend to continue solving all these crypto challenges, let's see how it goes!

Until next time, then!