I'm trying to create a very simple enigma machine cipher. But mine doesn't decrypt the data correcly.

#include <iostream>
#include <string>

using namespace std;

const char alphabet[27] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
const char reflector[27] = { "WSXRFVYHNIKPQAZEDCTGBUJMOL" };
const char rotors[27] = { "QWERTYUIOPASDFGHJKLZXCVBNM" };

/* Get the position of a character in the alphabet. */
int GetPos(char _char)
{
    return int(_char) - int('A');
}

void Encrypt(char *message, char *returnMessage, size_t size)
{
    char set1;
    char set1_r;
    char reflect;
    char letter;
    int position;

    for (int i = 0; i < size; i++)
    {
        letter = message[i];

        // Pass the character through the rotors.
        position = GetPos(letter);
        set1 = rotors[position];

        // Pass the character through the reflector.
        position = GetPos(set1);
        reflect = reflector[position];

        // Pass the character through the rotors in reverse.
        position = GetPos(reflect);
        set1_r = rotors[position];

        returnMessage[i] = set1_r;
    }
}

void Decrypt(char *message, char *returnMessage, size_t size)
{
    char set1;
    char set1_r;
    char reflect;
    char letter;
    int position;

    for (int i = 0; i < size; i++)
    {
        letter = message[i];

        // Pass the character through the rotors in reverse.
        position = GetPos(letter);
        set1 = rotors[position];

        // Pass the character through the reflector.
        position = GetPos(set1);
        reflect = reflector[position];

        // Pass the character through the rotors in reverse.
        position = GetPos(reflect);
        set1_r = rotors[position];

        returnMessage[i] = set1_r;
        char p = alphabet[position];

        returnMessage[i] = p;
    }
}

int main(int argc, char *argv[])
{
    char *message = "H";
    size_t size = strlen(message);
    char *retMessage = new char[size];
    char *retMessage2 = new char[size];

    if ((retMessage == nullptr) || (retMessage2 == nullptr))
    {
        cout << "Error allocating memory." << endl;
        return -1;
    }

    cout << "Original message: " << message << endl;
    Encrypt(message, retMessage, size);
    cout << "Encrypted message: ";
    for (int i = 0; i < size; i++)
        cout << retMessage[i];

    cout << endl;
    Decrypt(retMessage, retMessage2, size);
    cout << "Decrypted message: ";
    for (int i = 0; i < size; i++)
        cout << retMessage2[i];

    delete[] retMessage;
    delete[] retMessage2;

    cin.get();

    return 0;
}

As you can see it takes the letter H. It then encrypts the letter, and I get F, when I decrypt F then I get O. Clearly there is something wrong with my decryption stage or my encryption, but I suspect it is the decryption that is faulty.

What is wrong and how do I fix it?

Your problem is that even though you're reversing the steps you aren't reversing the operations. Which means that your GetPos function needs to be modified to get the character's index from any of the arrays. It should look something like this:

int GetPos(char _char, const char char_array[])
{
    for (int i = 0; i < 25; i++)
    {
        if (char_array[i] == _char)
            return i;
    }
}
void Encrypt(char *message, char *returnMessage, size_t size)
{
    char set1;
    char set1_r;
    char reflect;
    char letter;
    int position;
    for (int i = 0; i < size; i++)
    {
        letter = message[i];
        // Pass the character through the rotors.
        position = GetPos(letter, alphabet);
        set1 = rotors[position];
        // Pass the character through the reflector.
        position = GetPos(set1, alphabet);
        reflect = reflector[position];
        // Pass the character through the rotors in reverse.
        position = GetPos(reflect, alphabet);
        set1_r = rotors[position];
        returnMessage[i] = set1_r;
    }
}
void Decrypt(char *message, char *returnMessage, size_t size)
{
    char set1;
    char set1_r;
    char reflect;
    char letter;
    int position;
    for (int i = 0; i < size; i++)
    {
        letter = message[i];
        // Pass the character through the rotors in reverse.
        position = GetPos(letter, rotors);
        set1 = alphabet[position];
        // Pass the character through the reflector.
        position = GetPos(set1, reflector);
        reflect = alphabet[position];
        // Pass the character through the rotors in reverse.
        position = GetPos(reflect, rotors);
        set1_r = alphabet[position];

        returnMessage[i] = set1_r;
    }
}

A couple of caveats:

  • this only fixes the code you've submitted. It doesn't verify whether you're implementing a true enigma cipher.

  • There is no error checking(ie non-alphabetic characters)

WOW! Dont I feel stupid. I cant believe I've been overlooking that for the entire day.

Yes I know I'm not checking non-alphabetical characters, this was a smaller version of the full thing.

Thanks for the help.

Please remember to mark the post solved if your question is answered Thank You

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.