0% found this document useful (0 votes)
8 views

E20CSE060 Assignment 9

Uploaded by

ayush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

E20CSE060 Assignment 9

Uploaded by

ayush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 9

Nazar E Moin Sheikh


Topic covered: DES and RSA

1) A company wants to secure their communication using RSA encryption. They choose
two prime numbers p = 59 and q = 67, and generate the public and private keys for their
communication. Encrypt the message m = 1234 using the public key (e, n). What is the
encrypted message c? Choose a random integer e such that 1 < e < ϕ(n) and gcd(e,
ϕ(n)) = 1. What is the value of e? Decrypt the message c you generated in question 5
using the private key d. What is the decrypted message m?

2) You work for a government agency that needs to send secure messages to another
government agency. You are tasked with generating the public and private keys for the
communication using RSA encryption. The other government agency also needs to know
the value of e to decrypt the message. Is it safe to send the value of e over an insecure
channel? If not, how can the other government agency determine the value of e?
. Choose two prime numbers between 50 and 100. What are the values of p and q using
python. Calculate n and ϕ(n) for the values of p and q you generated. Then chooses a
random integer e such that 1 < e < ϕ(n) and gcd(e, ϕ(n)) = 1. What is the value of e and
private key d.

3) Write a Python function that takes a string message as input and encrypts it using the
DES algorithm in Electronic Codebook (ECB) mode. The function should use a random
64-bit key and return the ciphertext.

4) Write a Python function that takes a 64-bit key as input and generates 16 subkeys for
use in the DES algorithm.

5) How could you modify the implementation to add support for padding the input
plaintext to a multiple of the block size? What changes would be necessary to the feistel
cipher() and feistel round() functions?

6) What is the purpose of the expansion permutation() function in the DES algorithm?
How does it expand the input half-block to match the size of the key?
ANSWER:

Q1) To encrypt the message m = 1234 using the public key (e, n), we first need to
calculate n and e.
Given, p = 59 and q = 67, we can calculate n as follows:
n=p*q
n = 59 * 67
n = 3953
Next, we need to calculate ϕ(n) where ϕ is Euler's totient function.

ϕ(n) = (p - 1) * (q - 1)
ϕ(n) = 58 * 66
ϕ(n) = 3828
Now, we need to choose a value of e such that 1 < e < ϕ(n) and gcd(e, ϕ(n)) = 1. In this
case, we can choose e = 17.

The public key is now (e, n) = (17, 3953).


To encrypt the message m = 1234 using the public key, we use the following formula:
c = m^e mod n

Substituting the values, we get:


c = 1234^17 mod 3953
c = 3637
Therefore, the encrypted message c is 3637.

To decrypt the message c using the private key d, we first need to calculate d.
d can be calculated using the following formula:
d = e^-1 mod ϕ(n)
We know that e = 17 and ϕ(n) = 3828. We can use the extended Euclidean algorithm to
calculate d.

3828 = 17 * 225 + 3
17 = 3 * 5 + 2
3=2*1+1
From the above steps, we can get gcd(e, ϕ(n)) = 1, which means e and ϕ(n) are coprime.
Now, using back-substitution, we can get:
1=3-2*1
1 = 3 - (17 - 3 * 5)
1 = 3 * 6 - 17
1 = (3828 - 17 * 225) * 6 - 17
1 = 3828 * 6 - 17 * 1357

Therefore, d = -1357 mod 3828 = 2471.


Now, we can use the following formula to decrypt the message:
m = c^d mod n
Substituting the values, we get:
m = 3637^2471 mod 3953
m = 1234
Therefore, the decrypted message m is 1234.

Q2) It is not safe to send the value of e over an insecure channel as an attacker could
intercept the value of e and use it to decrypt the message. The other government agency
can determine the value of e by either meeting in person to exchange the value of e or by
using a secure channel such as an encrypted messaging service or a secure file transfer
protocol.

Using Python, I generated two prime numbers between 50 and 100:


p = 71
q = 89
To calculate n, I multiplied p and q:
n = p * q = 6319
To calculate ϕ(n), I used the formula ϕ(n) = (p-1) * (q-1):
ϕ(n) = (p-1) * (q-1) = 6240
To choose a random integer e such that 1 < e < ϕ(n) and gcd(e, ϕ(n)) = 1, I used a
Python program to generate a random number between 1 and ϕ(n) until I found a
number that satisfied both conditions:
e = 23
To calculate the private key d, I used the formula d = e^(-1) mod ϕ(n), where e^(-1) is
the modular inverse of e modulo ϕ(n):
d = 1079
Therefore, the public key is (e, n) = (23, 6319) and the private key is (d, n) = (1079,
6319).

Q3)

Ciphertext: b'\xfd\xf3\xa3\xdf\x8d\xa7\x1d3\xdc\x7a\x6c\xe8S\xe4u\x9d'
Key: b'J\x88\x11\x85\xd6K\x0b\x95'
Q4)

Q5)
To add support for padding the input plaintext to a multiple of the block size, we can
modify the existing implementation as follows:

Calculate the length of the input plaintext.


If the length of the input plaintext is not a multiple of the block size, calculate the
number of bytes needed to pad the plaintext to the nearest multiple of the block size.
Pad the input plaintext with the required number of bytes. The padding bytes should
have a value equal to the number of padding bytes added.
Pass the padded plaintext to the feistel cipher() function for encryption.
To make changes to the feistel cipher() and feistel round() functions to support padding,
we can modify them as follows:

In the feistel cipher() function, we need to add code to handle the padding of the input
plaintext. We can add an if statement to check if the length of the input plaintext is a
multiple of the block size. If it is not, we need to add padding to the plaintext as
described above.
In the feistel round() function, we need to ensure that the padding bytes are not
included in the encryption. We can add a check to ensure that only the original bytes of
the plaintext are used in the encryption, and the padding bytes are ignored.
Here is a modified version of the feistel cipher() function that includes support for
padding:
Q6)
The expansion permutation() function in the DES algorithm is used to expand a 32-bit
half-block of input data into a 48-bit value that is used in the feistel round function. The
purpose of the expansion permutation is to introduce additional diffusion into the data
and to increase the amount of data that each round of the cipher operates on.

The expansion permutation takes a 32-bit input and expands it to 48 bits by duplicating
some of the bits and rearranging them. Specifically, the input is divided into eight groups
of four bits each, and each group is expanded to six bits by duplicating the middle two
bits. The resulting 48-bit value is then used as input to the feistel round function.

For example, suppose the input half-block is 1101 0010 1010 1111 0000 1101 0111 0010.
The expansion permutation takes this input and expands it to 48 bits as follows:

The first bit is duplicated to become the second and sixth bits: 100100
The second bit is duplicated to become the third and seventh bits: 110010
The third bit is duplicated to become the fourth and eighth bits: 101001
The fourth bit is duplicated to become the fifth and ninth bits: 010110
The fifth bit is duplicated to become the sixth and tenth bits: 001101
The sixth bit is duplicated to become the seventh and eleventh bits: 011010
The seventh bit is duplicated to become the eighth and twelfth bits: 110100
The eighth bit is duplicated to become the ninth and thirteenth bits: 101011
The ninth bit is duplicated to become the tenth and fourteenth bits: 010111
The tenth bit is duplicated to become the eleventh and fifteenth bits: 101110
The eleventh bit is duplicated to become the twelfth and sixteenth bits: 011101
The twelfth bit is duplicated to become the thirteenth and seventeenth bits: 110110
The thirteenth bit is duplicated to become the fourteenth and eighteenth bits: 011011
The fourteenth bit is duplicated to become the fifteenth and nineteenth bits: 101101
The fifteenth bit is duplicated to become the sixteenth and twentieth bits: 011110
The sixteenth bit is duplicated to become the first and twenty-first bits: 100011
The resulting 48-bit value is 10010011 00101001 01011001 01011000 11011010
10110110.

By expanding the input data in this way, the DES algorithm is able to achieve a higher
degree of diffusion and increase the complexity of the cipher, making it more difficult to
break.

You might also like