Skip to content

Instantly share code, notes, and snippets.

@shaiguitar

shaiguitar/odrive-decrypt.py Secret

Last active Feb 28, 2017
Embed
What would you like to do?
odrive-decrypt.py
import sys
import pdb
import os
import base64
import hashlib
from Crypto.Cipher import AES
def cp_trim_headers_to(filedst, filesrc, bytes_to_trim):
command = "dd bs=%s skip=1 if=%s of=%s" % (bytes_to_trim, filesrc, filedst)
print("Running: %s" % command)
os.system(command)
def print_variable(var, name):
print "Derived %s, len:" % name
print var
print
print len(var)
HEADER_SIZE_BYTES = 25
SOURCE_PATH = "/Users/shai/Downloads/McFbqdhP_Ti8rM8a9lO7td1fP1_yf0uPkrOmHjrM_6hVBzIzO8CWfafrAzcuZPbxJdaxl1AtQeL-"
#
# START DO DECRYPTING WORK...
#
# get basic vars from crypto info from file header
with open(SOURCE_PATH, 'rb') as infile:
v = infile.read(1)
s = infile.read(8)
iv = infile.read(16)
print_variable(v, "v")
print_variable(s, "s")
print_variable(iv, "iv")
password = os.environ['ODRIVE_PASS']
# Calculate a 256-bit key (K) using PBKDF2 with S and the user's passphrase (P) using HMAC-SHA256 for PRF
# Derive k using s & p
k = hashlib.pbkdf2_hmac('sha256', password, s, 1)
print_variable(k, "k")
# get crypto text.
# create file with just crypted text first
dest = "/tmp/%s.txt" % os.path.basename(SOURCE_PATH)
cp_trim_headers_to(dest, SOURCE_PATH, 25)
print
# CT
file = open(dest, 'rb')
cipher_text = file.read()
file.close()
# decrypt CT using K and IV
# enc = base64.b64decode(enc)
decryption_suite = AES.new(k, AES.MODE_CBC, iv)
decrypted = decryption_suite.decrypt(cipher_text)
print decrypted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment