Hakam
Hakam

Reputation: 39

Decrypt ciphertext to plaintext with known key

string S, K, generated;
cout << "Enter the message: ";
cin >> S;
cout << "Enter the key: ";
cin >> K;
cout << "The message is: " << S << endl; // output the string
int seed = 0;
for(int i = 0; i < (int) K.length(); i++)
    seed += K[i]; // used to generate a certain sequence of numbers
srand(seed); // new seed per new key
cout << "The cipher is: ";
for(int i = 0; i < (int) S.length(); i++) {
    int R = rand() % K.length();
    char L = 65 + (S[i] - 65 + K[R] - 65) % 26;
    cout << L;
    generated += L; // to actually have something to use for decryption
}
// FINALLY, to reach to this step and do something like this took me over 2 hours of continuous debugging
cout << endl;
cout << "The message again is: ";
for(int i = 0; i < (int) generated.length(); i++) {
    int R = rand() % K.length();
    char L = 65 + (generated[i] - 65 + K[R] - 65) % 26;
    cout << L;
}
cout << endl;

Sorry for the messy code. Anyway, so here's what I did so far:

But now I'm actually stuck with the fact that I want to decrypt the ciphertext using the correct key. Basically, I want to return the ciphertext to plaintext. I put the effort to do this myself and the approach is listed under the 'The message again is:' but it gives me a wrong result.

What am I doing wrong here?

Upvotes: 1

Views: 1560

Answers (2)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

This code is strange but it can actually work. Provided that the encoder and decoder are made by the same compiler, and probably on the same computer.

You are using the key to generate the seed for srand. This seed can be reproduced. The proceeding random numbers will be predictable.

When decoding the message, you are supposed to srand again with the same seed.

int main()
{
    string S, K, generated;
    S = "MESSAGE";
    K = "KEY";
    cout << "The message is: " << S << endl; // output the string

    {
        int seed = 0;
        for (int i = 0; i < (int)K.length(); i++)
            seed += K[i]; // used to generate a certain sequence of numbers
        srand(seed); // new seed per new key
    }

    cout << "The cipher is: ";
    for (int i = 0; i < (int)S.length(); i++) 
    {
        int R = rand() % K.length();
        char L = 65 + (S[i] - 65 + K[R] - 65) % 26;
        cout << L;
        generated += L; // to actually have something to use for decryption
    }

    {//we can use the key to regenerate the same seed:
        int seed = 0;
        for (int i = 0; i < (int)K.length(); i++)
            seed += K[i]; 
        srand(seed); //**** this is critical ****
    }

    cout << endl << "The message again is: ";
    for (int i = 0; i < (int)generated.length(); i++) 
    {
        int R = rand() % K.length();
        char L = 65 + (generated[i] - 65 + (26 - (K[R] - 65)) ) % 26;//reverse shift
        cout << L;
    }
    cout << endl;
    return 0;
}

Upvotes: 1

Tanner
Tanner

Reputation: 88

Not totally sure since I didn't get to actually compile a new solution, but my guess would be because you are using a new random number during your decryption phase. You need to preserve the original random number used in encryption in order to make the function invertable.

Upvotes: 1

Related Questions