Professional Documents
Culture Documents
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
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
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:
The known ciphertext is "TNSLILALET". We can convert this into a numerical matrix C:
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]]
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';
}
}
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++];
}
}
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
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.