In a nutshell, here is the encryption process: we will open the file and linearly add N number of bytes to each of its bytes. The decryption process is reversed: we will subtract N number of bytes from each byte. Throughout this example the number N stands for the key. It might be a user-specified "whole number" or can be generated from a specific string (we are going to discuss this variation in the second example later on). To make this example even simpler we will request an int' for N.
The algorithm is quite easy. Check it out:
In the above algorithm string "s" stands for source and "d" for destination. Digits is a numeric integer; it represents the numbers of bytes that are going to be added. The algorithm is the same for decryption the only difference is that we change the sign of the "digits." If it was, say, 50 for encryption, then it is going to be -50 for decryption. Easy.
Now let's run our code to encrypt the following text file with N=50:
This is a text file for testing purposes. Hope you enjoy!
Developer Shed. Test, test, test. sdfsdfsdf. 123456789
The encrypted outcome looks like this:
₯R₯R"R¦ͺ¦RR‘€R¦₯¦ R’§€’‘₯₯`Rz‘’R«‘§R ‘«S?<?<v¨
Here is the full code:
#include <fstream.h> //or <fstream> and using namespace std;'
char srcfile, dstfile;
printf("Source file: "); scanf("%s",srcfile);
printf("nDestination file: "); scanf("%s",dstfile);
printf("nN= "); scanf("%d",&digits);
printf("nPress 1.> if Encryption or 2.> if Decryption: ");
if (opt==2) digits*=-1; // or digits=-digits;
ifstream s(srcfile, ios::binary);
ofstream d(dstfile, ios::binary);
if (!s || !d)
printf("nERROR: Couldn't open one of the files! ");
char origbyte, byte;
printf("nAll done. ");
If you want to download the compiled executable and the above source instead of copying, pasting and compiling it yourself, just click on the attached download button.
blog comments powered by Disqus