Let us implement a simple python program to show us how the public-key algorithm
ID: 3886574 • Letter: L
Question
Let us implement a simple python program to show us how the public-key algorithm called RSA works. To generate the two keys, choose two random large prime numbers p and q. For simplicity of calculation in our lab, let's assume small prime numbers such as p = 3 q = 7 Then calculate: n = p * q And calculate: phi (n) = (p - 1)(q - 1) Let's assume your public key is made of these two numbers: Public key = [e, n] Private keys = [d, n] Where: e = 11 d = 23 Assume your plaintext is: "Bluebonnets are beautiful!". Convert each letter of your plaintext (using parts of your program from Lab1 and Lab2) to a number m_i. You may use the functions ord() to find the ASCII number. For each number m_i, encrypt it by doing the following: c_i = E_PUB(m_i) - (m_i^e) modulus n Print the final ciphertext C = [C_0, C_1, C_2, ... C_n] (Suppose this is the ciphertext you need to send) Then, decrypt your ciphertext C by doing the following for each number c_i D_PRIV (c_i) = (c_i^d) modulus n This should give you back m_i and you can use chr() function to recover the character. Now with mi you can find which letter it corresponds to. Did you find 'Bluebonnets are beautiful!'? Repeat the encryption of the sentence above using p = 47, q = 71, e = 79 and d = 1019. Show the ciphertext and recovered message.Explanation / Answer
#you can change values to get task 3 encryption
#your algorithm is somewhere incomplete as
#it do not remove white spaces and decryption of "B"
#whose cyphertext comes to be '1' doesn't give back "B" you can verify it manully
#(as B is 66 on applying encryption comes 1 during decryption 1 raised to power 23 is 1
#and 1 modulo 23 is still 1 hence message not retrieved if we convert 1 in character also)
#still i implemented it in python you can make correction for it
import math
p=3
q=7
n=p*q
e=11
d=23
#storing message
s="Bluebonnets are beautiful"
#removing whitespaces
s.replace(" ","")
#declaring cypher array
c=[]
#applying encryption for every letter
for ele in s:
temp=math.pow(ord(ele),e)%n
c.append(int(temp))
m=[]
#applying decryption for every letter
for ele in c:
temp=math.pow(ele,d)%n
m.append(int(temp))
#converting to message(without whitespaces)
string=""
for ele in m:
s+chr(ele)
#math.pow(x,y) output x raised to power y
#int typecast float to int
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.