还需要分别打印解密后的值 - python

我正在寻找解密的值,以便它将显示所有可能的结果。而且由于密钥是消息长度的一半,因此为什么它会在那里显示正确的值。但我也想在结尾处分别显示解密后的文本。

def brute_force():
    msg = raw_input('Enter your text here: ')

    key = len(msg)/2

    alphabets = 'ABC2&0346_+/*~DEFGHIJK(){";.<.>LMNOPQRST:?UVWXYZ!#$%'
    cipher = ' '

    msg = msg.upper()

    for chars in msg:
        if chars in alphabets:
            sol = alphabets.find(chars)
            sol = sol + key

            if sol >= len(alphabets):
                sol = sol - len(alphabets)

            elif sol < 0:
                sol = sol + len(alphabets)

            cipher = cipher + alphabets[sol]

        else:
            cipher = cipher + chars
    print('Encrypted text {}'.format(cipher))
    decrypt = cipher

    for key in range(len(alphabets)):
        dec_text = ' '

        for alphas in decrypt:
            if alphas in alphabets:
                sol_dec = alphabets.find(alphas)
                sol_dec = sol_dec - key 

                if sol_dec < 0:
                    sol_dec = sol_dec + len(alphabets)

                dec_text = dec_text + alphabets[sol_dec]

            else:
                dec_text = dec_text + alphas

        print('key {} {}'.format(key, dec_text))

    if (dec_text == msg):
        word = dec_text
    else:
        word = 'Couldnt find'


    print('Decrypted text is {}'.format(word))
brute_force()

参考方案

我将这个问题解释为“即使我在较早的输出中可以看到解密的文本,为什么我的程序为什么总是显示'无法找到解密的文本'?”。有三个原因。首先,用空格初始化cipherdec_test

cipher = ' '
#...
dec_text = ' '

何时应使用空字符串初始化它们。如果您不这样做,那么所有解密的字符串的开头都会有两个空格,并且它们绝不会与原始消息进行比较。

cipher = ''
#...
dec_text = ''

此外,此条件:

if (dec_text == msg):
    word = dec_text
else:
    word = 'Couldnt find'

出现在for key in range(len(alphabets))循环之外。这意味着,当dec_text具有在循环的第51次迭代期间给出的任何值时,它将仅执行一次。如果要比较msgdec_text具有的所有不同值,则需要在循环内进行比较。

word = 'Couldnt find'
for key in range(len(alphabets)):
    dec_text = ''

    for alphas in decrypt:
        if alphas in alphabets:
            sol_dec = alphabets.find(alphas)
            sol_dec = sol_dec - key 

            if sol_dec < 0:
                sol_dec = sol_dec + len(alphabets)

            dec_text = dec_text + alphabets[sol_dec]

        else:
            dec_text = dec_text + alphas

    print('key {} {}'.format(key, dec_text))

    if (dec_text == msg):
        word = dec_text

请注意,我已经完全删除了else块,而是在开始时无条件地将word初始化为"Couldnt find"。在循环内仅在else旁边添加if是行不通的,因为即使在较早的迭代中达到了值之后,也没有阻止"Couldnt find"覆盖word的行为。这意味着除非在最后一次迭代中找到解密的文本,否则您始终会得到“无法找到”。只有if而没有else可以解决此问题。

最后,您可能希望以不区分大小写的方式将解密后的文本与原始文本进行比较。否则,您只能匹配全部大写的邮件。

if (dec_text == msg.upper()):

总而言之,最终程序如下所示:

def brute_force():
    msg = raw_input('Enter your text here: ')

    key = len(msg)/2

    alphabets = 'ABC2&0346_+/*~DEFGHIJK(){";.<.>LMNOPQRST:?UVWXYZ!#$%'
    cipher = ''

    msg = msg.upper()

    for chars in msg:
        if chars in alphabets:
            sol = alphabets.find(chars)
            sol = sol + key

            if sol >= len(alphabets):
                sol = sol - len(alphabets)

            elif sol < 0:
                sol = sol + len(alphabets)

            cipher = cipher + alphabets[sol]

        else:
            cipher = cipher + chars
    print('Encrypted text {}'.format(cipher))
    decrypt = cipher

    word = 'Couldnt find'
    for key in range(len(alphabets)):
        dec_text = ''

        for alphas in decrypt:
            if alphas in alphabets:
                sol_dec = alphabets.find(alphas)
                sol_dec = sol_dec - key 

                if sol_dec < 0:
                    sol_dec = sol_dec + len(alphabets)

                dec_text = dec_text + alphabets[sol_dec]

            else:
                dec_text = dec_text + alphas

        print('key {} {}'.format(key, dec_text))

        if (dec_text == msg):
            word = dec_text


    print('Decrypted text is {}'.format(word))
brute_force()

运行时,将产生结果:

Enter your text here: hello world
Encrypted text )JQQT #TUQI
key 0 )JQQT #TUQI
key 1 (IPPS !S?PH
key 2 KHOOR ZR:OG
key 3 JGNNQ YQTNF
key 4 IFMMP XPSME
key 5 HELLO WORLD
key 6 GD>>N VNQ>~
key 7 F~..M UMP.*
key 8 E*<<L ?LO</
key 9 D/..> :>N.+
key 10 ~+;;. T.M;_
key 11 *_""< S<L"6
key 12 /6{{. R.>{4
key 13 +4)); Q;.)3
key 14 _3((" P"<(0
key 15 60KK{ O{.K&
key 16 4&JJ) N);J2
key 17 32II( M("IC
key 18 0CHHK LK{HB
key 19 &BGGJ >J)GA
key 20 2AFFI .I(F%
key 21 C%EEH <HKE$
key 22 B$DDG .GJD#
key 23 A#~~F ;FI~!
key 24 %!**E "EH*Z
key 25 $Z//D {DG/Y
key 26 #Y++~ )~F+X
key 27 !X__* (*E_W
key 28 ZW66/ K/D6V
key 29 YV44+ J+~4U
key 30 XU33_ I_*3?
key 31 W?006 H6/0:
key 32 V:&&4 G4+&T
key 33 UT223 F3_2S
key 34 ?SCC0 E06CR
key 35 :RBB& D&4BQ
key 36 TQAA2 ~23AP
key 37 SP%%C *C0%O
key 38 RO$$B /B&$N
key 39 QN##A +A2#M
key 40 PM!!% _%C!L
key 41 OLZZ$ 6$BZ>
key 42 N>YY# 4#AY.
key 43 M.XX! 3!%X<
key 44 L<WWZ 0Z$W.
key 45 >.VVY &Y#V;
key 46 .;UUX 2X!U"
key 47 <"??W CWZ?{
key 48 .{::V BVY:)
key 49 ;)TTU AUXT(
key 50 "(SS? %?WSK
key 51 {KRR: $:VRJ
Decrypted text is HELLO WORLD

现在,它可以成功检测到解密的文本。

为什么dict.get(key)起作用但dict [key]不起作用? - python

我正在尝试根据字符串中存在的1将某些数字的二进制字符串分组在一起。这不起作用:s = "0 1 3 7 8 9 11 15" numbers = map(int, s.split()) binaries = [bin(x)[2:].rjust(4, '0') for x in numbers] one_groups =…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

如何更改字典中的元组值? - python

我有这本字典:d = {'a': (1, 2, 'a'), 'b': (1, 2, 'b'), 'c': (2, 4, 'c'), 'd': (1, 3, 'd'), 'e': (…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。