【发布时间】:2019-07-30 10:17:37
【问题描述】:
我已经为 vigenere 密码编写了一个加密和解密程序,但在涉及空格时我被卡住了。我可以用什么条件来克服它并完全忽略它
print("Enter the string to be encrypted")
s=input()
print("Enter the key for encryption")
t=input()
r=0
s=list(s)
t=list(t)
key=[]
encrypted=[]
decrypted=[]
#This is the loop for making the key string
for i in range(0,len(s),len(t)):
r=i
for j in range(0,len(t),1):
if(i<len(s)):
if(len(key)<len(s)):
key.append(t[j])
i=i+1
i=r
print("The encrypted key is: ")
key=''.join(key)
print(key)
#This is the code for encrypting the message with the key string
for i in range(0,len(s)):
x = (ord(s[i]) + ord(key[i])) % 26
x += ord('A')
encrypted.append(chr(x))
print("The encrypted string is: ")
encrypted=''.join(encrypted)
print(encrypted)
#For decryption
for i in range(0,len(s)):
x = (ord(encrypted[i]) - ord(key[i])+26) % 26
x += ord('A')
decrypted.append(chr(x))
print("The decrypted string is: ")
decrypted=''.join(decrypted)
print(decrypted)
这里Key字符串指的是->
字符串输入 - 西瓜
钥匙 - 柠檬
钥匙串 - LEMONLEMON
但它不适用于句子。我尝试输入类似
的代码if(s[i]==' '):
continue
但是没用
【问题讨论】:
-
代码不可用,除了你的问题是你正在创建与字符串匹配的密钥,你应该在加密之前删除空格。
-
该代码仅可用于大写字符和大写键但没有空格,在解密过程中我将如何在加密后放置空格...编辑它以使其工作,发布它可能已经改变有点
-
不是那个意思,尝试将代码复制粘贴到一个新文件中,您就会明白我的意思了。你很可能有混合缩进或其他东西,看看第一个循环,
r=i是“过度缩进 -
代码现在有望修复
标签: python python-3.x encryption cryptography