在python 3中使用ASCII加密

问题描述:

你好,并提前致谢。在python 3中使用ASCII加密

我正在努力使一个密码学程序,我必须为学校做。我不是高级Python专家,所以如果这是一个愚蠢的问题,我很抱歉。当我运行这个程序并插入例如abc与班次2它将返回cde这是很好的。但我试图插入xyz以及移位3,而不是正确移位abc它返回aaa。这也会发生,如果我使用shift 2然后它返回zaa。我怎么能当字母与ASCII TABEL

shift = int(input("Please insert a number you want to shift the characters with: ")) 

end = "" 

for x in alf: 
    ascii = ord(x) 

if ascii >= 97 and ascii <= 122: 
    res = ascii + shift 
    if res > 122: 
     res = 0 + 97 
     min = res + shift 
    end = end + chr(min) 

print (end)         
+3

如果'res'大于122则将其设置为97的固定值。 – Matthias

这是因为你的逻辑表达式是错误的做调整我的程序正确地从头开始。这里是一个例子,它将允许任何正整数作为右移,从再次开始。它可以非常优化(提示:使用模运算符),但是这是对你的代码和数字大写的大的改动。

for x in alf: 
    ascii = ord(x) 

    if ascii >= 97 and ascii <= 122: 
    res = ascii + shift 
    while res > 122: 
     res = res - (122 - 97) - 1 
    end = end + chr(res) 
+0

非常感谢stefan! –