2015-10-03 48 views
0

这是我用来编码它我将如何解码由此程序编码的字符串?

import string 

def encode(text,rotate_by): 
    s_from = string.ascii_lowercase + string.ascii_uppercase 
    s_to = (string.ascii_lowercase[rotate_by:] + 
      string.ascii_lowercase[:rotate_by] + 
      string.ascii_uppercase[rotate_by:] + 
      string.ascii_uppercase[:rotate_by]) 
    cypher_table = string.maketrans(s_from, s_to) 
    cypher_table = string.maketrans(s_from, s_to) 
    return text.translate(cypher_table) 


text = raw_input("Enter the text to encode: ") 
rotate_by = int(raw_input("Rotate by: ")) 
print encode(text, rotate_by) 

我将如何解码的字符串通过这个编码的代码?

+1

刚刚反转逻辑 –

回答

0

这是一个非常简单的旋转编码。你只需要扭转旋转。

我想试试这个:

def decode(text, rotate_by): 
    s_from = string.ascii_lowercase + string.ascii_uppercase 
    s_to = (string.ascii_lowercase[rotate_by:] + 
     string.ascii_lowercase[:rotate_by] + 
     string.ascii_uppercase[rotate_by:] + 
     string.ascii_uppercase[:rotate_by]) 
    reverse_table = string.maketrans(s_to, s_from) 
    return text.translate(reverse_table) 

BTW:在你的编码中,string.maketrans行是重复的。

上面的重要行是maketrans-line,因为我只是颠倒了参数,这也应该反转翻译表。其他一切与原始代码相似或相同。

0

只需调用编码(加密)字符串和原始rotate_by与其反转符号的编码。

例:

>>> encode("hello",3) 
'khoor' 
>>> encode("khoor",-3) 
'hello'