在不同的行上打印解密结果

时间:2013-12-12 15:43:47

标签: python loops encryption

我正在编写一个python脚本,使用凯撒密码技术将解密它不知道密钥的文本。我想知道是否有办法,或者更确切地说,是什么方式,将每个解密尝试打印到一个新行以及该行使用的键。 I.E. 5行会说“Key = 5,decryption = HELLO”之类的东西我已经尝试计算要解密的字符串的长度并使用它来循环脚本但无济于事。我是python的新手,所以如果这是一段相当基本的代码,我会道歉。

message = raw_input('Enter encrypted text: ')
decryption_message = ""
l = message.__len__()
print l
for x in range(0, 26):
    for c in message:
        for d in range (l):
            m = ord(c) - 65
            e = (int(m) - int(x)) %26
            ea = e + 65
            decryption_message = decryption_message + chr(ea)
print decryption_message

2 个答案:

答案 0 :(得分:1)

message = raw_input('Enter encrypted text: ')
decryption_message = ""
l = message.__len__()
print "Message length : {}".format(l)
for x in range(0, 26):
    for c in message:
        for d in range (l):
            m = ord(c) - 65
            e = (int(m) - int(x)) %26
            ea = e + 65
            decryption_message = decryption_message + chr(ea)
    print "Key = {}, decryption = {}".format(x, decryption_message)

答案 1 :(得分:1)

现在可以输入自己的字母,例如

 string.ascii_lowercase 

 string.ascii_uppercase

 string.ascii_letters


import string  #you need to import string 
# you enter your choice of ascii char sets(above) for the arg 'alpha', by default though it's ascii_letters
def decrypt_print(alpha=string.ascii_letters):
    message = raw_input('Enter encrypted text: ')
    de = ""
    l = len(message) # the proper way to call len
    for i in range(26):
        new_alphabet = alpha[i:] + alpha[:i] #creates new alphabet for each shift
        for c in message:
            index = alpha.index(c)
            new_letter = new_alphabet[index]
            de += new_letter
        print de + ' shift:'+ str(i)
        de = ""

输入输入gdkkn:

decrypt_print(string.ascii_letters)

Enter encryped text: gdkkn

打印:

gdkkn shift:0
**hello shift:1** # my bold
ifmmp shift:2
jgnnq shift:3
khoor shift:4
lipps shift:5
mjqqt shift:6
nkrru shift:7
olssv shift:8
pmttw shift:9
qnuux shift:10
rovvy shift:11
spwwz shift:12
tqxxA shift:13
uryyB shift:14
vszzC shift:15
wtAAD shift:16
xuBBE shift:17
yvCCF shift:18
zwDDG shift:19
AxEEH shift:20
ByFFI shift:21
CzGGJ shift:22
DAHHK shift:23
EBIIL shift:24
FCJJM shift:25