I'm finishing up my simple shift cipher which takes a .txt file line by line and converts each character of the line to its decimal value and then adds an integer value (shift value) to that decimal value.
The decryption procedure works the same way except that instead of adding an integer value to each decimal value, the integer value is subtracted and herein lies the problem, which will be illustrated below.
The plain.txt file:
Unflinching on a critical first test, Senate Democrats closed ranks Thursday behind $460 billion in politically risky Medicare cuts at the heart of health care legislation, thwarting a Republican attempt to doom President Barack Obama's sweeping overhaul.
The bid by the bill's critics to reverse cuts to the popular Medicare program failed on a vote of 58-42, drawing the support of two Democratic defectors. Approval would have stripped out money needed to pay for expanding coverage to tens of millions of uninsured Americans.
The broader legislation aims to extend health coverage to 31 million who now lack it, while barring insurance industry practices such as denying coverage on the basis of pre-existing medical conditions. Though the overhaul is estimated to cost about $1 trillion over a decade, the Congressional Budget Office has said it would cut federal deficits by $130 billion over that period, and probably reduce them further in the 10 years beyond that.
The cipher.txt file:
Ž§Ÿ¥¢§œ¡¢§ Y¨§YšYœ«¢¢œš¥YŸ¢«¬Yž¬eYŒž§šžY}ž¦¨œ«š¬Yœ¥¨¬žY«š§¤¬Y¡®«¬š²Y›ž¡¢§Y]moiY›¢¥¥¢¨§Y¢§Y©¨¥¢¢œš¥¥²Y«¢¬¤²Y†ž¢œš«žYœ®¬YšY¡žY¡žš«Y¨ŸY¡žš¥¡Yœš«žY¥ž ¢¬¥š¢¨§eY¡°š«¢§ YšY‹ž©®›¥¢œš§Yšž¦©Y¨Y¨¨¦Y‰«ž¬¢ž§Y{š«šœ¤Yˆ›š¦š`¬Y¬°žž©¢§ Y¨¯ž«¡š®¥g9
9
¡žY›¢Y›²Y¡žY›¢¥¥`¬Yœ«¢¢œ¬Y¨Y«ž¯ž«¬žYœ®¬Y¨Y¡žY©¨©®¥š«Y†ž¢œš«žY©«¨ «š¦YŸš¢¥žY¨§YšY¯¨žY¨ŸYnqfmkeY«š°¢§ Y¡žY¬®©©¨«Y¨ŸY°¨Y}ž¦¨œ«š¢œYžŸžœ¨«¬gYz©©«¨¯š¥Y°¨®¥Y¡š¯žY¬«¢©©žY¨®Y¦¨§ž²Y§žžžY¨Y©š²YŸ¨«Yž±©š§¢§ Yœ¨¯ž«š žY¨Yž§¬Y¨ŸY¦¢¥¥¢¨§¬Y¨ŸY®§¢§¬®«žYz¦ž«¢œš§¬g9
9
¡žY›«¨šž«Y¥ž ¢¬¥š¢¨§Yš¢¦¬Y¨Yž±ž§Y¡žš¥¡Yœ¨¯ž«š žY¨YljY¦¢¥¥¢¨§Y°¡¨Y§¨°Y¥šœ¤Y¢eY°¡¢¥žY›š««¢§ Y¢§¬®«š§œžY¢§®¬«²Y©«šœ¢œž¬Y¬®œ¡Yš¬Yž§²¢§ Yœ¨¯ž«š žY¨§Y¡žY›š¬¢¬Y¨ŸY©«žfž±¢¬¢§ Y¦ž¢œš¥Yœ¨§¢¢¨§¬gY¡¨® ¡Y¡žY¨¯ž«¡š®¥Y¢¬Yž¬¢¦šžY¨Yœ¨¬Yš›¨®Y]jY«¢¥¥¢¨§Y¨¯ž«YšYžœšžeY¡žY|¨§ «ž¬¬¢¨§š¥Y{® žYˆŸŸ¢œžY¡š¬Y¬š¢Y¢Y°¨®¥Yœ®YŸžž«š¥YžŸ¢œ¢¬Y›²Y]jliY›¢¥¥¢¨§Y¨¯ž«Y¡šY©ž«¢¨eYš§Y©«¨›š›¥²Y«ž®œžY¡ž¦YŸ®«¡ž«Y¢§Y¡žYjiY²žš«¬Y›ž²¨§Y¡šg9
The decrypted cipher.txt file:
湕汦湩档湩湯愠挠楲楴慣楦獲⁴整瑳敓慮整䐠浥捯慲獴挠潬敳慲歮桔牵摳祡戠桥湩㐤〶戠汩楬湯椠潰楬楴慣汬⁹楲歳⁹敍楤慣敲挠瑵瑡琠敨栠慥瑲漠敨污桴挠牡敬楧汳瑡潩Ɱ琠睨牡楴杮愠删灥扵楬慣瑡整灭⁴潴搠潯牐獥摩湥⁴慂慲正传慢慭猧猠敷灥湩癯牥慨汵.
吊敨戠摩戠⁹桴楢汬猧挠楲楴獣琠敲敶獲畣獴琠桴潰異慬敍楤慣敲瀠潲牧浡映楡敬湯愠瘠瑯景㔠ⴸ㈴牤睡湩桴畳灰牯⁴景琠潷䐠浥捯慲楴敤敦瑣牯䄠灰潲慶潷汵慨敶猠牴灩数畯⁴潭敮⁹敮摥摥琠慰⁹潦硥慰摮湩潣敶慲敧琠整獮漠業汬潩獮漠湵湩畳敲流牥捩湡윀윀桔牢慯敤敬楧汳瑡潩楡獭琠硥整摮栠慥瑬潣敶慲敧琠ㄳ洠汩楬湯眠潨渠睯氠捡瑩桷汩慢牲湩湩畳慲据湩畤瑳祲瀠慲瑣捩獥猠捵獡搠湥楹杮挠癯牥条湯琠敨戠獡獩漠牰ⵥ硥獩楴杮洠摥捩污挠湯楤楴湯吠潨杵桴癯牥慨汵椠獥楴慭整潴挠獯⁴扡畯⁴ㄤ琠楲汬潩癯牥愠搠捥摡ⱥ琠敨䌠湯牧獥楳湯污䈠摵敧⁴晏楦散栠獡猠楡瑩眠畯摬挠瑵映摥牥污搠晥捩瑩祢␠㌱‰楢汬潩癯牥琠慨⁴数楲摯湡牰扯扡祬爠摥捵桴浥映牵桴牥椠桴〱礠慥獲戠祥湯桴瑡.윊
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[]){
bool repeat = true;
while(repeat){
cout << "Press \"e\" to encrypt and \"d\" to decrypt a .txt file";
char mode = getch();
switch (mode){
case 'e':{
cout << '\n' << '\n' << "ENCRYPTION MODE" << '\n' << '\n';
cout << "Input Path: ";
string inputPath = "";
getline(cin, inputPath);
ifstream plainText(inputPath.c_str());
cout << '\n';
cout << "Output Path: ";
string outputPath= "";
getline(cin, outputPath);
ofstream cipherText(outputPath.c_str());
cout << '\n';
cout << "Shift Value = ";
int shiftValue = 0;
cin >> shiftValue;
cout << '\n';
while (!plainText.eof()){
string plainLine = "";
getline(plainText, plainLine);
int lineLength = plainLine.length();
for (int i=0; i<=lineLength; i++){
int decimalValue[lineLength];
char cipherLine[lineLength];
decimalValue[i] = (int)plainLine[i] + shiftValue;
cipherLine[i] = (char)decimalValue[i];
cipherText << cipherLine[i];
}
cipherText << '\n';
}
plainText.close();
cipherText.close();
remove(inputPath.c_str());
}
break;
case 'd':{
cout << '\n' << '\n' << "DECRYPTION MODE" << '\n' << '\n';
cout << "Input Path: ";
string inputPath = "";
getline(cin, inputPath);
ifstream cipherText(inputPath.c_str());
cout << '\n';
cout << "Output Path: ";
string outputPath= "";
getline(cin, outputPath);
ofstream plainText(outputPath.c_str());
cout << '\n';
cout << "Shift Value = ";
int shiftValue = 0;
cin >> shiftValue;
cout << '\n';
while (!cipherText.eof()){
string cipherLine = "";
getline(cipherText, cipherLine);
int lineLength = cipherLine.length();
for (int i=0; i<=lineLength; i++){
int decimalValue[lineLength];
char plainLine[lineLength];
decimalValue[i] = (int)cipherLine[i] - shiftValue;
plainLine[i] = (char)decimalValue[i];
plainText << plainLine[i];
}
plainText << '\n';
}
plainText.close();
cipherText.close();
remove(inputPath.c_str());
}
break;
}
cout << "Enter \"c\" to continue and \"e\" to exit";
char keystroke = getch();
if (keystroke == 'c'){
repeat = true;
cout << '\n' << '\n';
cin.ignore();
}
else
repeat = false;
}
return EXIT_SUCCESS;
}