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!