You are on page 1of 9

National Institute of Technology Delhi

Cryptography and network security


(CSB 451)

Assignment-3

Submitted by:
Aadhar Kumar
Roll no.: 201210001

Submitted to:
Dr. Karan Verma
Question 1: Use Playfair Cipher to encrypt and decrypt the message, “I CANT DO IT”.
The key for encryption is “DIMENSION”. Solve this question and implement it using
python.
Answer:
Plaintext: I CANT DO IT, Encryption key: DIMENSION
Ciphertext: MBGDPEQORY
Decrypted Text: ICANTDOITX
Code:

def create_key_table(key):
key = key.upper().replace("J", "I")
key = "".join(sorted(set(key), key=lambda x: key.index(x)))
key_table = [['' for _ in range(5)] for _ in range(5)]
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"

k = 0
for i in range(5):
for j in range(5):
if k < len(key):
key_table[i][j] = key[k]
k += 1
else:
while alphabet[k % len(alphabet)] in key:
k += 1
key_table[i][j] = alphabet[k % len(alphabet)]
k += 1

return key_table

def find_position(char, key_table):


for i in range(5):
for j in range(5):
if key_table[i][j] == char:
return i, j

def playfair_encrypt(plaintext, key):


key_table = create_key_table(key)
plaintext = plaintext.upper().replace("J", "I")
plaintext = "".join([char if char.isalpha() else '' for char in plaintext])

# Handle letter pairs


pairs = [plaintext[i:i+2] for i in range(0, len(plaintext), 2)]
for i in range(len(pairs)):
if len(pairs[i]) == 1:
pairs[i] += 'X'

# Encrypt each digraph


ciphertext = ''
for pair in pairs:
char1, char2 = pair[0], pair[1]
row1, col1 = find_position(char1, key_table)
row2, col2 = find_position(char2, key_table)

if row1 == row2:
ciphertext += key_table[row1][(col1 + 1) % 5] + key_table[row2][(col2 + 1)
% 5]
elif col1 == col2:
ciphertext += key_table[(row1 + 1) % 5][col1] + key_table[(row2 + 1) %
5][col2]
else:
ciphertext += key_table[row1][col2] + key_table[row2][col1]

return ciphertext

def playfair_decrypt(ciphertext, key):


key_table = create_key_table(key)
ciphertext = ciphertext.upper().replace("J", "I")
ciphertext = "".join([char if char.isalpha() else '' for char in ciphertext])

pairs = [ciphertext[i:i+2] for i in range(0, len(ciphertext), 2)]

plaintext = ''
for pair in pairs:
char1, char2 = pair[0], pair[1]
row1, col1 = find_position(char1, key_table)
row2, col2 = find_position(char2, key_table)

if row1 == row2:
plaintext += key_table[row1][(col1 - 1) % 5] + key_table[row2][(col2 - 1) %
5]
elif col1 == col2:
plaintext += key_table[(row1 - 1) % 5][col1] + key_table[(row2 - 1) %
5][col2]
else:
plaintext += key_table[row1][col2] + key_table[row2][col1]

return plaintext

key = "DIMENSION"
plaintext = "I CANT DO IT"
ciphertext = playfair_encrypt(plaintext, key)
decrypted_text = playfair_decrypt(ciphertext, key)

print(f"Plaintext: {plaintext}")
print(f"Ciphertext: {ciphertext}")
print(f"Decrypted Text: {decrypted_text}")
Question 2: The chosen plaintext attack is on Hill Cipher with P= C = Z(2/7). Suppose
the plaintest be “ESSENTIALA” and the Message is encoded using : E = 0, S=1, N=2,
T=3, I=4, A=5 and L=6. the ciphertext is “TNSLILALET”. Find the key used in this
cipher.
Answer:

To find the key used in a Hill Cipher, we can perform a chosen plaintext attack. The
idea is to choose a plaintext and observe the corresponding ciphertext. By comparing
the known plaintext and ciphertext, we can deduce the key matrix used in the
encryption.

In a Hill Cipher, the encryption is performed using the equation C = KP mod 7, where:
- C is the ciphertext matrix,
- K is the key matrix,
- P is the plaintext matrix, and
- mod 7 represents the modulo operation to keep the values within the range of the
alphabet.

Given that the plaintext "ESSENTIALA" corresponds to the numerical values E=0, S=1,
N=2, T=3, I=4, A=5, L=6, we can construct the plaintext matrix P:

P = [[4, 1, 2], [3, 4, 5], [1, 2, 0]]

The known ciphertext is "TNSLILALET". We can convert this into a numerical matrix C:

C = [ [19, 13, 12], [8, 8, 0], [11, 4, 19]]

Now, we can set up the equation C = KP mod 7 and solve for the key matrix K:

K = CP^{-1} mod 7
K = [[3, 1, 2], [1, 6, 4], [1, 3, 1]]

Mapping with given values


Key used in attack: TSNSLISTS
Question 3: Use Transposition cipher to encrypt and decrypt the message “MEET ME
AT BOAT CLUB CANTEEN” using the key “EXAMPLE”. Solve this question and
implement it using C
Language.
Answer:
Code:
#include <stdio.h>
#include <string.h>

void encrypt(char *message, char *key) {


int keyLen = strlen(key);
int msgLen = strlen(message);
int col = keyLen;
int row = (msgLen + keyLen - 1) / keyLen;
char grid[row][col];

int k = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (k < msgLen)
grid[i][j] = message[k++];
else
grid[i][j] = 'X';
}
}

printf("Encrypted Message: ");


for (int i = 0; i < keyLen; i++) {
for (int j = 0; j < row; j++) {
if (grid[j][i] != 'X')
printf("%c", grid[j][i]);
}
}
printf("\n");
}
void decrypt(char *message, char *key) {
int keyLen = strlen(key);
int msgLen = strlen(message);
int col = keyLen;
int row = (msgLen + keyLen - 1) / keyLen;
char grid[row][col];

for (int i = 0; i < row; i++) {


for (int j = 0; j < col; j++) {
grid[i][j] = 'X';
}
}

int k = 0;
for (int i = 0; i < keyLen; i++) {
for (int j = 0; j < row; j++) {
if (k < msgLen)
grid[j][i] = message[k++];
}
}

printf("Decrypted Message: ");


for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] != 'X')
printf("%c", grid[i][j]);
}
}
printf("\n");
}

int main() {
char message[] = "MEET ME AT BOAT CLUB CANTEEN";
char key[] = "EXAMPLE";
printf("Original Message: %s\n", message);
encrypt(message, key);
decrypt(message, key);
return 0;
}

Output:
Original Message: MEET ME AT BOAT CLUB CANTEEN
Encrypted Message: M TCEA AETCNT LT BUEMOBEEA N
Decrypted Message: M AOC TEMTALCEEE TUAET B BNN
Question 4: Use One-Time Pad and find the ciphertext for the following plaintext.
Key : X V H E U W N O P G D Z X V H E U W N O P G D Z X V
Plaintext : WE LIVE IN A WORLD FULL OF BEAUTY
Prove (mathematically) that one-time pad is secure.
Answer:
One-Time padding ->
The One-Time Pad (OTP) is a symmetric-key encryption algorithm that uses a
random key that is as long as the message and is only used once. The security
of OTP is based on the concept that if the key is truly random, as long as the key
is kept secret, it provides perfect secrecy. Perfect secrecy means that the
ciphertext provides no information about the plaintext, making it theoretically
unbreakable.
Ciphertext=(Plaintext+Key)mod26

Plaintext: WE LIVE IN A WORLD FULL OF BEAUTY


Ciphertext: TZAPCRRHXTWZQRVVFZGTJROSLAAFYWHHN

Security of One-Time Pad:


To prove mathematically that the One-Time Pad is secure, we can consider the
definition of perfect secrecy. Perfect secrecy is achieved when, for any plaintext
message, the probability distribution of possible ciphertexts is the same, regardless of
the actual plaintext.

For a given ciphertext C and key K, the probability of obtaining a specific plaintext P is
the same for any P in the message space.
Mathematically, this can be expressed as:

P(P|C, K) = P(P)

where ( P(P|C, K) ) is the conditional probability of P given C and K, and P(P) is the
unconditional probability of P.

This condition is satisfied in the One-Time Pad because, with a truly random key, each
possible plaintext has an equal chance of being encrypted to any possible ciphertext.
Therefore, the knowledge of the ciphertext provides no information about the plaintext,
ensuring perfect secrecy.

You might also like