-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
35 lines (32 loc) · 1.34 KB
/
cipher.py
File metadata and controls
35 lines (32 loc) · 1.34 KB
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
import random
import base64
async def generate_key():
'''
Generates a random 5 letter/digit key
'''
key = "".join((chr(random.randint(48, 57))if random.choice([True, False])else(chr(random.randint(65, 90))if random.choice([True, False])else chr(random.randint(97, 122))))for _ in range(5))
return key
async def xor_cipher(text: str, key: str):
'''
Encrypts the text to base64 using the XOR cipher with the key
'''
cipher_bytes = bytearray()
key_length = len(key)
for i, char in enumerate(text):
text_byte = ord(char) # convert char to byte
key_byte = ord(key[i % key_length]) # convert key to byte
xor_result = text_byte ^ key_byte # XOR text byte and key byte
cipher_bytes.append(xor_result) # store to bytearray
cipher_base64 = base64.b64encode(cipher_bytes).decode("utf-8") # encode byte array to base64
return cipher_base64
async def xor_decipher(cipher: str, key: str):
'''
Decrypts the base64 encoded XOR cipher with the key
'''
cipher_bytes = base64.b64decode(cipher)
text = ""
key_length = len(key)
for i, byte in enumerate(cipher_bytes):
xor_result = byte ^ ord(key[i % key_length])
text += chr(xor_result)
return text