Download - Caesar Cipher Implementation in C Language
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 1/9
Caesar CipherImplementati
on in C
Language
Submitted By: Mussawir Iqbal, Class no: 655,Submitted to: Sir ZubirAssignment no : Ist
Subject:crytogra!y
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 2/9
Caesar Cipher Implementation in C Language
Caesar Cipher Implementation in C Language
There are three types of cryptography techniques:
1. Secret key Cryptography
2. Public key cryptography
3. Hash Functions
ne si!ple an" basic !etho" to encrypt a !essage is using Caesar#s cipher. $t is a %ery si!ple
for! of encryption& 'here 'e take letters one by one fro! the original !essage an" translate it
into an encrypte" te(t.
Caesar ci!erIn cryptography, a Caesar cipher, also known as Caesar's cipher, the shift
cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption
techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a
letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would
be replaced by D, would become !, and so on. "he method is named after #ulius $aesar, who
used it in his pri%ate correspondence.
"he encryption can also be represented using modular arithmetic by first transforming the
letters into numbers, according to the scheme, A & ', & (,..., ) & *+. !ncryption of a letter by a
shift n can be described mathematically as,
Decryption is performed similarly,
$n this assign!ent& $ 'ill "iscuss ho' to create a C progra! co"e that 'ill encrypt an" "ecrypt
the te(t using Caesars cipher.
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 3/9
Caesar Cipher Implementation in C Language
In this example, on a highle%el, we will do the following-
"he source text that needs to be encrypted is gi%en in lower case. ut if you need to
decrypt the text, it should be gi%en in upper case.
hen it is encrypted, each letter will ha%e its A/0II code increased for tree places. hen
it is decrypted, it will ha%e its code mo%ed toward left.
"he letter 1x2 will be translated into 1A2, the letter 1y2 is transformed into the letter 12,
and the 12 will change into 1$2.
e are keeping this logic %ery simple so that we can understand the code. 4nce you get
the hang of it, comeup with more complex logic to encrypt and decrypt.
"he program will handle only !nglish letters and each input text will not be longer that
one sentence. At the end of the input sentence it should ha%e the marker for end 1.2.
If you don2t ha%e the sense marker, the longest sentence is ('*5 letters long. "his is some
form of protection, which would pre%ent the user to input the sentence that would o%er
populate sie of the program.
"he numbers in the input will not be changed.
"he blank symbol or any non letter symbol will not be changed.
C Source Code "#amle $or Ceaser Ci!er:
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 4/9
Caesar Cipher Implementation in C Language
6include 7stdio.h8
6include 7ctype.h8
6define 9A:0I)! ('*5
%oid encrypt;char<=>
%oid decrypt;char<=>
int menu;=>
int
main;%oid=
?
char c,
choice@*,
s@9A:0I)!>
while;(=
?
menu;=>
gets;choice=>
if;;choice@'&&BeB=CC;choice@'&&B!B==
?
puts;Input text to encrypt8=>
gets;s=>
encrypt;s=>
E
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 5/9
Caesar Cipher Implementation in C Language
else if;;choice@'&&BdB=CC;choice@'&&BDB==
?
puts;Input text to decrypt8=>
gets;s=>
decrypt;s=>
E
else
break>
E
return '>
E
%oid encrypt;char<str=
?
int n&'>
char <p&str,
q@9A:0I)!>
while;<p=
?
if;islower;<p==
?
if;;<p8&BaB=;<p7BxB==
q@n&toupper;<p G ;char=3=>
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 6/9
Caesar Cipher Implementation in C Language
else if;<p&&BxB=
q@n&BAB>
else if;<p&&ByB=
q@n&BB>
else
q@n&B$B>
E
else
?
q@n&<p>
E
nGG> pGG>
E
q@nGG&BH'B>
puts;q=>
E
%oid decrypt;char<str=
?
int n&'>
char <p&str,
q@9A:0I)!>
while;<p=
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 7/9
Caesar Cipher Implementation in C Language
?
if;isupper;<p==
?
if;;<p8&BDB=;<p7&B)B==
q@n&tolower;<p ;char=3=>
else if;<p&&BAB=
q@n&BxB>
else if;<p&&BB=
q@n&ByB>
else
q@n&BB>
E
else
?
q@n&<p>
E
nGG> pGG>
E
q@nGG&BH'B>
puts;q=>
E
int menu;=
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 8/9
Caesar Cipher Implementation in C Language
?
puts;"o encrypt, input e or !Hn=>
puts;"o decrypt, input d or DHn=>
puts;"o exit, input any other letterHn=>
puts;our choice-8Hn=>
return '>
E
Code Analysis
"he main function does the following-
First we include the stdio.h and ctype.h
"hen we create a macro for maximum sentence sie. In this example, it is ('*5.
"here are a few declarations to reser%e place for things that we use in our code.
hile loop will repeat until user inputs proper letter to stop the program. In the while loop, we call the function menu;=, which will display the menu to the user.
/ext, it does the following-
hen you input the letter, function gets;= reads your choice. According to the user input
appropriate function would be called.
4ne function encrypts the text, and the other function decrypts it.
First function gets one string into it, and modifies it. After that, we are changing each
letter according to the rule we need to apply.
"he pointer q is a helper to read the original string, and the q is used to store the output.
tolower;= will transform the letter into lower case. toupper;= will transform the letter into
upper case.
Function gets;= is used to read the input string from user.
/ow, to the function encrypt-
8/16/2019 Caesar Cipher Implementation in C Language
http://slidepdf.com/reader/full/caesar-cipher-implementation-in-c-language 9/9
Caesar Cipher Implementation in C Language
"o encrypt, this code will mo%e letters to a different offset by 3 spaces in A0$II table.
Also, at the end of alphabet you wrap around and replace- x, y and , with- a, b and c.
Instead of char type, use wcahrJt symbols that could be good for languages other than
!nglish. "here are usually similar functions that will work with two byte letters. 0ometimesit is enough to use one additional w.
As an additional exercise, modify the abo%e $ sample code to include different offsets in one
sentence itself.
hen we talk about breaking $aesars cipher, first algorithm that could be applied is statistical
decryption. For each language, there are usual frequencies of each letter and they could be used
to figure out the encrypted text without getting the key. 4n a related subKect, you should also
explore how Ligener2s cipher works.
Again, it is %ery easy to break the encrypted text generated by this example. "he abo%e code is
gi%en only for learning purpose to understand how this works.