2015-10-03 47 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'