-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc2.py
40 lines (37 loc) · 1.38 KB
/
rc2.py
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
import os
import time
start = time.time()
from Crypto.Cipher import ARC2
key = 'Sixteen byte key'
def encrypt_file(in_filename, out_filename, chunk_size, key, iv):
arc2 = ARC2.new(key, ARC2.MODE_CFB, iv)
with open(in_filename, 'r') as in_file:
with open(out_filename, 'w') as out_file:
while True:
chunk = in_file.read(chunk_size)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
out_file.write(arc2.encrypt(chunk))
def decrypt_file(in_filename, out_filename, chunk_size, key, iv):
arc2 = ARC2.new(key, ARC2.MODE_CFB, iv)
with open(in_filename, 'r') as in_file:
with open(out_filename, 'w') as out_file:
while True:
chunk = in_file.read(chunk_size)
if len(chunk) == 0:
break
out_file.write(arc2.decrypt(chunk))
from Crypto import Random
iv = Random.get_random_bytes(8)
with open('to_enc.txt', 'r') as f:
print 'to_enc.txt: %s' % f.read()
encrypt_file('to_enc.txt', 'to_enc.enc', 8192, key, iv)
with open('to_enc.enc', 'r') as f:
print 'to_enc.enc: %s' % f.read()
decrypt_file('to_enc.enc', 'to_enc.dec', 8192, key, iv)
with open('to_enc.dec', 'r') as f:
print 'to_enc.dec: %s' % f.read()
end = time.time()
print(end - start)