用Python解密凯撒密码



我正在学习一个使用Python的编程类,对于我们的最终项目,我们希望加密和解密密码。我们从密码列表开始:

#The password list - We start with it populated for testing purposes
passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]

然后我们有了加密密钥:

#The encryption key for the caesar cypher
encryptionKey=16

以及要求查找密码的代码:

if(choice == '2'): #Lookup at password
print("Which website do you want to lookup the password for?")
for keyvalue in passwords:
print(keyvalue[0])
passwordToLookup = input()
for key, value in passwords:
if key == passwordToLookup:
print(value)

我需要的是print(value(打印密钥的反面,这样密码在打印出来时就会解密。有人能给我指正确的方向吗?

Caesar密码将字母表中的每个字母替换为固定数量的位置。因此,如果encryptionKey是16,则字母a->q和z->p.

def encrypt(text):
result = ""
encryptionKey = 16
for i in range(len(text)):
char = text[i]
if (char.isupper()):  #check if upper case
result += chr((ord(char) + encryptionKey-65)%26 + 65)
else:
result += chr((ord(char) + encryptionKey-97)%26 + 97)
return result
text = "XqffoZeo"
print ("Text : "+ text) 
print ("Encrypted : " + encrypt(text))

那么你的输出将是:

Output:
Text : XqffoZeo
Encrypted : NgvvePue

最新更新