Skip to content

Commit 4028f6d

Browse files
committed
Fromdos-ify
1 parent 15a8b92 commit 4028f6d

File tree

2 files changed

+104
-104
lines changed

2 files changed

+104
-104
lines changed
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
from Crypto.Cipher import AES
2-
import hashlib
3-
4-
password = b'qwerty12345'
5-
key = hashlib.sha256(password).digest()
6-
key = b'0123456789abcdef'
7-
8-
IV = 16 * '\x00'
9-
mode = AES.MODE_CBC
10-
encryptor = AES.new(key, mode, IV=IV)
11-
12-
text = b'j' * 32 + b'i' * 64
13-
ciphertext = encryptor.encrypt(text)
14-
15-
decryptor = AES.new(key, mode, IV=IV)
16-
plain = decryptor.decrypt(ciphertext)
17-
print(plain)
1+
from Crypto.Cipher import AES
2+
import hashlib
3+
4+
password = b'qwerty12345'
5+
key = hashlib.sha256(password).digest()
6+
key = b'0123456789abcdef'
7+
8+
IV = 16 * '\x00'
9+
mode = AES.MODE_CBC
10+
encryptor = AES.new(key, mode, IV=IV)
11+
12+
text = b'j' * 32 + b'i' * 64
13+
ciphertext = encryptor.encrypt(text)
14+
15+
decryptor = AES.new(key, mode, IV=IV)
16+
plain = decryptor.decrypt(ciphertext)
17+
print(plain)
Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
1-
import os
2-
import struct
3-
import random
4-
from eblib.utils import Timer
5-
from Crypto.Cipher import AES
6-
7-
8-
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
9-
""" Encrypts a file using AES (CBC mode) with the
10-
given key.
11-
12-
key:
13-
The encryption key - a bytes object that must be
14-
either 16, 24 or 32 bytes long. Longer keys
15-
are more secure.
16-
17-
in_filename:
18-
Name of the input file
19-
20-
out_filename:
21-
If None, '<in_filename>.enc' will be used.
22-
23-
chunksize:
24-
Sets the size of the chunk which the function
25-
uses to read and encrypt the file. Larger chunk
26-
sizes can be faster for some files and machines.
27-
chunksize must be divisible by 16.
28-
"""
29-
if not out_filename:
30-
out_filename = in_filename + '.enc'
31-
32-
iv = os.urandom(16)
33-
encryptor = AES.new(key, AES.MODE_CBC, iv)
34-
filesize = os.path.getsize(in_filename)
35-
36-
with open(in_filename, 'rb') as infile:
37-
with open(out_filename, 'wb') as outfile:
38-
outfile.write(struct.pack('<Q', filesize))
39-
outfile.write(iv)
40-
41-
while True:
42-
chunk = infile.read(chunksize)
43-
if len(chunk) == 0:
44-
break
45-
elif len(chunk) % 16 != 0:
46-
chunk += b' ' * (16 - len(chunk) % 16)
47-
48-
outfile.write(encryptor.encrypt(chunk))
49-
return out_filename
50-
51-
52-
def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
53-
""" Decrypts a file using AES (CBC mode) with the
54-
given key. Parameters are similar to encrypt_file,
55-
with one difference: out_filename, if not supplied
56-
will be in_filename without its last extension
57-
(i.e. if in_filename is 'aaa.zip.enc' then
58-
out_filename will be 'aaa.zip')
59-
"""
60-
if not out_filename:
61-
out_filename = os.path.splitext(in_filename)[0]
62-
63-
with open(in_filename, 'rb') as infile:
64-
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
65-
iv = infile.read(16)
66-
decryptor = AES.new(key, AES.MODE_CBC, iv)
67-
68-
with open(out_filename, 'wb') as outfile:
69-
while True:
70-
chunk = infile.read(chunksize)
71-
if len(chunk) == 0:
72-
break
73-
outfile.write(decryptor.decrypt(chunk))
74-
75-
outfile.truncate(origsize)
76-
return out_filename
77-
78-
79-
if __name__ == "__main__":
80-
pwd = b'1' * 32
81-
filename = 'pycrypto_file.py'
82-
83-
with Timer('encrypt'):
84-
enc_filename = encrypt_file(pwd, filename)
85-
86-
with Timer('decrypt'):
87-
decrypt_file(pwd, enc_filename, out_filename=filename+'.dec')
1+
import os
2+
import struct
3+
import random
4+
from eblib.utils import Timer
5+
from Crypto.Cipher import AES
6+
7+
8+
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
9+
""" Encrypts a file using AES (CBC mode) with the
10+
given key.
11+
12+
key:
13+
The encryption key - a bytes object that must be
14+
either 16, 24 or 32 bytes long. Longer keys
15+
are more secure.
16+
17+
in_filename:
18+
Name of the input file
19+
20+
out_filename:
21+
If None, '<in_filename>.enc' will be used.
22+
23+
chunksize:
24+
Sets the size of the chunk which the function
25+
uses to read and encrypt the file. Larger chunk
26+
sizes can be faster for some files and machines.
27+
chunksize must be divisible by 16.
28+
"""
29+
if not out_filename:
30+
out_filename = in_filename + '.enc'
31+
32+
iv = os.urandom(16)
33+
encryptor = AES.new(key, AES.MODE_CBC, iv)
34+
filesize = os.path.getsize(in_filename)
35+
36+
with open(in_filename, 'rb') as infile:
37+
with open(out_filename, 'wb') as outfile:
38+
outfile.write(struct.pack('<Q', filesize))
39+
outfile.write(iv)
40+
41+
while True:
42+
chunk = infile.read(chunksize)
43+
if len(chunk) == 0:
44+
break
45+
elif len(chunk) % 16 != 0:
46+
chunk += b' ' * (16 - len(chunk) % 16)
47+
48+
outfile.write(encryptor.encrypt(chunk))
49+
return out_filename
50+
51+
52+
def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
53+
""" Decrypts a file using AES (CBC mode) with the
54+
given key. Parameters are similar to encrypt_file,
55+
with one difference: out_filename, if not supplied
56+
will be in_filename without its last extension
57+
(i.e. if in_filename is 'aaa.zip.enc' then
58+
out_filename will be 'aaa.zip')
59+
"""
60+
if not out_filename:
61+
out_filename = os.path.splitext(in_filename)[0]
62+
63+
with open(in_filename, 'rb') as infile:
64+
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
65+
iv = infile.read(16)
66+
decryptor = AES.new(key, AES.MODE_CBC, iv)
67+
68+
with open(out_filename, 'wb') as outfile:
69+
while True:
70+
chunk = infile.read(chunksize)
71+
if len(chunk) == 0:
72+
break
73+
outfile.write(decryptor.decrypt(chunk))
74+
75+
outfile.truncate(origsize)
76+
return out_filename
77+
78+
79+
if __name__ == "__main__":
80+
pwd = b'1' * 32
81+
filename = '/tmp/file.my.enc'
82+
83+
#with Timer('encrypt'):
84+
#enc_filename = encrypt_file(pwd, filename)
85+
86+
with Timer('decrypt'):
87+
decrypt_file(pwd, filename, out_filename=filename+'.dec')

0 commit comments

Comments
 (0)