-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
38 lines (31 loc) · 1 KB
/
cipher.py
File metadata and controls
38 lines (31 loc) · 1 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
36
37
38
from util_function import *
import random
# read input
def read_input(message_set):
message_num = int(input())
for _ in range(message_num):
message_set.append(input())
return message_num
# entrypy the message with given key
def encrypt(key, message):
cipher_text = strxor(key, message)
return cipher_text
# print a single encrypted message
def print_single_encrypted(cipher_text):
cipher_text_hex = ""
for char in cipher_text:
cipher_text_hex = cipher_text_hex + "{:02x}".format(ord(char))
print(cipher_text_hex)
# main function
def main():
message_set = []
message_num = read_input(message_set)
max_size = 2 * len(max(message_set, key = len))
random_ascii = [hex(random.randint(0, 15)).split('x')[-1] for _ in range(max_size)]
key = ''.join(random_ascii)
print(message_num)
for message in message_set:
cipher_text = encrypt(key, message)
print_single_encrypted(cipher_text)
if __name__ == "__main__":
main()