Python 3.x 试图创建加密程序,但变量似乎正在重置

Python 3.x 试图创建加密程序,但变量似乎正在重置,python-3.x,encryption,Python 3.x,Encryption,我正在试着让这个代码正常工作,我的老师想让我加密这些东西,这样它就可以使用非字母的chr,比如“.”(哈哈!那句话看起来像是哈利波特的金色飞贼!)我想制作一本包含chr和它们的ascii值的字典会很有效,加密就行了。但是,当我反转密码并尝试将CHR转换回时,它不起作用。我想vriable正在重置,但idk在何处。请。救命啊 a_cipher = { ' ' : '32', '!' : '33', '"' : '34', '#' : '35', '$' : '36', '%' : '

我正在试着让这个代码正常工作,我的老师想让我加密这些东西,这样它就可以使用非字母的chr,比如“.”(哈哈!那句话看起来像是哈利波特的金色飞贼!)我想制作一本包含chr和它们的ascii值的字典会很有效,加密就行了。但是,当我反转密码并尝试将CHR转换回时,它不起作用。我想vriable正在重置,但idk在何处。请。救命啊

 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")
a_密码={
' ' : '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',
'' : '62',
'?' : '63',
'@' : '64',
‘A’:‘65’,
‘B’:‘66’,
‘C’:‘67’,
‘D’:‘68’,
‘E’:‘69’,
‘F’:‘70’,
‘G’:‘71’,
‘H’:‘72’,
‘我’:‘73’,
"J":"74",,
‘K’:‘75’,
‘L’:‘76’,
‘M’:‘77’,
‘N’:‘78’,
“O:”,
“P:”!“,
‘Q’:‘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代表k,v在_cipher.items()中
def加密(s,方向):
output=“”
encryptme=“”
对于ch in s:
如果方向==“加密”:
print('ch是'+ch+',a_-cipher[ch]是'+a_-cipher[ch])
encryptme=输出+a_密码[ch]
打印('输出为'+str(encryptme))
打印(加密)
elif方向==“解密”:
输出=输出+反向密码[encryptme]
打印('输出为'+str(输出))
返回输出
a=加密(输入(“加密”),“加密”)
加密(一种“解密”)
硬件分配:

扩展旋转密码 到目前为止,在每个旋转密码练习中,您都已将消息翻译为大写字母 加密前请输入大小写字符。这简化了密码的设计,因为只有26个密码 需要旋转的字符。 许多字符没有正确加密或根本没有加密。包括标点符号,数字,, 小写字母和许多特殊字符($&#+及更多)。 在本程序中,您将创建一个新的旋转密码,该密码将使用更广泛的 人物。分配包括ASCII表中的所有可显示字符 从右大括号}中的空格“”开始。 此旋转密码必须设计为具有与Rot13相同的主要优点,并且应该 被置于加密类中。修改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.的输出复制并粘贴到电子邮件中。“

您可以避免所有这些措辞。如果您想要字母的ASCII码,可以使用ord()。相反,可以使用chr()

chr(97)=“a”

ord('a')=97

为什么不创建2个函数而不是1个(加密(s)和解密(代码))?这更容易理解,并且可以避免使用“if”(在您的案例中,每个字符都会检查它)

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

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

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

所有这些拆分不会提供相同的输出。要做到这一点,您应该看看字典树的算法,它很复杂

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

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

在“for”循环中,您有:

encryptme = output + a_cipher[ch]
print('output is ' + str(encryptme))
当你进入循环时

output=“”(并且您从不覆盖它)

encryptme=“”+c(c=a_密码[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))
不幸的是,这看起来并不好,因为加密的解决方案没有提供逻辑性

注意:我们无法在第一个sig中按预期使用string.maketrans()
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))