https://en.wikipedia.org/wiki/Caesar_cipher
大家好,我正在为 Uni 做这个任务。我非常接近完成,但我被困在最后一部分。我确信这是一些基本的东西,但我已经花了将近四个小时试图使用打印语句和所有内容来查找我的错误。
基本上这个想法是,它会通过循环遍历所有 -26 个偏移量来强力解密用户的加密消息。我的问题是我可以让代码循环 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
我的输出:
Please enter string to decrypt: ff
Offset: -1 = Decrypted string: ff
Offset: -2 = Decrypted string: ff
Offset: -3 = Decrypted string: ff
Offset: -4 = Decrypted string: ff
Offset: -5 = Decrypted string: ff
Offset: -6 = Decrypted string: ff
Offset: -7 = Decrypted string: ff
Offset: -8 = Decrypted string: ff
Offset: -9 = Decrypted string: ff
Offset: -10 = Decrypted string: ff
Offset: -11 = Decrypted string: ff
Offset: -12 = Decrypted string: ff
Offset: -13 = Decrypted string: ff
Offset: -14 = Decrypted string: ff
Offset: -15 = Decrypted string: ff
Offset: -16 = Decrypted string: ff
Offset: -17 = Decrypted string: ff
Offset: -18 = Decrypted string: ff
Offset: -19 = Decrypted string: ff
Offset: -20 = Decrypted string: ff
Offset: -21 = Decrypted string: ff
Offset: -22 = Decrypted string: ff
Offset: -23 = Decrypted string: ff
Offset: -24 = Decrypted string: ff
Offset: -25 = Decrypted string: ff
Offset: -26 = Decrypted string: ff
我的代码(我已经删掉了大部分程序)
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.')
有任何想法吗?!