使用 Python 的简单对称加密
Simple symmetric encryption with Python
我想通过用 echaracters
中的字符更改每个字母来加密一些字母,该字符与 characters
中的字母具有相同的索引。
因此,我尝试使用 zip()
遍历这两个列表。
首先,我遍历了 userInput
,在同一个循环中,我遍历了 key
(即 zip(characters, echaracters)
)
然后我检查 i
是否等于 a
如果条件是 True
我将 b
添加到 cipher
.
userInput = input("Enter: ")
characters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
echaracters = ["@", "$", "(", "]", "=", ")", "&", "#", "!", "%", "~", "[", "/", ";", "*", "}", "9", "?", "5", "1", "_", ">", "<<", "+", ",", "-"]
key = zip(characters, echaracters)
cipher = ""
for i in userInput:
for a, b in key:
if i == a:
cipher += b
print(cipher)
输出
Enter: ABC
@
我不明白为什么 returns 只有第一个符号。
期望的输出
Enter: ABC
@$(
我做错了什么?
你的 key
在第一个循环通过后变为空
你可以试试这个:
key=list(zip(characters, echaracters))
for i in userInput:
for a, b in key:
if i == a:
cipher += b
原因是因为'key'是一个zip-type,在第一次迭代后失去了它的值,因为zip-type是一个迭代器。 Here 是一个类似的问题。
你的代码有点复杂。有更简单的方法来完成您的任务。
userInput = input("Enter: ")
characters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
echaracters = ["@", "$", "(", "]", "=", ")", "&", "#", "!", "%", "~", "[", "/", ";", "*", "}", "9", "?", "5", "1", "_", ">", "<<", "+", ",", "-"]
cipher = ""
for i in userInput:
cipher += echaracters[characters.index(i)]
print(cipher)
SYBMOLS = {
"A": "@",
"B": "$",
"C": "(",
"D": "]",
"E": "=",
"F": ")",
"G": "&",
"H": "#",
"I": "!",
"J": "%",
"K": "~",
"L": "[",
"M": "/",
"N": ";",
"O": "*",
"P": "}",
"Q": "9",
"R": "?",
"S": "5",
"T": "1",
"U": "_",
"V": ">",
"W": "<<",
"X": "+",
"Y": ",",
"Z": "-",
}
userInput = input("Enter: ")
for symbol in userInput:
print(SYBMOLS[symbol], end="")
此外,如果您只需要列表而不是字典,则不要使用:
characters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
您可以使用
from string import ascii_uppercase
print(ascii_uppercase) # example zip(ascii_uppercase, echaracters)
# ABCDEFGHIJKLMNOPQRSTUVWXYZ
我想通过用 echaracters
中的字符更改每个字母来加密一些字母,该字符与 characters
中的字母具有相同的索引。
因此,我尝试使用 zip()
遍历这两个列表。
首先,我遍历了 userInput
,在同一个循环中,我遍历了 key
(即 zip(characters, echaracters)
)
然后我检查 i
是否等于 a
如果条件是 True
我将 b
添加到 cipher
.
userInput = input("Enter: ")
characters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
echaracters = ["@", "$", "(", "]", "=", ")", "&", "#", "!", "%", "~", "[", "/", ";", "*", "}", "9", "?", "5", "1", "_", ">", "<<", "+", ",", "-"]
key = zip(characters, echaracters)
cipher = ""
for i in userInput:
for a, b in key:
if i == a:
cipher += b
print(cipher)
输出
Enter: ABC
@
我不明白为什么 returns 只有第一个符号。
期望的输出
Enter: ABC
@$(
我做错了什么?
你的 key
在第一个循环通过后变为空
你可以试试这个:
key=list(zip(characters, echaracters))
for i in userInput:
for a, b in key:
if i == a:
cipher += b
原因是因为'key'是一个zip-type,在第一次迭代后失去了它的值,因为zip-type是一个迭代器。 Here 是一个类似的问题。 你的代码有点复杂。有更简单的方法来完成您的任务。
userInput = input("Enter: ")
characters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
echaracters = ["@", "$", "(", "]", "=", ")", "&", "#", "!", "%", "~", "[", "/", ";", "*", "}", "9", "?", "5", "1", "_", ">", "<<", "+", ",", "-"]
cipher = ""
for i in userInput:
cipher += echaracters[characters.index(i)]
print(cipher)
SYBMOLS = {
"A": "@",
"B": "$",
"C": "(",
"D": "]",
"E": "=",
"F": ")",
"G": "&",
"H": "#",
"I": "!",
"J": "%",
"K": "~",
"L": "[",
"M": "/",
"N": ";",
"O": "*",
"P": "}",
"Q": "9",
"R": "?",
"S": "5",
"T": "1",
"U": "_",
"V": ">",
"W": "<<",
"X": "+",
"Y": ",",
"Z": "-",
}
userInput = input("Enter: ")
for symbol in userInput:
print(SYBMOLS[symbol], end="")
此外,如果您只需要列表而不是字典,则不要使用:
characters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
您可以使用
from string import ascii_uppercase
print(ascii_uppercase) # example zip(ascii_uppercase, echaracters)
# ABCDEFGHIJKLMNOPQRSTUVWXYZ