Is this what I should have done? (Cipher program)

Hello, I have written a program for a Random Cipher. Here is the description:

The program generates a cipher by replacing each Latin letter in the alphabet by one of the other letters, based on the pseudo-random numbers generator. Two different letters cannot be replaced by the same letter.

I have written the program based on the way I perceived the description. However, looking at it now, it arises questions, because the cipher itself is impossible to break as each letter is substituted absolutely randomly using the pseudo-random number generator. Maybe I should have written a Caesar cipher and the key is simply the random number generated by rand()? I also thought of this when I started my program but it seemed to me unlikely that this is the case.

What do you think, has my approach been correct? I have to send it by 8th to my professor, but I got anxious now wondering whether this is what I should have done.

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    string alphaUp="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string alphaLow="abcdefghijklmnopqrstuvwxyz";                      
    const int LENGTH=alphaUp.size();
    
    srand(time(0));     
    int r;
    char temp, tempb;	

    //shuffles the strings
    for (int i=0; i<LENGTH; i++)                 
    {
        r = rand() % LENGTH;                   

        //shuffle upper-case Latin letters
        temp = alphaUp[i];
        alphaUp[i] = alphaUp[r];
        alphaUp[r] = temp;

        //shuffle lower-case Latin letters
        tempb = alphaLow[i];
        alphaLow[i] = alphaLow[r];
        alphaLow[r] = tempb;
     }

    string encrypted;
    getline(cin, encrypted);
    for (unsigned int i=0; i<encrypted.length(); i++)
    {
        if (isalpha(encrypted[i]))                              
        {
            if (islower(encrypted[i]))
            {
                encrypted[i]=alphaLow[encrypted[i] - 'a'];      
            }
            else
            {
                encrypted[i]=alphaUp[encrypted[i] - 'A'];       
            }
        }
    }
OP wrote:
However, looking at it now, it arises questions, because the cipher itself is impossible to break as each letter is substituted absolutely randomly using the pseudo-random number generator.

Not quite, it's still a mono-alphabetic substitution cipher. In fact my local newspaper used to have a game in where breaking this was the objective. Here is one way to do it: http://en.wikipedia.org/wiki/Frequency_analysis

I'd like to point out std::swap: http://www.cplusplus.com/reference/utility/swap/ but other then that, it looks like you went with a direct route. The only thing I would say is don't bother with discerning upper and lower case. 'A' is NOT the same as 'a' so you still wouldn't have one letter represented by two symbols.
yeah frequency analysis can break any monoalphabetic substitution cipher quite easily,which this is.also a lot of attacks come when a pseudo random number generator is used,because knowing the algorithm means that it can be used to find the numbers that were generated.
atleast thats what i think.
Thank you for the answer, but, hm, at the bottom of the page of my course I have a link to "Polyaplphabetic cipher" in wikipedia, which is supposed to be something like a hint to my homework, and both of you guys are talking about Monoaplhabetic cipher (about my program).

I guess I have done something terribly wrong, because these things are different? Damn.

However, I read the wiki page before starting my program and I thought that what I had written was a poly cipher since each letter is substituted by a different random number, generated at each loop iteration (25 times).

What should I change to make it a polyalphabetic if it is a monoalphabetic cipher as of now?

wiki:

A polyalphabetic cipher is any cipher based on substitution, using multiple substitution alphabets.


thanks
OP wrote:
Two different letters cannot be replaced by the same letter... What should I change to make it a polyalphabetic if it is a monoalphabetic cipher as of now?

Sorry to be TFG here OP, but these two goals are mutually exclusive to one and other. Multiple substitutions is exactly what a poly-cipher is all about.
Here is a demonstrations of a poly-cipher:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <vector>


int main()
{
    std::string Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    std::vector<std::string> AlphaVec;
    std::string Input;

    size_t FoundAt  = 0;

    for(size_t i = 0; i < Alpha.size(); ++i) ///ROTATE THE LETTERS IN YOUR LIBRARY
    {
        AlphaVec.push_back(Alpha); ///SAVE CURRENT INSTANCE OF ALPHABET

        Input = Alpha.back(); ///BORROW INPUT TO COPY CHARACTER FROM END OF STRING
        Alpha.pop_back(); ///DELETE CHARACTER FROM END OF STRING
        Alpha.insert(0, Input); ///INSERT COPY INTO BEGINNING OF STRING

        //std::cout << Alpha << "\n";
    } ///NOTE THAT THIS IS DONE ENOUGH TIMES TO RETURN 'Alpha' TO IT'S ORIGINAL STATE

    std::cout << "Enter Data: ";
    std::cin >> Input;

    for(size_t i = 0; i < Input.size(); ++i)
    {
        FoundAt = Alpha.find(Input[i], 0); ///FIND OCCURANCE OF LETTER IN 'Alpha'

        if(FoundAt != std::string::npos) ///IF THE LETTER WAS FOUND
        {
            Input[i] = AlphaVec[(i % AlphaVec.size())][FoundAt]; ///APPLY THAT OFFSET TO CURRENT LOOKUPTABLE AND REPLACE IT
        }
    }

    std::cout << "Encrypted String: " << Input << "\n";

    return 0;
}

Notice what happens when you enter in the same letter over and over? Try putting in
AAAA
. Note that the same later, 'A', gets replaced with different ciphers
@Computergeek01 thank you for the replies. I ran your code, but I guess I didn't quite understand it as I haven't used some of the functions in it since it seems like gcc 4.8.2 does not support them. I noticed that entering the same letter many times produces the same result as far as letters, only differing in case.

Anyway, what you said about the two conditions of my task being mutually exclusive, I did not quite understand. Do you mean that a poly cipher cannot work without repeating letters in the encryption? I tried running a vigenere cipher, which is supposedly a poly cipher, but I got some letters repeated. You pointed out that "multiple substitutions is exactly what a poly-cipher is all about." I thought in my case I was doing multiple substitutions of a given input.

I am afraid I am still uncertain of what I have to change in order to make it a poly cipher. I guess I have to rework the for loop which encrypts in a certain way?
Last edited on
By the way, your code to shuffle the strings is wrong. Rather than swapping each letter with a random letter in the alphabet, you should do it like a deck of cards: pick a random letter to go first. Then pick one of the remaining letters to go second, etc.
has my approach been correct?

It's impossible to know without knowing the exact problem you're trying to solve. Can you post the text of the assignment?
Last edited on
Topic archived. No new replies allowed.