暴力Caeser加密Python

2024-06-07 17:12:11 发布

您现在位置:Python中文网/ 问答频道 /正文

https://en.wikipedia.org/wiki/Caesar_cipher

嘿,大家好,我是作为大学的任务来做的。我已经快完成了,但我还是困在最后一节了。我确信这是一些基本的东西,但是我已经花了将近四个小时用print语句和其他所有的方法来查找我的错误。在

基本上,它的想法是,它只需通过所有-26个偏移量来对用户的加密消息进行强力解密。我的问题是,每一次我的密码都会被破解。。如果有道理的话。在

正确的样本输出:

Please enter string to decrypt: ykixkz&yw{oxxkr

Offset: -1 = Decrypted string: xjhwjy%xvznwwjq
Offset: -2 = Decrypted string: wigvix$wuymvvip
Offset: -3 = Decrypted string: vhfuhw#vtxluuho
Offset: -4 = Decrypted string: ugetgv"uswkttgn
Offset: -5 = Decrypted string: tfdsfu!trvjssfm
Offset: -6 = Decrypted string: secret squirrel
Offset: -7 = Decrypted string: rdbqds}rpthqqdk
Offset: -8 = Decrypted string: qcapcr|qosgppcj
Offset: -9 = Decrypted string: pb`obq{pnrfoobi
Offset: -10 = Decrypted string: oa_napzomqennah
Offset: -11 = Decrypted string: n`^m`oynlpdmm`g
Offset: -12 = Decrypted string: m_]l_nxmkocll_f
Offset: -13 = Decrypted string: l^\k^mwljnbkk^e
Offset: -14 = Decrypted string: k][j]lvkimajj]d
Offset: -15 = Decrypted string: j\Zi\kujhl`ii\c
Offset: -16 = Decrypted string: i[Yh[jtigk_hh[b
Offset: -17 = Decrypted string: hZXgZishfj^ggZa
Offset: -18 = Decrypted string: gYWfYhrgei]ffY`
Offset: -19 = Decrypted string: fXVeXgqfdh\eeX_
Offset: -20 = Decrypted string: eWUdWfpecg[ddW^
Offset: -21 = Decrypted string: dVTcVeodbfZccV]
Offset: -22 = Decrypted string: cUSbUdncaeYbbU\
Offset: -23 = Decrypted string: bTRaTcmb`dXaaT[
Offset: -24 = Decrypted string: aSQ`Sbla_cW``SZ
Offset: -25 = Decrypted string: `RP_Rak`^bV__RY
Offset: -26 = Decrypted string: _QO^Q`j_]aU^^QX

我的输出:

^{pr2}$

我的代码(我删掉了大部分程序)

choice = 0

print ('*** Menu ***\n')

print ('1. Encrypt string')
print ('2. Decrypt string')
print ('3. Brute force decryption')
print ('4. Quit\n')

    elif choice == 3:
        print ('In command 3 - Brute force')
        userString = input('\nPlease enter string to decrypt: ')
        userList = list(userString)
        offsetValue = 0
        decryptIndex = 0
        while offsetValue != -26 : # Once the count reaches -26 stop, hammer time
            while decryptIndex < len(userList):
                decryptChr = chr(ord(userList[decryptIndex]) + offsetValue)
                userList[decryptIndex] = decryptChr
                decryptIndex += 1
                offsetValue -= 1
            userString = ''.join(userList)
            print ('Offset',offsetValue,' = Decrypted string:' ,userString)
        print ('\n*** Menu ***\n')
        print ('1. Encrypt string')
        print ('2. Decrypt string')
        print ('3. Brute force decryption')
        print ('4. Quit\n')
        choice = int(input('What would you like to do? [1,2,3,4]? '))
        while choice != 1 and choice != 2 and choice != 3 and choice != 4:
            choice = int(input('\nPlease re-enter either [1,2,3,4] '))

print ('\nGoodbye.')

有什么想法吗?!在


Tags: toinputstringoffsetprintenterforcechoice
3条回答

更容易使用str.translate

chars = "abcdefghijklmnopqrstuvwxyz"
rot1 = str.maketrans(chars, chars[1:]+chars[0])

message = input()

for i in chars:
    print(message)
    message = message.translate(rot1)

在Python中解码Caesar-cypher的一种更简洁的方法是使用字符串切片。在

以字母表为例:

src = 'abcdefghijklmnopqrstuvwxyz'

现在,对于每种可能性,使用shiftn = 0, ..., 25,我们可以为密码建立一个新的字符集:

^{pr2}$

然后,您可以通过查找源和目标集中每个字符的位置来解码字符串s

    decoded = ''.join(dest[src.index(c)] for c in s)

你可以按照以下思路做些事情:

from string import ascii_lowercase as alphabet 
import string

def caesar(plaintext, shift):
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    table = string.maketrans(alphabet, shifted_alphabet)
    return plaintext.translate(table)

plain='hello there'  
shift=3
coded=caesar(plain,shift)  
decoded=caesar(coded,-shift)    
print plain
print coded
print decoded  

印刷品:

^{pr2}$

相关问题 更多 >

    热门问题