【问题标题】:Attempting to create an encryption program but variable seems to be resetting尝试创建加密程序,但变量似乎正在重置
【发布时间】:2017-05-24 02:10:47
【问题描述】:

我正在尝试 2 让这段代码正常工作,我的老师要我加密这些东西,以便它可以使用非字母字符,如“。”(哈哈!那个引用的东西看起来像哈利波特的金色告密者!)我认为制作带有 chrs 及其 ascii 值的字典会起作用,它确实用于加密。但是当我反转密码并尝试将 chrs 转换回来时,它不起作用。我认为一个变量正在重置,但不知道在哪里。请。帮助。

 a_cipher = {
 ' ' : '32',
 '!' : '33',
 '"' : '34',
 '#' : '35',
 '$' : '36',
 '%' : '37',
 '&' : '38',
 "'" : '39',
 '(' : '40',
 ')' : '41',
 '*' : '42',
 '+' : '43',
 ',' : '44',
 '-' : '45',
 '.' : '46',
 '/' : '47',
 '0' : '48',
 '1' : '49',
 '2' : '50',
 '3' : '51',
 '4' : '52',
 '5' : '53',
 '6' : '54',
 '7' : '55',
 '8' : '56',
 '9' : '57',
 ':' : '58',
 ';' : '59',
 '<' : '60',
 '=' : '61',
 '>' : '62',
 '?' : '63',
 '@' : '64',
 'A' : '65',
 'B' : '66',
 'C' : '67',
 'D' : '68',
 'E' : '69',
 'F' : '70',
 'G' : '71',
 'H' : '72',
 'I' : '73',
 'J' : '74',
 'K' : '75',
 'L' : '76',
 'M' : '77',
 'N' : '78',
 'O' : ' ',     
 'P' : '!',     
 'Q' : '"',     
 'R' : '#',
 'S' : '$',
 'T' : '%',
 'U' : '&',     
 'V' : '86',
 'W' : '87',
 'X' : '88',
 'Y' : '89',
 'Z' : '90',
 '[' : '91',
 '\\' : '92' ,
 ']' : '93',
 '^' : '94',
 '_' : '95',
 '`' : '96',
 'a' : '97',
 'b' : '98',
 'c' : '99',
 'd' : '100',
 'e' : '101',
 'f' : '102',
 'g' : '103',
 'h' : '104',
 'i' : '105',
 'j' : '106',
 'k' : '107',
 'l' : '108',
 'm' : '109',
 'n' : '110',
 'o' : '111',
 'p' : '112',
 'q' : '113',
 'r' : '114',
 's' : '115',
 't' : '116',
 'u' : '117',
 'v' : '118',
 'w' : '119',
 'x' : '120',
 'y' : '121',
 'z' : '122',
 '{' : '123',
 '|' : '124',
 '}' : '125',}
reverse_cipher =  {v: k for k, v in a_cipher.items()}
def encrypt(s, direction):
    output = ""
    encryptme=""
    for ch in s:
          if direction == "encrypt":
           print('ch is '+ ch + ', a_cipher[ch] is '+ a_cipher[ch])
           encryptme = output + a_cipher[ch]
           print('output is ' + str(encryptme))
           print(encryptme)
          elif direction == "decrypt":
            output= output + reverse_cipher[encryptme]
            print('output is ' + str(output))                           
    return output
a=encrypt(input("Encrypt"),"encrypt")
encrypt(a,"decrypt")

硬件分配:

扩展旋转密码 到目前为止,在每个旋转密码练习中,您都已将消息翻译成大写 加密之前的大小写字符。这简化了密码的设计,因为只有 26 字符需要旋转。 许多字符没有正确加密或根本没有加密。其中包括标点、数字、 小写字母和许多特殊字符($ & # + 等)。 在这个程序中,您将创建一个新的循环密码,该密码将利用更广泛的 人物。任务是在 ASCII 表中包含所有可显示的字符 从右大括号 } 开始的空格“ ”。 此轮换密码必须设计为具有与 Rot13 相同的主要优点,并且应该 放在 Crypto 类中。修改您的 encrypt4.py 程序以调用新的轮换 密码。将新程序另存为 encrypt5D.py。 1. 加密以下消息: 汉·索罗从达戈巴返回,率领叛军与伊沃克人反超 死星护盾发生器。通过此安全通道传输所有新订单。 2. 解密以下消息: |2CJO925O2O=:EE=6O=2>3[O=:EE=6O=2>3[O=:EE=6O=2>3]OO|2CJO925O2O=:EE=6O=2>3[O:E DO7=6646OH2DOH9:E6O2DOD?@H] 奖励(5 分):在密码中添加正确处理“换行”字符(ASCII 代码 10)。 奖励(1 分):向您的讲师发送一条额外的加密消息。 通过电子邮件将您的 encrypt5D.py 文件、crypto.py 文件以及上面 1. 和 2. 的输出发送到您的 讲师。将上面 1. 和 2. 的输出复制并粘贴到电子邮件中。”

【问题讨论】:

    标签: python-3.x encryption


    【解决方案1】:

    您可以避免所有这些字典。如果你想要一个字母的 ASCII,你可以使用 ord()。相反,您可以使用 chr()。

    chr(97) = 'a'。

    ord('a') = 97

    为什么不创建 2 个函数而不是 1 个(加密(s)和解密(代码))?这更容易理解,并且您避免了“如果”(此外,还会检查您案例中的每个字符)

    最后一点,对于解密,您将无法这样做(我的意思是简单的方式)。这是因为您的字符串的结果也将是一个字符串,您将无法轻松检查是否必须保留 1、2 或 3 个字符。

    例如,使用 ord() 加密的“加密”给出 6911099114121112116

    当您解密时,您可以以无限的方式拆分,例如: 6 - 9 - 110 - 99 - 11 - 41 -... 或者 69 - 110 - 99 - 114 - 12 - 11 - ... 或者 6 - 91 - 109 - 91 - 14 - ...

    所有这些拆分都不会提供相同的输出。为此,您应该查看字典树的复杂算法。

    一种解决方案是创建一个元素来检测拆分位置(比如说#)

    def encrypt(string):
        output = "#".join([str(ord(char)) for char in string])
        return output
    
    def decrypt(code):
        code = [int(x) for x in code.split("#")]
        output = ""
        for num in code:
            output += chr(num)
        return output
    
    s = 'Encrypt'
    a = encrypt(s) 
    print(a) # gives => 69#110#99#114#121#112#116
    t = decrypt(a)
    print(t)
    

    编辑 #1: 我忘了重播你的答案-_-' 变量不会重置,但您没有“正确”使用它们

    在你的'for'循环中你有:

    encryptme = output + a_cipher[ch]
    print('output is ' + str(encryptme))
    

    当你进入循环时

    output = ""(而且你永远不会覆盖它)

    encryptme = "" + c (c = a_cipher[ch] = 加密的字母)

    每个循环的 encryptme 都发生了变化,因为您从不连接它。

    如果你这样做:

    encryptme = encryptme  + c #(c = a_cipher[ch])
    

    每个循环,encryptme 都会采用自己的值和新的代码。这个也可以写成:encryptme += c

    编辑 #2 你的问题是我们所说的凯撒算法,可以这样做:

    def caesar(s, offset):
        a = [(ord(char) + offset)%255 for char in s]
        output = ""
        for num in a:
            output += chr(num)
        return output
    
    print(caesar('code', 13)) # => VbWX
    

    编辑 #3: 通过完整的练习,我最终得到了:

    def caesar_encrypt(s, offset):
        output = ""
        up_limit = max(ord('}'), ord(' '))
        down_limit = min(ord('}'), ord(' '))
        for char in s:
            #if the element in included in the list of valid character
            if up_limit >= ord(char) >= down_limit:
                new_char = chr(ord(char) + offset) # Shift
                output += new_char
            else:
                output += char
    
        return output
    
    def caesar_decrypt(s, offset):
        output = ""
        up_limit = max(ord('}'), ord(' '))
        down_limit = min(ord('}'), ord(' '))
        for char in s:
            # if the character was included before the offset in the range of element possible to shift
            if up_limit >= ord(char) + offset >= down_limit:
                new_char = chr(ord(char) + offset)
                output += new_char
            else:
                output += char
    
        return output
    
    s = 'Han Solo reporting in, returning from Dagobah, leading Rebel forces with Ewoks to overtake Death Star shield generator'
    print(s)
    print(caesar_encrypt(s, 13))
    i = '|2CJO925O2O=:EE=6O=2>3[O=:EE=6O=2>3[O=:EE=6O=2>3]OO|2CJ‌​O925O2O=:EE=6O=2>3[O‌​:E DO7=6646OH2DOH9:E6O2DOD?@H]'
    print(i)
    print(caesar_decrypt(i, -13))
    

    不幸的是,这看起来不太好,因为加密的解决方案没有提供合乎逻辑的东西。

    注意:我们不能一开始就使用 string.maketrans()

    希望对你有帮助

    【讨论】:

    • Thx,这很有帮助,我怎样才能使它转换成 ascii,加上 13,而不是把它变成一个字符?我需要它来配合我的学校指示。我需要解密这个:|2CJO925O2O=:EE=6O=2>3[O=:EE=6O=2>3[O=:EE=6O=2>3]OO|2CJO925O2O=:EE=6O=2 >3[O:E DO7=6646OH2DOH9:E6O2DOD?@H]
    • 我在编辑 2 中回复了,因为代码在评论中看起来不太好:D
    • 我认为我做错了什么,当我输入代码然后输入 -13 解密时,我得到这个:o%6=B,%(B%B0-880)B0 %1&NB0-880)B0%1&NB0-880)B0%1&PBBo%6=B,%(B%B0-880)B0%1&NB-87B*0))')B;%7B;,-8)B%7B723 ;P
    • 我已经在你的代码上尝试了几个偏移量(从 -32 到 +32),但我没有找到任何“合乎逻辑”的句子。你能粘贴你的练习吗?我不确定你必须应用凯撒算法
    • 哦,我明白了,这不是我们所做的……在这种情况下,我们可以使用 string 库的另一个函数,称为 maketrans。我会尽量轻松找到它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多