LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Affine Cipher (https://www.linuxquestions.org/questions/programming-9/affine-cipher-372845/)

Gato Azul 10-13-2005 10:01 PM

Affine Cipher
 
I'm trying to write a brute force function that decrypts text that's been encrypted with an affine cipher, i.e. plaintext = (ciphertext * multiplier + adder) mod 26. So far here's what I have:

Code:

void decrypt(string str1)
{
    int i = 0;
    int mult = 0;
    int add = 0;
    int length = str1.length();
    string str2;

    for(mult=0; mult<26; mult++)
    {
        for(add=0; add<26; add++)
        {
            // decrypt string
            for(i=0; i<length; i++)
            {
                // convert from ASCII to 0-25, perform operations,
                // reconvert to ASCII
                str2[i] = (((mult * (str1[i] - 65) + add) % 26) + 65);
            }

            // Print trial results
            cout << "Multiplier: " << mult << " Adder: " << add << endl;
            for(i=0; i<length; i++)
            {
                cout << str2[i];
            }
            cout << endl << endl;
        }
    }
}

However, once I get to i=36 in the decryption loop, my adder (and later on my multiplier) variable starts to change, which is most disturbing (and quite bad for results). I've been going back through my old programming books and trying to figure out what I'm doing wrong (apart from the ugly coding...I'm most sincerely sorry about that) as well as Googling around, but no such luck. Any suggestions, either on what I'm doing wrong or how to improve the code/algorithm/logic/etc? Thanks so much in advance for any help!

YetAnotherDave 10-15-2005 09:12 AM

My guess is that str2 is not big enough for the "str2[i]" indexing that you are doing and this is overwriting the where mult and add are stored. Try setting the str2 length with resize() :

Code:

    int add = 0;
    int length = str1.length();
    string str2;

    str2.resize(length);  //  <-- NEW LINE


oregon1979 02-01-2013 11:38 AM

Str1 and Str2?
 
You pass in str1, but then where does str2 come from? You also make length equal str1.length() but don't use it?

YetAnotherDave 02-01-2013 01:47 PM

Here's the whole function including my suggested modification and some additional comments to address your concerns:

Code:

void decrypt(string str1)
{
    int i = 0;
    int mult = 0;
    int add = 0;
    int length = str1.length();
    string str2;                  //  <-- str2 DEFINED HERE ( AS IN CODE POSTED FOR ORIGINAL QUESTION )

    str2.resize(length);          //  <-- NEW LINE

    for(mult=0; mult<26; mult++)
    {
        for(add=0; add<26; add++)
        {
            // decrypt string
            for(i=0; i<length; i++)
            {
                // convert from ASCII to 0-25, perform operations,
                // reconvert to ASCII
                str2[i] = (((mult * (str1[i] - 65) + add) % 26) + 65);  //  <-- length ELEMENTS OF str2 SET HERE.
            }

            // Print trial results
            cout << "Multiplier: " << mult << " Adder: " << add << endl;
            for(i=0; i<length; i++)
            {
                cout << str2[i];                                        //  <-- length ELEMENTS OF str2 USED HERE.
            }
            cout << endl << endl;
        }
    }
}



All times are GMT -5. The time now is 10:36 AM.