mardi 3 février 2015

Decoding text using a random generated substitution cipher

I am almost done with a program for a random cipher which encodes and decodes. However, I can't get my program to also decode messages. It confuses me as I don't have a key to the cipher, since the letters are replaced in a random manner each time, so I have no idea how a decryption code should look like for this program. What approach should I take?


Here is the code:



void randomizer() //cipher


{ srand(time(0)); //seed for rand() static char alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //static array to hold uppercase Latin letters static char alphabetlow[]="abcdefghijklmnopqrstuvwxyz"; //lower-case Latin letters const int LENGTH=sizeof(alphabet)-1; //length of the array



int r;
char temp;
char tempb;
for (unsigned int i=0; i<LENGTH; i++) //loop which shuffles the array
{
r=rand() % LENGTH; //generate a sequnce of pseudo-random numbers

temp=alphabet[i]; //temp gets the int values of the array elements
alphabet[i] = alphabet[r]; //randomized letters are copied into the ordered alphabet (now scrambled)
alphabet[r]=temp; //ordered letters are copied into the scrambled alphabet

tempb=alphabetlow[i]; //shuffle second array
alphabetlow[i]=alphabetlow[r];
alphabetlow[r]=tempb;
}
string text, textb;
getline(cin,text);

for (unsigned int i=0; i<text.length(); i++) //loop to encrypt
{
if (isalpha(text[i])) //encrypt only input text which is Latin letters
{
if (islower(text[i])) //checks if input text is lowercase letters
{
text[i]=alphabetlow[text[i] - 'a']; //scrambles lowercase letters
}
else
{
text[i]=alphabet[text[i] - 'A']; //scrambles uppercase letters
}
}
}
cout<<"Encrypted: "<<text<<endl;

getline (cin, textc);
for (unsigned i=0; i<textc.length(); i++)
{
//supposedly code to decode should be here
}
cout<<"Decrypted: "<<textc;




Aucun commentaire:

Enregistrer un commentaire