-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathrepeating-key-xor
executable file
·42 lines (33 loc) · 1.33 KB
/
repeating-key-xor
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
39
40
41
42
#!/usr/bin/python3
from BitVector import BitVector
import string
import sys
import pprint
import logging
import itertools
logging.basicConfig(level=logging.DEBUG)
BIT = 1
BYTE = 8 * BIT
def encrypt_xor(input_bv, key_bv):
input_len = len(input_bv)
key_len = len(key_bv)
encrypted_bv = BitVector(size=input_len)
for i, _ in enumerate(input_bv):
# '+' here is concat, not sum
encrypted_bv[i] = input_bv[i] ^ key_bv[i%key_len]
return encrypted_bv
if __name__ == "__main__":
# ./repeating-key-xor clear.txt key.txt cipher.txt
cleartext_file_path = sys.argv[1]
key_file_path = sys.argv[2]
ciphertext_file_path = sys.argv[3]
key_bv = None
with open(key_file_path, "rt") as key_file:
key_bv = BitVector(textstring=key_file.read())
logging.info(f"Key: {key_bv.get_bitvector_in_ascii()}")
with open(cleartext_file_path, "rb") as input_file, open(ciphertext_file_path, "wt") as output_file:
for i, input_b in enumerate(input_file.read()):
key_index = (i%int(len(key_bv)/BYTE)) * BYTE
# print(f"taking bits from {key_index} to {key_index+BYTE} of {len(key_bv)}")
ciphertext = BitVector(intVal=input_b, size=BYTE) ^ BitVector(bitlist=key_bv[key_index:key_index+BYTE])
output_file.write(ciphertext.get_bitvector_in_hex())