Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Cryptography Lab

Lab File

Name: Yarooq Anwar

Class: BSCS – B (7th Semester)

Seat No: B19102159


Caesar Cypher
Introduction

The Caesar cipher is one of the oldest and simplest encryption techniques. It is named
after Julius Caesar, who used it to communicate with his officials. The cipher involves
shifting each letter of the plaintext by a fixed number of positions down the alphabet
to create the ciphertext.

Formula

The Caesar cipher can be represented using the following formula:

C = (P + K) mod 26

where C is the ciphertext, P is the plaintext, K is the key (i.e., the number of positions
to shift), and mod 26 ensures that the result stays within the range of the alphabet
(i.e., 0-25).

Description

To encrypt a message using the Caesar cipher, each letter in the plaintext is replaced
with a letter that is K positioned down the alphabet. For example, if K = 3, then A is
replaced with D, B is replaced with E, and so on. The resulting string of letters is the
ciphertext. To decrypt the ciphertext, the receiver simply shifts each letter K positions
up the alphabet.

Limitations

While the Caesar cipher was effective in ancient times, it is now easily cracked by
modern cryptanalysis techniques. The small number of possible keys (i.e., 26) means
that an attacker can easily try all possible keys until the correct one is found.
Additionally, the frequency distribution of letters in the English language can be used
to further narrow down the possible keys.

Conclusion

Despite its limitations, the Caesar cipher remains an important historical artifact and
a valuable teaching tool for introducing students to the concept of encryption. Its
simplicity and ease of implementation make it a useful building block for more
complex encryption algorithms.
Code

Output
Vignere Cypher
Introduction

The Vigenere cipher is a polyalphabetic substitution cipher that was invented by the
French cryptographer Blaise de Vigenere in the 16th century. It is an extension of the
Caesar cipher and uses a series of interwoven Caesar ciphers to encrypt a message.

Formula

The Vigenere cipher can be represented using the following formula:

C[i] = (P[i] + K[i % key_length]) mod 26

where C[i] is the ith letter of the ciphertext, P[i] is the ith letter of the plaintext, K[i %
key_length] is the ith letter of the key (repeating as necessary to match the length of
the plaintext), and mod 26 ensures that the result stays within the range of the
alphabet (i.e., 0-25).

Description

To encrypt a message using the Vigenere cipher, a key is chosen and repeated to match
the length of the plaintext. Each letter of the plaintext is then shifted by a different
amount based on the corresponding letter of the key. For example, if the key is
"LEMON" and the first letter of the plaintext is "A", then the first letter of the
ciphertext is "L", which is the result of shifting "A" by "L" positions down the
alphabet. To decrypt the ciphertext, the receiver simply shifts each letter of the
ciphertext back by the corresponding amount.

Limitations

While the Vigenere cipher is more secure than the Caesar cipher, it is still vulnerable to
frequency analysis attacks. Since the same key is used to encrypt multiple letters, the
frequency distribution of letters in the plaintext can be used to reveal information
about the key. Additionally, if the key is short and easily guessable, the ciphertext can
be easily decrypted using a brute-force attack.

Conclusion

The Vigenere cipher was an important development in the history of cryptography and
served as a major advance over earlier encryption techniques. While it is no longer
considered secure, it remains a valuable teaching tool for introducing students to the
concept of polyalphabetic substitution ciphers.

Code
Output

Playfair Cypher
Introduction

The Playfair cipher is a polygraphic substitution cipher that was invented by the
British mathematician Charles Wheatstone in the 19th century. It uses a 5x5 grid of
letters (usually omitting "J") to encrypt pairs of letters in the plaintext.

Formula

The Playfair cipher can be represented using the following steps:

1. Generate the key grid by filling in the letters of the key (omitting "J") in order,
followed by the remaining letters of the alphabet in order.
2. Break the plaintext into pairs of letters (ignoring spaces and punctuation) and
apply the following rules to each pair:
a. If the two letters are the same, insert a filler letter (e.g., "X") between
them.
b. If the two letters are in the same row of the key grid, replace each letter
with the letter to its right (wrapping around to the beginning of the row
if necessary).
c. If the two letters are in the same column of the key grid, replace each
letter with the letter below it (wrapping around to the top of the column
if necessary).
d. Otherwise, replace each letter with the letter in the same row and the
column of the other letter.
3. The resulting string of letters is the ciphertext.
Description

The Playfair cipher is more secure than earlier substitution ciphers because it encrypts
pairs of letters instead of single letters, making it more difficult to use frequency
analysis to crack the code. Additionally, the use of a key grid provides an extra layer of
security by mixing up the order of the letters in the alphabet.

Limitations

Despite its strengths, the Playfair cipher is not completely secure. It is vulnerable to
known-plaintext attacks, in which an attacker who knows some of the plaintext can
use that information to deduce the key and decrypt the rest of the message.
Additionally, the key grid must be kept secret to ensure the security of the cipher

Conclusion

The Playfair cipher was an important development in the history of cryptography and
served as a major advance over earlier substitution ciphers. While it is no longer
considered secure for modern use, it remains a valuable historical artifact and a useful
teaching tool for introducing students to the concept of polygraphic substitution
ciphers.
Code
Output

One Time Pad Cipher


Introduction

The one-time pad cipher, also known as the Vernam cipher, is a symmetric encryption
technique that was invented by Gilbert Vernam in 1917. It is a type of stream cipher
that uses a random key of the same length as the plaintext to encrypt the message.

Formula

The one-time pad cipher can be represented using the following formula:

C[i] = (P[i] + K[i]) mod 26

where C[i] is the ith letter of the ciphertext, P[i] is the ith letter of the plaintext, K[i] is
the ith letter of the key (generated randomly and used only once), and mod 26 ensures
that the result stays within the range of the alphabet (i.e., 0-25).

Description

To use the one-time pad cipher, a key is generated randomly with the same length as
the plaintext. The key is then used to encrypt the message by adding each letter of the
plaintext to the corresponding letter of the key (mod 26), resulting in the ciphertext.
To decrypt the message, the receiver simply subtracts each letter of the key from the
corresponding letter of the ciphertext (mod 26), revealing the plaintext.

Limitations

The one-time pad cipher is considered to be unbreakable in theory, but in practice it is


difficult to implement securely. One major limitation is the need for a truly random
key that is the same length as the plaintext and used only once. Generating and
exchanging such a key securely can be a challenging task. Additionally, if the key is
ever reused or compromised in any way, the entire system is compromised.
Conclusion

The one-time pad cipher is a powerful encryption technique that provides perfect
secrecy when used correctly. However, due to its limitations and practical challenges,
it is not widely used in modern cryptography. Nonetheless, the one-time pad remains
an important concept in the history of cryptography and a useful teaching tool for
introducing students to the concept of perfect secrecy.

Code
Output

Rail Fence Cipher


Introduction:

Rail Fence Cipher is a transposition cipher that rearranges the plaintext letters by
writing them in a zigzag pattern over a set number of rows, and then reading the
letters off in a different order. It was named after its resemblance to a fence made of
interlocking rails.

Formula:

1. To encrypt a message using Rail Fence Cipher, the following formula can be
used:
2. Write the message in a zigzag pattern over a set number of rows, filling in the
gaps with dummy characters if necessary.
3. Read off the letters row by row to obtain the ciphertext.
4. To decrypt a message using Rail Fence Cipher, the following formula can be
used:
5. Calculate the number of rows needed based on the length of the ciphertext and
the rail pattern used.
6. Write the ciphertext in a zigzag pattern over the same number of rows, filling in
the gaps with dummy characters if necessary.
7. Read off the letters in the original order to obtain the plaintext.

Description:

The Rail Fence Cipher works by taking the plaintext message and writing it in a zigzag
pattern over a set number of rows. For example, if the message is "HELLO WORLD"
and the number of rows is 3, the message would be written as follows:
H...O...L...

.E.L.W.R.D.

..L...O....

The dots represent empty spaces that are filled with dummy characters if necessary to
complete the rows. The ciphertext is then obtained by reading off the letters row by
row, resulting in "HO LWRDLELO".

To decrypt the ciphertext, the process is reversed. First, the number of rows needed is
calculated based on the length of the ciphertext and the rail pattern used. For example,
if the ciphertext is "HO LWRDLELO" and the number of rows is 3, the number of
columns would be 6. The ciphertext is then written in a zigzag pattern over the same
number of rows, filling in the gaps with dummy characters if necessary:

H...O...L...

.E.L.W.R.D.

..L...O....

Finally, the letters are read off in the original order, resulting in the plaintext "HELLO
WORLD".

Limitation:

One of the limitations of the Rail Fence Cipher is that it is vulnerable to frequency
analysis attacks. Since the ciphertext is obtained by rearranging the letters of the
plaintext, the frequency distribution of the letters is not preserved. As a result, an
attacker who knows or can guess the length of the message can use frequency analysis
to break the cipher.

Another limitation of the Rail Fence Cipher is that it is not very secure against
brute-force attacks. Since the number of possible rail patterns is limited, an attacker
can easily try all possible patterns until the correct one is found.

Conclusion:

In conclusion, Rail Fence Cipher is a simple and easy-to-implement transposition


cipher that can be used to encrypt messages. However, it has some limitations that
make it less secure than more modern encryption methods. As a result, it is not
suitable for use in situations where strong security is required.
Code
Output

Row Column Transposition


Introduction:

Row Column Transposition, also known as Columnar Transposition, is a classical


cryptographic technique that involves permuting the columns of a plaintext message
to form the ciphertext. This technique is a type of transposition cipher, where the
order of the characters in the plaintext message is rearranged to form the ciphertext.

Formula:

The formula for encrypting a plaintext message using Row Column Transposition is as
follows:

1. Divide the plaintext message into equal-sized blocks of characters, each block
containing the same number of characters as the key.
2. Write the key in a row at the top of a matrix.
3. Fill the matrix column-wise with the characters of the plaintext message,
padding the last block with filler characters if necessary.
4. Read the ciphertext column-wise, starting from the leftmost column.
5. The formula for decrypting the ciphertext message is simply the reverse of the
encryption formula, where the ciphertext is arranged in the matrix
column-wise according to the key, and the plaintext is read row-wise.
Description:

Row Column Transposition works by permuting the columns of the plaintext message
using a key, which is a string of integers that specifies the order of the columns in the
transposition. The plaintext message is first divided into blocks of characters, each
block containing the same number of characters as the key. The key is then written in
a row at the top of a matrix, and the plaintext characters are filled in column-wise,
starting from the leftmost column. If the last block of the plaintext message is not the
same length as the key, it is padded with filler characters to ensure that each block is
the same length. The ciphertext is then read column-wise, starting from the leftmost
column.

To decrypt the ciphertext, the columns are read in the order specified by the key, and
the rows are reconstructed. If the last block of the plaintext message was padded with
filler characters, these characters are removed before the original plaintext message is
recovered.

Limitation:

One of the main limitations of Row Column Transposition is that it is vulnerable to


cryptanalysis techniques such as frequency analysis and pattern recognition. Since the
characters in the plaintext message are simply rearranged, the frequencies of the
characters are preserved in the ciphertext. This makes it possible to perform
frequency analysis on the ciphertext to infer information about the original plaintext
message. Additionally, the transposition pattern of the columns may also reveal
patterns that can be exploited by an attacker.

Conclusion:

Row Column Transposition is a simple and effective technique for encrypting


messages, but it is not secure against modern cryptanalysis techniques. As such, it is
typically used in combination with other cryptographic techniques, such as
substitution ciphers or modern encryption algorithms, to provide stronger security.
Code:
Output

You might also like