1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class FenceCipher: def encryption(self, plaintext, space): ''' 加密函数 ''' cyphertext = '' for i in range(0, space): for j in range(i, len(plaintext), space): if j < len(plaintext): cyphertext += plaintext[j] return cyphertext
def decryption(self, cyphertext, space): ''' 解密函数 ''' plaintext = '' if len(cyphertext) % space == 0: key = len(cyphertext) // space else: key = len(cyphertext) // space + 1 for i in range(0, key): for j in range(i, len(cyphertext), key): if j < len(cyphertext): plaintext += cyphertext[j] return plaintext
t_plain = '12.25 HappyBirthday!' t_space = 3 t = FenceCipher() print('明文是:', t_plain) t_cypher = t.encryption(t_plain, t_space) print('密文是:', t_cypher) t_result = t.decryption(t_cypher, t_space) print('解密结果是:', t_result)
|