我在过去两天研究了这个主题,并尝试了多种尝试解决这个问题的方法。该程序假设采用16位随机密钥并将其与用户输入的消息进行异或。我的程序目前没有错误,但我遇到了问题。
import string
import random
message = raw_input("Enter your message: ").split(",")
size = 2 # or whatever lenght you want your random string to be
allowed = string.ascii_letters # add any other allowed characters here
randomstring = ''.join([allowed[random.randint(0, len(allowed) - 1)] for x in xrange(size)])
print
print("This is the key used for encryption:")
print randomstring #prints out key used
print
print("APPLYING XOR METHOD TO MESSAGE AND KEY")
print("--------------------------------------")
for i in xrange(0,len(message)-1):
l += [ord(message) ^ ord(randomstring) for message,randomstring in zip(message,randomstring)]
print l
print
#BRUTE FORCE TO FIND OUT RANDOMSTRING
key= []
count =0
while(key!=randomstring):
key = ''.join([allowed[random.randint(0, len(allowed) - 1)] for x in xrange(size)])
count = count + 1
if(key==randomstring):
print ("FOUND KEY USED BY BRUTE FORCE: "+key)
print (count)
break
此代码生成随机密钥,并在for循环结束后找出相同的密钥。例如,我的输出是,
Enter your message: hi
This is the key used for encryption:
ng
APPLYING XOR METHOD TO MESSAGE AND KEY
--------------------------------------
FOUND KEY USED BY BRUTE FORCE: ng
1052
我需要一种方法将加密的XOR消息打印到屏幕上。 提前谢谢!
答案 0 :(得分:1)
首先,修改您对raw_input
:
message = raw_input("Enter your message: ")
当您在输入字符串上调用.strip(",")
时,您创建了一个字符串列表。因此,如果您最初输入“Hello,world”,则结果为['Hello', ' world']
。
稍后在您的程序中,您需要message
为字符串,而不是列表。
接下来,初始化变量l
并在循环后打印其值:
l=[]
for i in xrange(0,len(message)-1):
l += [ord(message) ^ ord(randomstring) for message,randomstring in zip(message,randomstring)]
print l