我目前正在开发一个python项目来获取一串文本,通过向其添加关键字然后输出结果来加密文本。我目前拥有该程序的所有功能,除了将数值转换回文本。
例如,原始文本将转换为数值,例如[a, b, c]
将变为[1, 2, 3]
。
目前我对如何纠正这个问题没有任何想法,欢迎任何帮助,我目前的代码如下:
def encryption():
print("You have chosen Encryption")
outputkeyword = []
output = []
input = raw_input('Enter Text: ')
input = input.lower()
for character in input:
number = ord(character) - 96
output.append(number)
input = raw_input('Enter Keyword: ')
input = input.lower()
for characterkeyword in input:
numberkeyword = ord(characterkeyword) - 96
outputkeyword.append(numberkeyword)
first = output
second = outputkeyword
print("The following is for debugging only")
print output
print outputkeyword
outputfinal = [x + y for x, y in zip(first, second)]
print outputfinal
def decryption():
print("You have chosen Decryption")
outputkeyword = []
output = []
input = raw_input('Enter Text: ')
input = input.lower()
for character in input:
number = ord(character) - 96
output.append(number)
input = raw_input('Enter Keyword: ')
input = input.lower()
for characterkeyword in input:
numberkeyword = ord(characterkeyword) - 96
outputkeyword.append(numberkeyword)
first = output
second = outputkeyword
print("The following is for debuging only")
print output
print outputkeyword
outputfinal = [y - x for x, y in zip(second, first)]
print outputfinal
mode = raw_input("Encrypt 'e' or Decrypt 'd' ")
if mode == "e":
encryption()
elif mode == "d":
decryption()
else:
print("Enter a valid option")
mode = raw_input("Encrypt 'e' or Decrypt 'd' ")
if mode == "e":
encryption()
elif mode == "d":
decryption()
答案 0 :(得分:0)
假设您有一个数字列表,如a = [1, 2, 3]
和一个字母:alpha = "abcdef"
。然后你将其转换如下:
def NumToStr(numbers, alpha):
ret = ""
for num in numbers:
try:
ret += alpha[num-1]
except:
# handle error
pass
return ret
答案 1 :(得分:0)
根据您的问题,您希望将号码转换回文字。
您可以为数字和字母声明两个列表,例如:
NUM = [1,2,3 .... 26]
ALPA = [' A'' B'' C' ....' Z']
然后从num列表中找到索引/位置,并在alpa列表中找到该索引的字母。
答案 2 :(得分:0)
虽然你的问题不太清楚,但我写了一个用字典加密的脚本
plaintext = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') #Alphabet String for Input
Etext = list('1A2B3C4D5E6F7G8H90IJKLMNOP') """Here is string combination of numbers and alphabets you can replace it with any kinda format and your dictionary will be build with respect to the alphabet sting"""
def messageEnc(text,plain, encryp):
dictionary = dict(zip(plain, encryp))
newmessage = ''
for char in text:
try:
newmessage = newmessage + dictionary[char.upper()]
except:
newmessage += ''
print(text,'has been encryptd to:',newmessage)
def messageDec(text,encryp, plain):
dictionary = dict(zip(encryp,plain))
newmessage = ''
for char in text:
try:
newmessage = newmessage + dictionary[char.upper()]
except:
newmessage += ''
print(text,'has been Decrypted to:',newmessage)
while True:
print("""
Simple Dictionary Encryption :
Press 1 to Encrypt
Press 2 to Decrypt
""")
try:
choose = int(input())
except:
print("Press Either 1 or 2")
continue
if choose == 1:
text = str(input("enter something: "))
messageEnc(text,plaintext,Etext)
continue
else:
text = str(input("enter something: "))
messageDec(text,Etext,plaintext)
答案 3 :(得分:0)
有几件事。第一种是将编码值转换回可以使用chr(i)命令的字符。请记住重新打开96。
for code in outputfinal:
print chr(code + 96),
我试过' aaa'作为我的明文和' bbb'作为我的关键字和这个印刷的&ccc'这就是我认为你想要做的事情。
另一件事。 zip命令迭代两个列表,但(在我的播放中)只有一个列表用完成员。如果您的明文是10个字符,而您的关键字只有5个,那么只有明文的前5个字符会被加密。您需要将密钥扩展或扩展到明文消息的长度。我玩过类似的东西:
plaintext = ['h','e','l','l','o',' ','w','o','r','l','d']
keyword = ['f','r','e','d']
index = len(keyword) # point to the end of the key word
keyLength = len(keyword)
while index < len(plaintext): # while there are still letters in the plain text
keyword.append(keyword[index - keyLength]) # expand the key
index += 1
print keyword
for a,b in zip(plaintext, keyword):
print a, b
我希望这会有所帮助。如果我误解了,请告诉我。