A one time pad encryption system represents the letters A-E with numbers 1-5 and a space with the number 0. Given the cipher text: DBCA
and the one time pad: BCBC
What is the encrypted word?
我的解决方案:
A B C D E _ 1 2 3 4 5 0
密文:DBCA 一次性垫子&关键: BCBC
B C B C. 2 3 2 3
D B C A. 4 2 3 1
加密文字 B A A B 2 1 1 2
我得到的加密字是:BAAB,这不是一个单词,所以我是不正确的
我研究过如何使用一次性垫,但我仍然不理解
答案 0 :(得分:0)
一次性垫不是那样的。 您的代码长度必须至少与文本一样长。由于每次加密和解密文本都会使用一次。
f = open("plain.txt", "rb+")
kf = open ("key.txt", "rb" )
f2= open("cipher.txt", "r+")
allplain = f.read()
key =[]
key = kf.read()
# - encrypt plain text and save into cipher.txt
for i in range(len(allplain)):
k = chr (key[i])
p = chr (allplain[i])
c = chr ((ord(p)) ^ (ord(k)))
f2.write(c)
这是Python中One-Time Pad加密的完整实现。 One-Time Pad Encryption