26279 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
WordPress warns of
mass Tumblr
defections after
Yahoo! deal
Flailing QLogic"s
boss gives up CEO,
president gigs
Rogue Nokia
splinter cell drops
its Jolla phone
A-BOMB
They WANT to EAT
YOUR COMPUTER -
welcome your ANT
overlords
Canadian regulators
welcome US Bitcoin
refugees with open
arms
Securo-boffins
uncover new GLOBAL
cyber-espionage
operation
It! Started! With!
A! GIF!... Yahoo!
Actually! Buys!
Tumblr! for! $1bn!
Petshop iPad fanboi
charged with
filming up young
model"s skirt
Amazon
cloud-watcher shows
some love for
Microsoft"s Azure
Crack Army pilot to
be first PROPER
British astronaut
IN SPAAAACE
Slashdot
Narrowing Down When
Humans Began
Hurling Spears
What Professors Can
Learn From "Hard
Core" MOOC Students
FDA To Decide Fate
of Triclosan,
Commonly Used In
Antibacterial Soaps
Cyber Attack From
Inside India Hits
Pakistan Government
Jolla Announces
First Meego Phone
Available By End
2013
Over 100 Hours of
Video Uploaded To
YouTube Every
Minute
Interviews: McAfee
Says House Fire Was
No Accident
Yahoo Pinkie-Swears
It Won"t Ruin
Tumblr
Uptick In Whooping
Cough Linked To
Subpar Vaccines
Dark Matter, WIMPS,
and NASA"s Alpha
Magnetic
Spectrometer Data
Article viewer

Introduction to Cryptography Part 1



Written by:dimport
Published by:CodeX
Published on:2003-06-21 07:19:46
Topic:C++
Search OSI about C++.More articles by dimport.
 viewed 16167 times send this article printer friendly

Digg this!
    Rate this article :
In todays modern society, where everything is fast-paced and digital. Many governments, companies, and citizens rely on cryptography to keep the personal business private....

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

Did you like this article? There are hundreds more.

Comments:
Nightscript
2008-07-02 18:20:29
This is a pretty good read, considering the ceasar shift is easily figured out and broken maybe you should add some 'innovative' ideas to you code? It would definitely make it difficult.

I personally use a flip of the alphabet at the 13th letter and shift as needed there. but you know..to each his how

good article
CodeX
2009-02-08 22:04:24
Technically I think a ceasar cypher is a shift of 3 to encrypt and so it doesnt really have a key, but as its a specific case of a shift cypher it is true that there are 26 unique possible shift values, although shifting it 26 places would leave you where you started which isn't very good. Another thing is that as an example of a monoalphabetic substitution cypher, and for any cypher of this kind (with the letters A-Z) there can only be 26! possible keys or character mappings (26 factorial which is where the 4*10^26 comes from). Also there isn't any mention of how these cyphers are broken, which if it was technically a Caesar cypher and you know it then all you have to do is a reverse Caesar cypher, if its only a shift cypher then you can shift it 1 to 25 places to see which shift yeilds something legible (there are some cases where shifting cyphertext in two or more ways all yield something legible but that is usually only a for a word or two at a time). If you only know that's the cypertext was produced by some monoalphabetic cypher then going through all the possible keys would take far too long to even consider, so you'd have to take another path such as Frequency analysis.

Another thing, I think the simplest cypher could be rot13 (another shift cypher) as its encryption and decryption process are the same, also what has happened to the code? If you want to keep things secret from people (even without any knowledge on cryptography) this really isn't something to use as even a schoolboy can work out that you can use frequency analysis, but as a starting place for cryptography monoalphabetic cyphers are the place to start! *rant*
CodeX
2009-02-17 05:53:56
also here are example Caesar cypher encode and decode functions that have conventional uppercase cyphertext output and lowercase plaintext output (they accept any case as input):
cypher
void encode(char *plaintext){
    char *c = plaintext;
    char tmp;
    while(*c){
        tmp = (*c & 0xDF);
        if(tmp>='A' && tmp<='Z'){
            *c = ((tmp-'A'+3)%26)+'A';
        }
        c++;
    }
}

decypher
void decode(char *cyphertext){
    char *c = cyphertext;
    char tmp;
    while(*c){
        tmp = (*c | 32);
        if(tmp>='a' && tmp<='z'){
            *c = (tmp>='d')? tmp-3 : 'x'+(tmp-'a');
        }
        c++;
    }
}
Anonymous
2009-04-22 09:09:31
I think the simplest cypher could be rot13 as its encryption and decryption process are the same, also what has happened to the code? If you want to keep things secret from people this really isn't something to use as even a schoolboy can work out that you can use frequency analysis, but as a starting place for cryptography monoalphabetic cyphers are the place to start! flash games
ObatAsamUrat
2011-06-16 07:10:13
Oh my gosh health benefits! a wonderful document person. Thanks a lot Even so We are suffering from trouble with your really simply syndication . Don’t recognize precisely why Can not enroll in the idea. Perhaps there is any individual receiving the exact same really simply syndication dilemma? Anybody who is aware of please answer. Thnkx
kaos distro
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
harry
Blog entry for Thu 28th Sep 12pm on Thu 28th Sep 12pm
Hi In school i want to net send my mates but hide who its coming off any ideas. no programs though as the machine in school sets off an alarm if it detects any batch files etc... thanks Harry


     
Your Ad Here
 
Copyright Open Source Institute, 2006