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;
}

Edited 6 Years Ago by BobRoss: n/a

I got this with a shift of 3:

Xqiolqfklqj#rq#d#fulwlfdo#iluvw#whvw/#Vhqdwh#Ghprfudwv#forvhg#udqnv#Wkxuvgd|#ehklqg#'793#eloolrq#lq#srolwlfdoo|#ulvn|#Phglfduh#fxwv#dw#wkh#khduw#ri#khdowk#fduh#ohjlvodwlrq/#wkzduwlqj#d#Uhsxeolfdq#dwwhpsw#wr#grrp#Suhvlghqw#Edudfn#Redpd*v#vzhhslqj#ryhukdxo1
Wkh#elg#e|#wkh#eloo*v#fulwlfv#wr#uhyhuvh#fxwv#wr#wkh#srsxodu#Phglfduh#surjudp#idlohg#rq#d#yrwh#ri#8;075/#gudzlqj#wkh#vxssruw#ri#wzr#Ghprfudwlf#ghihfwruv1#Dssurydo#zrxog#kdyh#vwulsshg#rxw#prqh|#qhhghg#wr#sd|#iru#h{sdqglqj#fryhudjh#wr#whqv#ri#ploolrqv#ri#xqlqvxuhg#Dphulfdqv1

Wkh#eurdghu#ohjlvodwlrq#dlpv#wr#h{whqg#khdowk#fryhudjh#wr#64#ploolrq#zkr#qrz#odfn#lw/#zkloh#eduulqj#lqvxudqfh#lqgxvwu|#sudfwlfhv#vxfk#dv#ghq|lqj#fryhudjh#rq#wkh#edvlv#ri#suh0h{lvwlqj#phglfdo#frqglwlrqv1#Wkrxjk#wkh#ryhukdxo#lv#hvwlpdwhg#wr#frvw#derxw#'4#wuloolrq#ryhu#d#ghfdgh/#wkh#Frqjuhvvlrqdo#Exgjhw#Riilfh#kdv#vdlg#lw#zrxog#fxw#ihghudo#ghilflwv#e|#'463#eloolrq#ryhu#wkdw#shulrg/#dqg#suredeo|#uhgxfh#wkhp#ixuwkhu#lq#wkh#43#|hduv#eh|rqg#wkdw1

And this with the decrypt:

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

I get a few stray characters now and again (from carriage returns, etc) but nothing crossing into the extended sets. Are you compiling it under Visual C++ with the character settings (under project/properties/configuration properties/character set) to multi-byte?
(I'm not 100% confident that this will solve the problem but it seemed like it could be a possibility and I thought I would throw it out there)

Edited 6 Years Ago by jonsca: n/a

I got this with a shift of 3:

Xqiolqfklqj#rq#d#fulwlfdo#iluvw#whvw/#Vhqdwh#Ghprfudwv#forvhg#udqnv#Wkxuvgd|#ehklqg#'793#eloolrq#lq#srolwlfdoo|#ulvn|#Phglfduh#fxwv#dw#wkh#khduw#ri#khdowk#fduh#ohjlvodwlrq/#wkzduwlqj#d#Uhsxeolfdq#dwwhpsw#wr#grrp#Suhvlghqw#Edudfn#Redpd*v#vzhhslqj#ryhukdxo1
Wkh#elg#e|#wkh#eloo*v#fulwlfv#wr#uhyhuvh#fxwv#wr#wkh#srsxodu#Phglfduh#surjudp#idlohg#rq#d#yrwh#ri#8;075/#gudzlqj#wkh#vxssruw#ri#wzr#Ghprfudwlf#ghihfwruv1#Dssurydo#zrxog#kdyh#vwulsshg#rxw#prqh|#qhhghg#wr#sd|#iru#h{sdqglqj#fryhudjh#wr#whqv#ri#ploolrqv#ri#xqlqvxuhg#Dphulfdqv1

Wkh#eurdghu#ohjlvodwlrq#dlpv#wr#h{whqg#khdowk#fryhudjh#wr#64#ploolrq#zkr#qrz#odfn#lw/#zkloh#eduulqj#lqvxudqfh#lqgxvwu|#sudfwlfhv#vxfk#dv#ghq|lqj#fryhudjh#rq#wkh#edvlv#ri#suh0h{lvwlqj#phglfdo#frqglwlrqv1#Wkrxjk#wkh#ryhukdxo#lv#hvwlpdwhg#wr#frvw#derxw#'4#wuloolrq#ryhu#d#ghfdgh/#wkh#Frqjuhvvlrqdo#Exgjhw#Riilfh#kdv#vdlg#lw#zrxog#fxw#ihghudo#ghilflwv#e|#'463#eloolrq#ryhu#wkdw#shulrg/#dqg#suredeo|#uhgxfh#wkhp#ixuwkhu#lq#wkh#43#|hduv#eh|rqg#wkdw1

And this with the decrypt:

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

I get a few stray characters now and again (from carriage returns, etc) but nothing crossing into the extended sets. Are you compiling it under Visual C++ with the character settings (under project/properties/configuration properties/character set) to multi-byte?
(I'm not 100% confident that this will solve the problem but it seemed like it could be a possibility and I thought I would throw it out there)

Thank you for your reply. I get the extended characters in the decrypt when I use large shift values in the 100's, e.g. 654.

I am also using Bloodshed.

Edited 6 Years Ago by BobRoss: n/a

Oh ok. I did try to check it with larger values but the program was taking some time to finish. I suppose you could use mod to keep it within the bounds of the character set you want. Anyway, good luck with it!

I don't understand why I need to use mod here if I don't mind the encryption going into the extended character sets.

The process is simple and I don't understand why it doesn't work in reverse with large shift values:

If I add 700 to the decimal value of a character and then subtract that 700 I should get the original decimal value of that character.

Right?

Right?

No, unfortunately. Run yourself a listing of the chars from say 1 to 600, there's a lot of repeats and they don't act like one would think they will.
viz.

char P = 592;
	std::cout <<P<<std::endl;
	int upP = (int)P;
	std::cout <<upP<<std::endl;

No, unfortunately. Run yourself a listing of the chars from say 1 to 600, there's a lot of repeats and they don't act like one would think they will.
viz.

char P = 592;
	std::cout <<P<<std::endl;
	int upP = (int)P;
	std::cout <<upP<<std::endl;

Grrrrrr....

This article has been dead for over six months. Start a new discussion instead.