We are interested in how to use the pair of Encryption and Decryption Keys to encrypt a message. Say, here is the pair of Encryption and Decryption Keys:
• Encryption Key (e, n) = (19, 221)
• Decryption Key (d, n) = (91, 221)
If the message to be encrypted is “A”, which’s Unicode/ASCII code value is 65. We can use
the Encryption Key (e, n), i.e., (19, 221), and the equation, i.e., c = me mod n, to encrypt the message:
• c = 6519 mod 221 = 143. The Encrypted Message is 143, i.e., '\x8f'.
Given that the message to be decrypted is “\x8f”, which’s Unicode/ASCII code value is 143. We can use the Decryption Key (d, n), i.e., (91, 221), and the equation, i.e., m = cd mod n, to decrypt the message:
• m = 14391 mod 221 = 65. The Decrypted Message is 65, i.e., ‘A’.
The Power-Modulo function can help you to calculate the value of c and m in the above
cases. For example, if we calculate the value of 6519 mod 221, we just need to type pow(65, 19, 211) in Python.
Please write a Python programme to decrypt the message in the “encrypted.txt” using the Decryption Key (91, 221). The decrypted message should be store in a text file named “decrypted.txt”. Please indicate the decrypted message in the Python programme using a Comment.
可以这样写代码:
with open('encrypted.txt', 'w', encoding='utf-8') as f0:
en = []
for x in 'this is a message':
en.append(chr(ord(x)**19 % 221))
enc = ''.join(en)
f0.write(enc)
with open('encrypted.txt','r',encoding='utf-8') as f,open('decrypted.txt','w',encoding='utf-8') as f1:
#decrypted:this is a message
de=[]
for c in f.read().strip():
de.append(chr(ord(c)**91%221))
dec=''.join(de)
f1.write(dec)
print(dec)
F:\2021\qa\ot2>t3
ÂÃoo×o¾ejj×ge
F:\2021\qa\ot2>t3
this is a message
如有帮助,请点击我回答的右上方采纳按钮。