I’m working on a Shit Cipher and decrypting a particular piece of text. Ok, so, how the program works:
- Takes the characters from a text file
- Shift each of the characters 9 places down the alphabet.
Now, I have done this, however, I know that the character cannot be shift always by 9 places, so the program looks at where the character is in the alphabet char array and then if it can be done, it just adds 9, and if it cannot be done, it just takes 9 away (Finds the difference). But, it’s not working and I can’t figure out where I am going wrong.
Here is the code:
#include <iostream>
#include <fstream>
using namespace std;
string inputFile = "";
#define MAX_FILE_SIZE 10000
const char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
const char alphabetUpper[26] =
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
const int sizeAlpha = sizeof(alphabet)/sizeof(alphabet[0]);
void Data(char* theData)
{
ifstream txtFile(inputFile.c_str());
if(!txtFile.is_open())
{
cerr << "Cannot open text file";
}
txtFile.read(theData, 520);
}
int main(int argc, char *argv[]) {
char getData[MAX_FILE_SIZE];
Data(getData);
char decrypted[520];
int algorthm;
for(unsigned i=0; (i < 520); i++)
{
for(unsigned j=0; (j < 26); j++)
{
if(getData[i] == alphabet[j] || alphabetUpper[j])
{
algorthm = j + 9; // we move 9 places.
if(sizeAlpha < algorthm)
{
decrypted[i] = alphabet[algorthm];
}else if(algorthm > sizeAlpha || algorthm == sizeAlpha)
{
algorthm = sizeAlpha - j;
decrypted[i] = alphabet[algorthm];
}
}
}
}
for(unsigned i=0; (i < 520); i++)
{
cout << decrypted[i];
}
}
Anyone know where I’m going wrong, or, can offer a simular solution?
That cannot possibly be reversible, because you map two different values to the same one.
Instead, you have to wrap around: