-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecrypt.py
More file actions
42 lines (33 loc) · 1.15 KB
/
decrypt.py
File metadata and controls
42 lines (33 loc) · 1.15 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
import sys
import time
from Crypto.Cipher import AES
BLOC = 16
buf = open(sys.argv[1], "rb").read()
key = buf[:BLOC]
iv = buf[BLOC:BLOC*2]
data = buf[BLOC*2:]
pad = 0
if (len(data) % BLOC) > 0:
# Decrypt the second-to-last ciphertext block using ECB mode
pad = BLOC - (len(data) % BLOC)
secondToLast = data[len(data)-2*BLOC+pad:len(data)-BLOC+pad]
dec = AES.new(key, AES.MODE_ECB).decrypt(secondToLast)
# Pad the ciphertext to the nearest multiple of the block size
# using the last B−M bits of block cipher decryption
# of the second-to-last ciphertext block
data += bytes(dec[len(dec)-pad:len(dec)])
# Swap the last two ciphertext blocks
data = data[:len(data)-2*BLOC] + data[len(data)-BLOC:] + data[len(data)-2*BLOC:len(data)-BLOC]
# Decrypt the (modified) ciphertext using the standard CBC mode
index = 0
decd = b''
cipher = AES.new(key, AES.MODE_CBC, iv)
while index < len(data):
decd += cipher.decrypt(data[index:index+BLOC])
index += BLOC
# Truncate the plaintext to the length of the original ciphertext
if pad != 0:
decd = decd[:len(decd)-pad]
out = open(sys.argv[2], "wb")
out.write(decd)
out.close()