seg/trabalho1/vernam.py

34 lines
912 B
Python

import sys
if len(sys.argv) != 3:
print("Usage: python3 vernam.py [arquivo de chave] [arquivo string]")
sys.exit(1)
key = open(sys.argv[1], 'r', newline='\n').read()
string = open(sys.argv[2], 'r', newline='\n').read()
if len(string) != len(key):
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]")
encrypted = ""
for i in range(len(string)):
encrypted += chr(ord(string[i]) ^ ord(key[i]))
# print(ord(string[i]) ^ ord(key[i]))
print(encrypted)
with open('vernam.txt', 'w+', newline='\n') as f:
f.write(encrypted)
# python3 vernam.py ABCDEF TOMATE | xargs python3 vernam.py ABCDEF