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
def double_encrypt(msg, key1, key2):
ciphertext1 = encrypt(msg.encode(), key1)
ciphertext2 = encrypt(ciphertext1, key2)
return ciphertext2

def double_decrypt(ciphertext, key1, key2):
plaintext1 = decrypt(ciphertext, key1)
plaintext2 = decrypt(plaintext1, key2)
return plaintext2.decode()

def encrypt(msg, key):
key = key.encode()
ciphertext = bytearray()
for i, b in enumerate(msg):
ciphertext.append((b + key[i % len(key)]) % 256)
return bytes(ciphertext)

def decrypt(ciphertext, key):
key = key.encode()
plaintext = bytearray()
for i, b in enumerate(ciphertext):
plaintext.append((b - key[i % len(key)]) % 256)
return bytes(plaintext)

msg = '你好,世界!'
key1 = '密码1'
key2 = '99密码999'

ciphertext = double_encrypt(msg, key1, key2)
# print('Ciphertext:', ciphertext)

plaintext1 = double_decrypt(ciphertext, key1, key2)
print('Plaintext1:', plaintext1)
plaintext2 = double_decrypt(ciphertext, key2, key1)
print('Plaintext2:', plaintext2)

#>>> Plaintext1: 你好,世界!
#>>> Plaintext2: 你好,世界!