def transform(raw):
return ''.join(i.upper() for i in raw if i.isalpha())
def create_transformer(number, dictionary):
return dict(zip(dictionary, dictionary[number:] + dictionary[:number]))
def encryption(word, key, dictionary):
(word, length, transformers) = (transform(word), len(key), [create_transformer(ord(i) - 65, dictionary) for i in transform(key)])
return ''.join(transformers[i % length][word[i]] for i in range(len(word)))
if __name__ == '__main__':
print('加密结果:', encryption(input('请输入要加密的文字: '), input('密钥: '), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'))
|