Crypto Assignment
Crypto Assignment
ID: 1921430
Submitted to: Mohammad Noor Nabi
Problem -1
We received the following ciphertext which was encoded with a shift cipher:
Xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpitghlxiwiwtxgqadds
Ans-
In this case, only one letter, ‘e,’ was identified, and this gave the shift value
for the cipher. Decoded =
ifweallunitewewillcausetheriverstostainthegreatwaters withtheirblood
= if we all unite we will cause the rivers to stain the great waters with their blood
Problem-2
This problem deals with the affine cipher with the key parameters a = 7, b = 22.
a. Decrypt the text below:
falszztysyjzyjkywjrztyjztyynaryjkyswarztyegyyj
=FIRSTTHESENTENCEANDTHENTHEEVIDENCESAIDTHEQUEEN
Next, we need to find the key used in the affine cipher. The affine cipher has two
parameters: a and b. We can use the following formula to find the key: a^-1 = (m - b)
mod 26 where m is the size of the alphabet (26 in English), and mod 26 means the
result should be in the range of 0 to 25.
To find a, we need to know the inverse of (m - b) mod 26. We can use the extended
Euclidean algorithm to find the inverse. Let's assume we have done that and found that
the inverse of (m - b) mod 26 is 9.
Now we can find a by using the formula: a = (9 * (f1 - f2)) mod 26 where f1 is the
frequency of the most frequent letter (in this case, "B"), and f2 is the frequency of the
second most frequent letter (in this case, "U"). Let's assume we have done that and
found that a is 3.
Finally, we can find b by using the formula: b = (f1 - a * (letter - 'A')) mod 26 where
letter is any known plaintext letter and 'A' is the ASCII code of the first letter in the
alphabet (in this case, 65). Let's assume we know that the plaintext letter
corresponding to the ciphertext letter "B" is "E". We can substitute these values into
the formula and solve for b: 2 = (3 * (4 - 0)) mod 26 b = (2 - 3 * (4 - 65)) mod 26 b = 23
Now we have found the key (a=3, b=23) and can decrypt the ciphertext using the
inverse formula: plaintext = a^-1 * (ciphertext - b) mod 26 Let's assume the ciphertext is
"BUBUBUBU". We can substitute these values into the formula and decrypt the
ciphertext: plaintext = 9 * (1 - 23) mod 26 =
Problem-4
Modular arithmetic is the basis of many
cryptosystems. Compute the result without a
calculator.
a. 15 · 29 mod 13
b. 2 · 29 mod 13
c. 2 · 3 mod 13
d. −11 · 3 mod 13
The results should be given in the range from 0,1, . . ., modulus-1. Briefly describe the
relation between the different parts of the problem.
Ans-
a. 15 · 29 mod 13 = (2 * 3) |13| = 6 |13|
b. 2 · 29 mod 13 = (2 * 3) |13| = 6 |13|
c. 2 · 3 mod 13 = (2 * 3) |13| = 6 |13|
d. −11 · 3 mod 13, -11 is in equivalence class {… -11, 2, 15, …}, so; = (2 * 3) |13| = 6 |13|
In all 4 cases the first integer of the product is a member of the equivalence class {… -
11, 2, 15, …} and the second integer of the product is a a member of the equivalence
class {… 3, 16, 29, …}, thus all products are equal.
The results should be given in the range from 0,1,…, modulus-1.
Briefly describe the relation between the different parts of the
problem.
Problem -5
a.
Construct the addition and multiplication table for ℤ4.
b.
Construct the addition and multiplication tables for ℤ5.
c.
Construct the addition and multiplication tables for ℤ଼8.
d.
There are elements in ℤ4 and ℤ଼8 without a multiplicative inverse. Which
elements are these? Why does a multiplicative inverse exist for all nonzero
elements in ℤ5?
e.
What is the multiplicative inverse of 5 in ℤ11
Ans-
a. To construct the addition and multiplication table for ℤ4, we list all the elements in
ℤ4, which are {0, 1, 2, 3}.
b. To construct the addition and multiplication tables for ℤ5, we list all the elements in
ℤ5, which are {0, 1, 2, 3, 4}.
e. To find the multiplicative inverse of 5 in ℤ11, we need to find a number x such that
5x ≡ 1 (mod 11). By trying different values of x, we find that x = 9 satisfies the equation,
so the multiplicative inverse of 5 in ℤ11 is 9.
Problem-6
Assume an OTP-like encryption with a short key of 256 bit. This key is then being
used periodically to encrypt large volumes of data. Describe how an attack works
that breaks this scheme
Ans-
Attacker knows encryption scheme uses a 128-bit key periodically to encrypt
large volumes of data.
Attacker collects a large amount of ciphertext, preferably multiple periods of key
usage.
Attacker divides ciphertext into blocks of 128 bits each.
Attacker performs statistical analysis on each block, looking for patterns or
correlations.
The attacker can use techniques such as frequency analysis, comparing the
distribution of characters in the ciphertext blocks to the expected distribution
of characters in the plaintext language.
Attacker makes educated guesses about the key and attempts to decrypt the
ciphertext.
Attacker tests candidate key against ciphertext to see if it produces
meaningful plaintext. If it does, encryption scheme is broken.
Problem-7
Compute the first two output bytes of the LFSR of degree 8 and degree 7 and the
feedback polynomial from Table 2.3 where the initialization vector has the value FF in
hexadecimal notation.
Ans-
To compute the first two output bytes of the LFSR of degree 8 with the feedback
polynomial x^8 + x^4 + x^3 + x + 1 and an initialization vector of FF in hexadecimal
notation, follow these steps:
3. Compute the first output bit by XORing the feedback bits with the initialization
vector: 1 XOR 1 XOR 1 XOR 1 XOR 1 = 0.
4. Shift the initialization vector one bit to the right and insert the computed output bit
at the leftmost position: 01111111.
5. Repeat steps 3 and 4 to compute the second output bit: 0 XOR 1 XOR 1 XOR 1 XOR 1 =
0.
6. Shift the initialization vector one bit to the right and insert the computed output bit
at the leftmost position: 00111111.
The first two output bytes of the LFSR are 00 in binary notation.
Problem-8
Given is a stream cipher which uses a single LFSR as key stream generator. The LFSR has a
degree of 512.
a. How many plaintext/ciphertext bit pairs are needed to launch a successful attack?
b. Describe all steps of the attack in detail and develop the formulae that need to be
solved.
c. What is the key in this system? Why doesn’t it make sense to use the initial
contents of the LFSR as the key or as part of the key?
Ans-
a. To determine the number of plaintext/ciphertext bit pairs needed to launch a
successful attack, we need to find the period of the LFSR. The period is the number of
steps it takes for the LFSR to repeat its state. For an LFSR with a degree of 512, the
maximum period it can have is 2^512 - 1. However, we need to observe the key stream
output to determine the actual period. So, we would need to encrypt plaintexts and
observe the resulting ciphertexts until we observe a repeated key stream output. The
number of plaintext/ciphertext pairs needed to observe a repeated key stream output is
equal to the period of the LFSR.
Ans-
a. To determine the degree m of the key stream generator, we count the
number of bits in the key stream output.
In this case, the key stream output has 4 bits:
1011. Therefore, the degree of the key stream
generator is 4.
```
We can use this program by calling the function with a message and
a shift value. For example:
encrypted_message = caesar_cipher("Hello,
World!", 3) print(encrypted_message) # Output:
Khoor, Zruog! decrypted_message =
caesar_cipher(encrypted_message, -3)
print(decrypted_message) # Output: Hello, World!
2. Write a program that can perform a letter frequency attack on an additive cipher
without human intervention. Your software should produce possible plaintexts in
rough order of likelihood.
Ans-
Follow the steps below to solve the problem:
Initialize a string say T as “ETAOINSHRDLCUMWFGYPBVKJXQZ”.
Find the frequency of each character of the string S, and store it in a variable, say freq[].
Iterate over the range [0, 5] using the variable i and perform the following steps:
th
Find the i most occurring element in the string S and store it in a variable, say ch.
th
Find the
difference between the ch and i character of the string T and store it in a variable, say x.
Iterate over the characters of string S, and shift all characters by x and then push the obtained string into an
array plaintext[].
Finally, after the above steps, print the strings obtained in the array plaintext[].
Code-
# Python3 program for the above approach
# Function to decrypt a
monoalphabetic # substitution
# frequency
attack def
printString(S, N):
deciphered # plaintext
plaintext = [None] * 5
freq = [0] * 26
= [None] * 26
# Traverse the
string S for i in
range(N):
if S[i] != ' ':
freq[ord(S[i]) - 65] += 1
array for i in
range(26):
freqSorted[i] = freq[i]
# Stores the string formed from
language
T = "ETAOINSHRDLCUMWFGYPBVKJXQZ"
order freqSorted.sort(reverse
= True)
range(26):
used[j]
= 1 ch =
j break
if ch == -1:
break
letter_frequency
x = ord(T[i]) - 65
shift used # in
monoalphabetic cipher
x = x - ch
# Temporary string to
a time
curr = ""
range(N):
# Insert whitespaces
of the # cipher by x
y = ord(S[k])
- 65 y += x
if y < 0:
y += 26
if y > 25:
y -= 26
# Add the kth
calculated/shifted #
plaintext[i] = curr
print(plaintext[i])
# Driver code
# Given string
N = len(S)
# Function
Call
printString(S
, N)