Caesar Cipher in Cryptography
Caesar Cipher in Cryptography
Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI
Advantages:
• Easy to implement and use thus, making suitable for beginners
to learn about encryption.
• Can be physically implemented, such as with a set of rotating
disks or a set of cards, known as a scytale, which can be useful
in certain situations.
• Requires only a small set of pre-shared information.
• Can be modified easily to create a more secure variant, such as
by using a multiple shift values or keywords.
Disadvantages:
• It is not secure against modern decryption methods.
• Vulnerable to known-plaintext attacks, where an attacker has
access to both the encrypted and unencrypted versions of the
same messages.
• The small number of possible keys means that an attacker can
easily try all possible keys until the correct one is found, making
it vulnerable to a brute force attack.
• It is not suitable for long text encryption as it would be easy to
crack.
• It is not suitable for secure communication as it is easily broken.
• Does not provide confidentiality, integrity, and authenticity in a
message.
Features of caesar cipher:
1. Substitution cipher: The Caesar cipher is a type of substitution
cipher, where each letter in the plaintext is replaced by a letter
some fixed number of positions down the alphabet.
2. Fixed key: The Caesar cipher uses a fixed key, which is the
number of positions by which the letters are shifted. This key is
known to both the sender and the receiver.
3. Symmetric encryption: The Caesar cipher is a symmetric
encryption technique, meaning that the same key is used for
both encryption and decryption.
4. Limited keyspace: The Caesar cipher has a very limited
keyspace of only 26 possible keys, as there are only 26 letters
in the English alphabet.
5. Vulnerable to brute force attacks: The Caesar cipher is
vulnerable to brute force attacks, as there are only 26 possible
keys to try.
6. Easy to implement: The Caesar cipher is very easy to implement
and requires only simple arithmetic operations, making it a
popular choice for simple encryption tasks.
Rules for the Caesar Cipher:
1. Choose a number between 1 and 25. This will be your “shift”
value.
2. Write down the letters of the alphabet in order, from A to Z.
3. Shift each letter of the alphabet by the “shift” value. For
example, if the shift value is 3, A would become D, B would
become E, C would become F, and so on.
4. Encrypt your message by replacing each letter with the
corresponding shifted letter. For example, if the shift value is 3,
the word “hello” would become “khoor”.
5. To decrypt the message, simply reverse the process by shifting
each letter back by the same amount. For example, if the shift
value is 3, the encrypted message “khoor” would become
“hello”.
The Caesar Cipher is a type of shift cipher. Shift Ciphers work by
using the modulo operator to encrypt and decrypt messages. The
Shift Cipher has a key K, which is an integer from 0 to 25. We will
only share this key with people that we want to see our message.
How to Encrypt:
For every letter in the message M :
1. Convert the letter into the number that matches its order in the
alphabet starting from 0, and call this number X.
( A=0, B=1, C=2, ...,Y=24, Z=25)
2. Calculate: Y = (X + K) mod 26
3. Convert the number Y into a letter that matches its order in the
alphabet starting from 0.
So, after applying the Shift Cipher with key K=19 our message text
"KHAN" gave us cipher text "DATG".
How to decrypt:
For every letter in the cipher text C :
1. Convert the letter into the number that matches its order in the
alphabet starting from 0, and call this number Y.
(A=0, B=1, C=2, ..., Y=24, Z=25)
2. Calculate: X= (Y - K) mod 26
3. Convert the number X into a letter that matches its order in the
alphabet starting from 0.
(A=0, B=1, C=2, ..., Y=24, Z=25)
Our friend now decodes the message using our agreed upon key
K=19. As follows:
So, after decrypting the Shift Cipher with key K=19 our friend
deciphers the cipher text "DATG" into the message text "KHAN".
Encryption Technique
For the encryption process let us consider the following example:
PlainText: "instruments"
After Split: 'in' 'st' 'ru' 'me' 'nt' 'sz'
1. Pair cannot be made with same letter. Break the letter in single and add a
bogus letter to the previous letter.
Plain Text: “hello”
After Split: ‘he’ ‘lx’ ‘lo’
Here ‘x’ is the bogus letter.
2. If the letter is standing alone in the process of pairing, then add an extra
bogus letter with the alone letter
Plain Text: “helloe”
AfterSplit: ‘he’ ‘lx’ ‘lo’ ‘ez’
Here ‘z’ is the bogus letter.
Rules for Encryption:
• If both the letters are in the same column: Take the letter below
each one (going back to the top if at the bottom).
For example:
Diagraph: "me"
Encrypted Text: cl
Encryption:
m -> c
e -> l
•
•
• If both the letters are in the same row: Take the letter to the right
of each one (going back to the leftmost if at the rightmost position).
For example:
Diagraph: "st"
Encrypted Text: tl
Encryption:
s -> t
t -> l
•
•
• If neither of the above rules is true: Form a rectangle with the
two letters and take the letters on the horizontal opposite corner of
the rectangle.
For example:
Diagraph: "nt"
Encrypted Text: rq
Encryption:
n -> r
t -> q
•
•
For example:
Decryption:
Decryption is performed by going to the row in the table corresponding to
the key, finding the position of the ciphertext letter in this row, and then
using the column’s label as the plaintext. For example, in row A (from
AYUSH), the ciphertext G appears in column G, which is the first plaintext
letter. Next, we go to row Y (from AYUSH), locate the ciphertext C which is
found in column E, thus E is the second plaintext letter.
A more easy implementation could be to visualize Vigenère algebraically
by converting [A-Z] into numbers [0–25].
Encryption
The plaintext(P) and key(K) are added modulo 26.
Ei = (Pi + Ki) mod 26
Decryption
Di = (Ei - Ki) mod 26
Note: Di denotes the offset of the i-th character of the plaintext. Like offset
of A is 0 and of B is 1 and so on.
Autokey Cipher | Symmetric Ciphers
Autokey Cipher is a polyalphabetic substitution cipher. It is closely related
to the Vigenere cipher but uses a different method of generating the key. It
was invented by Blaise de Vigenère in 1586. In general, more secure than
the Vigenere
cipher.
Example-1:
Plaintext = "HELLO"
Autokey = N
Ciphertext = "ULPWZ"
Example-2:
Plaintext = "GEEKSFORGEEKS"
Autokey = P
Ciphertext = "VKIOCXTFXKIOC"
In this cipher, the key is a stream of subkeys which is used to encrypt the
corresponding character in the
plaintext.
Let's encrypt:
Plain Text(P) : H E L L O
Corresponding Number: 7 4 11 11 14
Key(K) : N H E L L
Corresponding Number: 13 7 4 11 11
---------------------
Applying the formula: 20 11 15 22 25
Corresponding
Letters are : U L P W Z
Let's decrypt:
Cipher Text(C) : U L P W Z
Key(K) : N H E L L
---------------------
Applying the formula: H E L L O
0 1 2 3 4 5 6 7 8 9
K L M N O P Q R S T
10 11 12 13 14 15 16 17 18 19
U V W X Y Z
20 21 22 23 24 25
The relation between the key and plain text: In this algorithm, the length of the key
should be equal to that of plain text.
Examples:
Input: Message = HELLO, Key = MONEY
Output: Cipher – TSYPM, Message – HELLO
Explanation: Part 1: Plain text to Ciphertext Plain text — H E L L O ? 7 4 11 11
14 Key — M O N E Y ? 12 14 13 4 24 Plain text + key ? 19 18 24 15 38 ? 19 18
24 15 12 (= 38 – 26) Cipher Text ? T S Y P M Part 2: Ciphertext to
Message Cipher Text — T S Y P M ? 19 18 24 15 12 Key — M O N E Y? 12 14
13 4 24 Cipher text – key ? 7 4 11 11 -12 ? 7 4 11 11 14 Message ? H E L L
O Input: Message = SAVE, Key = LIFE Output: Cipher – DIAI Message – SAVE
Security of One-Time Pad
• If any way cryptanalyst finds these two keys using which two plaintext
are produced but if the key was produced randomly, then the
cryptanalyst cannot find which key is more likely than the other. In fact,
for any plaintext as the size of ciphertext, a key exists that produces that
plaintext.
• So if a cryptanalyst tries the brute force attack(try using all possible
keys), he would end up with many legitimate plaintexts, with no way of
knowing which plaintext is legitimate. Therefore, the code is
unbreakable.
• The security of the one-time pad entirely depends on the randomness of
the key. If the characters of the key are truly random, then the characters
of the ciphertext will be truly random. Thus, there are no patterns or
regularities that a cryptanalyst can use to attack the ciphertext.
Advantages
• One-Time Pad is the only algorithm that is truly unbreakable and can be
used for low-bandwidth channels requiring very high security(ex. for
military uses).
Disadvantages
• There is the practical problem of making large quantities of random keys.
Any heavily used system might require millions of random characters on
a regular basis.
• For every message to be sent, a key of equal length is needed by both
sender and receiver. Thus, a mammoth key distribution problem exists.
Hill Cipher
D
DamanAnand
• Read
• Discuss
• Courses
• Practice
Hill cipher is a polygraphic substitution cipher based on linear algebra.Each
letter is represented by a number modulo 26. Often the simple scheme A = 0, B =
1, …, Z = 25 is used, but this is not an essential feature of the cipher. To encrypt a
message, each block of n letters (considered as an n-component vector) is
multiplied by an invertible n × n matrix, against modulus 26. To decrypt the
message, each block is multiplied by the inverse of the matrix used for
encryption.
The matrix used for encryption is the cipher key, and it should be chosen
randomly from the set of invertible n × n matrices (modulo 26).
Examples:
Encryption
We have to encrypt the message ‘ACT’ (n=3).The key is ‘GYBNQKURP’ which can
be written as the nxn matrix:
The message ‘ACT’ is written as vector:
Decryption
To decrypt the message, we turn the ciphertext back into a vector, then simply
multiply by the inverse matrix of the key matrix (IFKVIVVMI in letters).The
inverse of the matrix used in the previous example is:
For the previous Ciphertext ‘POH’:
Encryption
It uses modular arithmetic to transform the integer that each plaintext letter
corresponds to into another integer that correspond to a ciphertext letter.
The encryption function for a single letter is
E ( x ) = ( a x + b ) mod m
modulus m: size of the alphabet
a and b: key of the cipher.
a must be chosen such that a and m are coprime.
Decryption
In deciphering the ciphertext, we must perform the opposite (or inverse)
functions on the ciphertext to retrieve the plaintext. Once again, the first
step is to convert each of the ciphertext letters into their integer values. The
decryption function is
D ( x ) = a^-1 ( x - b ) mod m
a^-1 : modular multiplicative inverse of a modulo m. i.e., it
satisfies the equation
1 = a a^-1 mod m .
To find a multiplicative inverse
We need to find a number x such that:
If we find the number x such that the equation is true, then x is the inverse
of a, and we call it a^-1. The easiest way to solve this equation is to search
each of the numbers 1 to 25, and see which one satisfies the equation.
[g,x,d] = gcd(a,m); % we can ignore g and d, we dont need them
x = mod(x,m);
If you now multiply x and a and reduce the result (mod 26), you will get the
answer 1. Remember, this is just the definition of an inverse i.e. if a*x = 1
(mod 26), then x is an inverse of a (and a is an inverse of x)
Example:
Transposition
• Discuss
• Courses
• Practice
Given a plain-text message and a numeric key, cipher/de-cipher the given text
using Rail Fence algorithm.
The rail fence cipher (also called a zigzag cipher) is a form of transposition
cipher. It derives its name from the way in which it is encoded.
Examples:
Encryption
Input : "GeeksforGeeks "
Key = 3
Output : GsGsekfrek eoe
Decryption
Input : GsGsekfrek eoe
Key = 3
Output : "GeeksforGeeks "
Encryption
Input : "defend the east wall"
Key = 3
Output : dnhaweedtees alf tl
Decryption
Input : dnhaweedtees alf tl
Key = 3
Output : defend the east wall
Encryption
Input : "attack at once"
Key = 2
Output : atc toctaka ne
Decryption
Input : "atc toctaka ne"
Key = 2
Output : attack at once
Encryption
In a transposition cipher, the order of the alphabets is re-arranged to obtain the
cipher-text.
Decryption
As we’ve seen earlier, the number of columns in rail fence cipher remains equal
to the length of plain-text message. And the key corresponds to the number of
rails.
• Hence, rail matrix can be constructed accordingly. Once we’ve got the
matrix we can figure-out the spots where texts should be placed (using
the same way of moving diagonally up and down alternatively ).
• Then, we fill the cipher-text row wise. After filling it, we traverse the
matrix in zig-zag manner to obtain the original text.
Implementation:
Let cipher-text = “GsGsekfrek eoe” , and Key = 3
* _ _ _ * _ _ _ * _ _ _ *
_ * _ * _ * _ * _ * _ *
_ _ * _ _ _ * _ _ _ * _
Given a plain-text message and a numeric key, cipher/de-cipher the given
text using Columnar Transposition Cipher
The Columnar Transposition Cipher is a form of transposition cipher just
like Rail Fence Cipher. Columnar Transposition involves writing the
plaintext out in rows, and then reading the ciphertext off in columns one by
one.
Examples:
Encryption
Input : Geeks for Geeks
Key = HACK
Output : e kefGsGsrekoe_
Decryption
Input : e kefGsGsrekoe_
Key = HACK
Output : Geeks for Geeks
Encryption
Input : Geeks on work
Key = HACK
Output : e w_eoo_Gs kknr_
Decryption
Input : e w_eoo_Gs kknr_
Key = HACK
Output : Geeks on work
Encryption
In a transposition cipher, the order of the alphabets is re-arranged to obtain
the cipher-text.
1. The message is written out in rows of a fixed length, and then read
out again column by column, and the columns are chosen in some
scrambled order.
2. Width of the rows and the permutation of the columns are usually
defined by a keyword.
3. For example, the word HACK is of length 4 (so the rows are of
length 4), and the permutation is defined by the alphabetical order
of the letters in the keyword. In this case, the order would be “3 1
2 4”.
4. Any spare spaces are filled with nulls or left blank or placed by a
character (Example: _).
5. Finally, the message is read off in columns, in the order specified
by the keyword.
Decryption
1. To decipher it, the recipient has to work out the column lengths by
dividing the message length by the key length.
2. Then, write the message out in columns again, then re-order the
columns by reforming the key word.