以前制作的加密算法的解密算法 - python

Decryption algorithm for a previously made encryption algorithm - python

所以我创建了一个自定义加密算法,我称之为 Grid。 这是代码:

    print('')
    print("Welcome to the Project Grid encryption tool.")
    print("Note that this encryption method is not completely secure.")
    print("Anyone with this program can learn the encryption.")
    print("Encrypted documents may be confused if numbers are used.")
    print("Happy Encrypting!")
    print("")
    e = raw_input()
    o = e.lower()
    list(o)
    o = [s.replace('a','11') for s in o]  #all of this is just to do the     encryption
o = [s.replace('b','12') for s in o]
o = [s.replace('c','13') for s in o]
o = [s.replace('d','14') for s in o]
o = [s.replace('e','15') for s in o]
o = [s.replace('f','21') for s in o]
o = [s.replace('g','22') for s in o]
o = [s.replace('h','23') for s in o]
o = [s.replace('i','24') for s in o]
o = [s.replace('j','25') for s in o]
o = [s.replace('k','31') for s in o]
o = [s.replace('l','32') for s in o]
o = [s.replace('m','33') for s in o]
o = [s.replace('n','34') for s in o]
o = [s.replace('o','35') for s in o]
o = [s.replace('p','41') for s in o]
o = [s.replace('q','42') for s in o]
o = [s.replace('r','43') for s in o]
o = [s.replace('s','44') for s in o]
o = [s.replace('t','45') for s in o]
o = [s.replace('u','51') for s in o]
o = [s.replace('v','52') for s in o]
o = [s.replace('w','53') for s in o]
o = [s.replace('x','54') for s in o]
o = [s.replace('y','55') for s in o]
o = [s.replace('z','61') for s in o]    
o = [s.replace('.', '') for s in o]   #This just removes any punctuation, so as to not mess the cypher up.
o = [s.replace('?', '') for s in o]
o = [s.replace('!', '') for s in o]
o = [s.replace("'", '') for s in o]
o = [s.replace('"', '') for s in o]
o = [s.replace(',', '') for s in o]
o = [s.replace(' ', '.') for s in o]

f = ''.join(o)
print f

with open('encrypted.txt', 'a') as the_file:   #writes the encryption to a file
    the_file.write(f + ' <br> ')

这是我的问题:我正在研究解密算法,但在将它 returns 的数字行转换回包含代码的字母时遇到了很多麻烦,因此不能让它真正解密。这是解密代码。

print('')
print("Welcome to the Project Grid decryption tool.")
print("Happy Decrypting!")
print("")
r = raw_input()
r = [s.replace('.', '62') for s in r]
x = [int(i) for i in r]
n = 2
o = [x[i:i+n] for i in range(0, len(x), n)]
print o

o = [s.replace('11','a') for s in o]
o = [s.replace('12','b') for s in o]
o = [s.replace('13','c') for s in o]
o = [s.replace('14','d') for s in o]
o = [s.replace('15','e') for s in o]
o = [s.replace('21','f') for s in o]
o = [s.replace('22','g') for s in o]
o = [s.replace('23','h') for s in o]
o = [s.replace('24','i') for s in o]
o = [s.replace('25','j') for s in o]
o = [s.replace('31','k') for s in o]
o = [s.replace('32','l') for s in o]
o = [s.replace('33','m') for s in o]
o = [s.replace('34','n') for s in o]
o = [s.replace('35','o') for s in o]
o = [s.replace('41','p') for s in o]
o = [s.replace('42','q') for s in o]
o = [s.replace('43','r') for s in o]
o = [s.replace('44','s') for s in o]
o = [s.replace('45','t') for s in o]
o = [s.replace('51','u') for s in o]
o = [s.replace('52','v') for s in o]
o = [s.replace('53','w') for s in o]
o = [s.replace('54','x') for s in o]
o = [s.replace('55','y') for s in o]
o = [s.replace('61','z') for s in o]
o = [s.replace('62',' ') for s in o]    

f = ''.join(o)
print f

with open('decrypted.txt', 'a') as the_file:
    the_file.write(f + ' <br> ')

如有任何帮助,我们将不胜感激。此外,您所做的任何减肥也可能有所帮助,只是因为我觉得 has 是更好的方法。 提前谢谢大家! -理查德

你的算法的问题是你转换成的数字不是 independent/orthogonal 或任何可能被调用的数字。以单词 pass 为例,它将被编码为 41114444。然后是您的解码算法,首先解码所有 a=11,这意味着在解码第一个符号后,您的半解码字符串将是 4a14444。接下来它将匹配 14 = d 结果为 4ad444 最后匹配 44 = s,所以 pass 变成了 4ads4 .

要解决此问题(使用示例中的代码字),您需要在解码过程中从左到右扫描编码字符串,然后一次解码一个符号

def conv_sym(o):
    o = o.replace('11','a')
    o = o.replace('12','b')
    o = o.replace('13','c') 
    o = o.replace('14','d')
    o = o.replace('15','e') 
    o = o.replace('21','f') 
    o = o.replace('22','g') 
    o = o.replace('23','h') 
    o = o.replace('24','i') 
    o = o.replace('25','j') 
    o = o.replace('31','k') 
    o = o.replace('32','l')   
    o = o.replace('33','m') 
    o = o.replace('34','n') 
    o = o.replace('35','o') 
    o = o.replace('41','p') 
    o = o.replace('42','q') 
    o = o.replace('43','r')
    o = o.replace('44','s')
    o = o.replace('45','t') 
    o = o.replace('51','u') 
    o = o.replace('52','v') 
    o = o.replace('53','w')
    o = o.replace('54','x') 
    o = o.replace('55','y') 
    o = o.replace('61','z') 
    o = o.replace('62',' ') 
    return o

enc_str = "41114444"
dec_str = ""
while len(enc_str) > 0:
    sym = enc_str[:2]
    dec_str += conv_sym(sym)
    enc_str = enc_str[2:]
print (dec_str)

但更好的方法是将符号 table 存储在字典中,然后在那里查找正确的翻译。这是与上面相同的示例,但使用这种方法

dec_table = {'11': 'a', '41': 'p', '44': 's'}
enc_str = "41114444"
dec_str = ""

while len(enc_str) > 0:
    sym = enc_str[:2]
    dec_str += dec_table[sym]
    enc_str = enc_str[2:]

print (dec_str)

编码时也可以采用这种方法

orig_str = "pass"
enc_str = ""
dec_str = ""

enc_table = {'s': '44', 'a': '11', 'p': '41'}
dec_table = dict (zip(enc_table.values(),enc_table.keys()))

print ("Original: ", orig_str)
# Encode
for i in orig_str:
    enc_str += enc_table[i];

print ("Encoded: ", enc_str)

while len(enc_str) > 0:
    sym = enc_str[:2]
    dec_str += dec_table[sym]
    enc_str = enc_str[2:]

print ("Decoded: ", dec_str)

这个输出是

Original:  pass
Encoded:  41114444
Decoded:  pass