遇到这个Affine Cipher代码有点麻烦

时间:2015-02-17 01:00:44

标签: python

chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.".lower()
ext_alphabet = dict(zip(chars, range(len(chars))))
rev_alphabet = dict(zip(range(len(chars)), chars))

def find_coprime(a):
    for i in range(39):
        if ((i * a) % 39) == 1:
            return i

def encrypt():
    key1 = input("Enter the multiplictive key: ")
    key2 = input("Enter additive key: ")
    msg = raw_input("Enter message: ")
    ciphertext = [ rev_alphabet[(key1 * ext_alphabet[i] + key2) % 39] for i in msg ]   
    return ''.join(ciphertext)

def decrypt():
    key1 = input("Enter the additive key: ")
    key2 = input("Enter multiplictive key: ")
    msg = raw_input("Enter message: ")
    co_prime = find_coprime(key1)
    plaintext = [ rev_alphabet[(co_prime * (ext_alphabet[i] - key2)) % 39] for i in msg ]
    return ''.join(plaintext)



print encrypt()

有两个测试用例。第一个工作,这是“快乐编码”,多个1和一个9的加法,但第二个不起作用,“今天是5月15日”,多个2和3的加法。我不知道为什么第二个测试用例不起作用。

0 个答案:

没有答案