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.
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.
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.
#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?
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?