seg/trabalho1/vernam.py

34 lines
912 B
Python
Raw Normal View History

2023-08-23 12:38:41 +00:00
import sys
if len(sys.argv) != 3:
2023-08-31 02:03:29 +00:00
print("Usage: python3 vernam.py [arquivo de chave] [arquivo string]")
2023-08-23 12:38:41 +00:00
sys.exit(1)
2023-08-31 02:03:29 +00:00
key = open(sys.argv[1], 'r', newline='\n').read()
string = open(sys.argv[2], 'r', newline='\n').read()
2023-08-23 12:38:41 +00:00
if len(string) != len(key):
2023-08-31 02:03:29 +00:00
print(f"Key and string must have the same length [{len(string)}] vs [{len(key)}]")
print("Generating random key...")
import random
key = ""
for i in range(len(string)):
key += chr(random.randint(0, 255))
with open('key.txt', 'w+', newline='\n') as f:
f.write(key)
print("Key generated and saved to [key.txt]")
2023-08-23 12:38:41 +00:00
encrypted = ""
for i in range(len(string)):
encrypted += chr(ord(string[i]) ^ ord(key[i]))
# print(ord(string[i]) ^ ord(key[i]))
print(encrypted)
2023-08-31 02:03:29 +00:00
with open('vernam.txt', 'w+', newline='\n') as f:
f.write(encrypted)
2023-08-23 12:38:41 +00:00
# python3 vernam.py ABCDEF TOMATE | xargs python3 vernam.py ABCDEF