Every time you make a financial transaction online or deposit/withdraw money from your bank account using an ATM cryptography is what prevents malicious crackers from stealing your credit card number or extracting all the money from your bank account to theirs. If it weren't for cryptography, the world may very well be in chaos.
This tutorial and the ones following it will introduce the basic concepts of cryptography and show you how to code them using the C/C++ programming language. In this tutorial you will learn about the basic shift cipher, commonly called the Caesar Shift Cipher, and how you can program a cryptography program using it in C/C++. Now let us begin, and also how it can be broken.
The most basic of all cipher is known as the Caesar shift cipher, after its creator, Julius Caesar. This cipher was used by the Romans during many of their wars to deliver secret messages to each other.
The basic concept is as follows. Line up all the letters of the regular alphabet with the same alphabet shifted so many characters as follows:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
||||||||||||||||||||||||||||||||||||||||
DEFGHIJKLMNOPQRSTUVWXYZABC
The shifted alphabet is called your cipher alphabet. This is Caesar shift cipher of 3, because we shifted the alphabet 3 letters, thus A,B, and C are at the end. This is how it works as follows. Say I have the message:
SEND TROOPS
To encrypt this message using the Caesar cipher your merely transpose the letters in the regular alphabet with the letter below it in the cipher alphabet. So "S" becomes "V" and "E" becomes "H" and so on until you arrive at the ciphertext:
VHQG WURRSV
Wow! will you look at that! How easy was that? And you can do this for messages of any length. Now since there are 26 letters in the English alphabet, and there are 26 different ways to shift the letters and thus 26 different distinct ciphers, but if you don't limit yourself to just shifting the letters, and you allow yourself to arrange the letters in any way then there are 403,291,461,126,605,635,584,000,000 rearrangements, and thus that many ciphers. Now to dip into some C++ code. Here is the modernized C++ implementation of the Caesar shift Cipher:
#include <iostream>
#include <string>
using namespace std;
int main(int argc,char** argv) {
string message;
int shift;
cout> shift;
if(shift > 26) {
shift = shift%26;
}
if(shift == 0) {
shift = 1;
}
for(int i = 0; i
#include <iostream>
#include <string>
using namespace std;
int main(int argc,char** argv) {
cout<<"Enter Ciphertext: "<<endl;
string ciphertext;
getline(cin,ciphertext);
int i = 0;
char* temp = new char[ciphertext.length()];
temp = (char*)ciphertext.c_str();
char c;
while(i != 26) {
for(int j = 0; j < ciphertext.length(); j++) {
temp[j] = (temp[j]-'A'+1)%26)+'A' ;
}
cout<<"Is this the plaintext: "<<endl;
cout<<temp<<endl;
cout<<"Press Q if yes, or any key to continue.."<<endl;
c = toupper(getch());
if(c == 'Q' || c == 27)
return 0;
system("cls");
i++;
}
return 0;
}
I hope this tutorial has given a valid introduction to cryptography. If you have any questions or comments please email me at bitshift2002@yahoo.com. Enjoy!;)
This article was originally written by bitshift |