The main idea of this algorithm is the same as that of the aforementioned Byte-Adder. However, instead of adding/subtracting a user-specified numeric value we will generate, via a quick mathematical formula, an integer from the user-specified string. Therefore, we will work again with two text files and a character string that stands for the password. Here's the algorithm: (in this case— ‘-' for encryption or ‘+' for decryption) for (int i=0;i<len;i++) total += pw[i]; c -= pw[len-1]*(total / len); Variables: "len" stands for the length of the password; "pw" represents the given password string; and "c" will be the newly generated byte as we pass through the content of the file. For the exact same input text file check out the generated output: Í ÍÍ!%!ÍÍÍ! !Í" ÛÍõÍ&"Í&η·ñ#Í ÛÍ !ÙÍ! !ÙÍ! !ÛÍ ÛÍÞßàáâãäåæ The password was: "longpassword123" in the above example. Now, let's see the complete program: #include <fstream.h> //or <fstream> and ‘using namespace std;' #include <stdio.h> #include <string.h> char srcfile[30], dstfile[30], pw[30]; void main() { printf("Source file: "); scanf("%s",srcfile); printf("nDestination file: "); scanf("%s",dstfile); printf("nPassword: "); scanf("%s",pw); printf("nPress 1.> if Encryption or 2.> if Decryption: "); int opt; char c; scanf("%d",&opt); int total=0; int len=strlen(pw); for (int i=0;i<len;i++) total += pw[i]; ifstream s(srcfile); ofstream d(dstfile); if (!s || !d) printf("nERROR: Couldn't open one of the files!"); while (s.get(c)) { if (opt==1) d << c - pw[len-1]*(total / len); else d << c + pw[len-1]*(total / len); } s.close(); d.close(); printf("nAll done."); } You can download its compiled version in executable format and also the source by clicking on the archive button below. It is archived in RAR. It's in working condition. I have purposefully reduced the complexity of these algorithms to make them "obviously simple," easy and comprehensible for a beginner too. My intention with this introductory article was to familiarize you, the reader, with encryption algorithms rather than writing about top-notch algorithms. I am aware that there are zillions of other variations of the aforementioned algorithms and different ways to code it — more efficiently, more secure, etc. — but for now, our top priority was to understand what cryptography is all about. Conclusions I've shown you two possible de/encryptions. You should be able to use this mindset and work out a few variations of your own, too. Just use your imagination. It's that simple. Play with encryption. That's the beauty of it — you can actually see your results! You encrypt it… you get gibberish. You decrypt it… you get the original file. It works! Jokes aside, unfortunately, I am running out of space right now. However, be sure to stick around for the second part of this series. There I will show you one of the most popular encryption algorithms. It's the XOR. Yes, we will XOR. In the next part I will also explain three unique ways to break an XOR-based encryption. None of those are secret but very few users actually grab a book to do the research to find out how. Obviously, use them just for educational purposes. And to whet your appetite... there will be a third part to this series too! In that article I will talk about security and reliability. I will show you some of the top-notch encryption algorithms and security solutions that currently dominate the cryptographic landscape. I will also spark some of your hidden desires toward this and motivate you to read some of the best literature that's available on this topic. Expect to see an awesome compilation of further reading and references. Until then -- keep coding, learning, reading and researching. See you at part two!
blog comments powered by Disqus |
|
|
|
|
|
|
|