LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-13-2005, 10:01 PM   #1
Gato Azul
Member
 
Registered: Sep 2003
Location: /dev/null
Distribution: CentOS, Ubuntu
Posts: 128

Rep: Reputation: 16
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!
 
Old 10-15-2005, 09:12 AM   #2
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 94

Rep: Reputation: 17
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
 
1 members found this post helpful.
Old 02-01-2013, 11:38 AM   #3
oregon1979
LQ Newbie
 
Registered: Feb 2013
Posts: 1

Rep: Reputation: Disabled
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?

Last edited by oregon1979; 02-01-2013 at 11:39 AM. Reason: Forgot to put something
 
Old 02-01-2013, 01:47 PM   #4
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 94

Rep: Reputation: 17
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;
        }
    }
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
True stream cipher in crypto API module? ta0kira Linux - Security 0 07-23-2005 02:10 AM


All times are GMT -5. The time now is 10:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration