This is the first article of a multi-part series. It serves the purpose of introducing you to cryptography. I cannot promise an easy ride through this journey, but it will be a comprehensible and amazing one. As much as we’ll get into this, we’ll realize how complex cryptography can be. But that shouldn’t scare you away. I start by assuming that the reader has no experience with cryptography but has a bit of coding experience.

If that doesn’t describe you, it’s not a problem per se. You can skip the code parts and still understand the rest of the series. So let’s move on — we have no time to waste!

**What Will I Learn Reading This Series?**

By reading this multi-part article series, you should become familiar with cryptography. We will get into a brief explanation of the whole process before moving on to real-life examples. Then we will venture into the coding of several quick-and-easy encryption and decryption algorithms in ANSI C. Therefore, you will be able to see its results with your own eyes. We learn the best by doing it!

As a closure to the series I will try to challenge you to study cryptography. Hopefully, I might spark and bring to life some of those hidden desires. You will find a few of the most influential and best literature on cryptography as further reading and reference at the epilogue of this series. You will also get to know the latest algorithms that are currently used in various areas. Of course, their complexity is unimaginable compared to the ones we will code in C.

{mospagebreak title=The Study of Cryptography}

Nowadays we can say that cryptography is part of computer science due to the explicit formulas, algebra and/or calculation schemes. As a science, cryptography plays the role of securing communication between two parties. A large part of it is encryption and decryption but in the past few years its range has been dramatically expanded. In a nutshell, if it is de/encryption then the information gets "encrypted" and then "decrypted" by knowing a secret "key" or password.

Like I said, cryptography’s utilization has been expanded and it isn’t uncommon at all to find authentication, digital signature verification and such. Basically this way the receiver is "verified" to determine if s/he is indeed eligible to view the secured information. This is another usage of cryptography that plays a huge role in our era of computing.

In any form of communication the information is sent by a "sender" and received by a "receiver." To secure the information it can be encrypted by a specific mathematic algorithm via a specified "key." As soon as the information arrives at the receiver, if s/he knows the required key then the process can be reversed. Let’s introduce two terms: "plaintext" means un-encrypted data, while "ciphertext" means encrypted data.

Almost all of the legacy (and simple) encryption algorithms work on a user-specified key. That means that technically anybody who knows the key is able to decrypt and regenerate the secured information into its original form — plaintext. If the "attacker" doesn’t knows the exact key then the whole decryption process requires a lot of effort but mostly time and resources. More about this later on.

**Real-World Encryption Algorithm Examples**

In this section we will venture into the coding of encryption algorithms. All of these examples involve working with files. So you might want to create a test text document. It is up to you.

Keep in mind that the encryption algorithms described in this article are solely for **educational purposes**. You should not rely on them for top-priority files and/or data. However, the author guarantees that the algorithms presented in this article will** not** do any harm to your computer. They do not contain any virus, trojan, worm, spyware, malware, or anything that’s malicious.

{mospagebreak title=First Example: Byte Adder Algorithm}

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:

while (s.get(origbyte))

{

byte=(int)origbyte+digits;

d.put(byte);

}

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¦—ª¦R˜›ž—R˜¡¤R¦—¥¦› ™R¢§¤¢¡¥—¥`Rz¡¢—R«¡§R— œ¡«S?<?<v—¨—

ž¡¢—¤R…š—–`R†—¥¦^R¦—¥¦^R¦—¥¦`R¥–˜¥–˜¥–˜`Rcdefghijk

Here is the full code:

#include <fstream.h> //or <fstream> and ‘using namespace std;’

#include <stdio.h>

#include <string.h>

char srcfile[30], dstfile[30];

int digits;

void main()

{

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: ");

int opt;

scanf("%d",&opt);

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;

while (s.get(origbyte))

{

byte=(int)origbyte+digits;

d.put(byte);

}

s.close(); d.close();

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.

{mospagebreak title=Second Example: Variation of the Byte-Adder}

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!