-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_5.py
More file actions
54 lines (40 loc) · 1.31 KB
/
test_5.py
File metadata and controls
54 lines (40 loc) · 1.31 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import unittest
import sys
from util.somecode import *
import math
from bitstring import BitArray as BA
def xor_cycle_encrypt(key:BA, m:BA) -> BA:
e = key * math.ceil(float(len(m))/len(key))
e = e[:len(m)]
assert len(e) == len(m)
return e ^ m
def xor_encrypt(key_text, ms) -> str:
key_ = BA(key_text.encode())
encrypt = xor_cycle_encrypt(key_, BA(ms.encode()))
return encrypt.hex
def xor_encrypt_ba(key_text: bytes, ms: bytes) -> BA:
key_ = BA(key_text)
encrypt = xor_cycle_encrypt(key_, BA(ms))
return encrypt
class TestChallenge5(unittest.TestCase):
def test_example(self):
x = b'Burning \'em, if you ain\'t quick and nimble\nI go crazy when I hear a cymbal'
e = xor_encrypt_ba(b'ICE', x)
a_ = BA("0x0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f")
self.assertEqual(e, a_)
@unittest.SkipTest
def test_end_to_end(self):
e = xor_encrypt_ba(b'ICE', SAMPLE_TEXT.encode())
print(to_str(e))
t = top_n_key_sizes(20, e)
pprint(t)
blocks = transpose(e, 3)
key = ""
for i, b in enumerate(blocks):
key += best_decrypt_key(b)[1]
print(key)
m = to_str(xor_cycle_encrypt(BA(key.encode()), e))
print(m)
if __name__ == "__main__":
key = sys.argv[1]
print(xor_encrypt(key, sys.stdin.read()))