75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
#!/usr/bin/python
|
|
import base64
|
|
from Crypto.Cipher import AES
|
|
from Crypto.PublicKey import RSA
|
|
from Crypto.Cipher import PKCS1_OAEP
|
|
import json
|
|
import tarfile
|
|
|
|
def generate_RSA(bits=2048):
|
|
'''
|
|
Generate an RSA keypair with an exponent of 65537 in PEM format
|
|
param: bits The key length in bits
|
|
Return private key and public key
|
|
'''
|
|
new_key = RSA.generate(bits, e=65537)
|
|
public_key = new_key.publickey().exportKey("PEM")
|
|
private_key = new_key.exportKey("PEM")
|
|
return {'private':private_key, 'public':public_key}
|
|
|
|
def encrypt_RSA(public_key, message):
|
|
'''
|
|
param: public_key_loc Path to public key
|
|
param: message String to be encrypted
|
|
return base64 encoded encrypted string
|
|
'''
|
|
#public_key = open(public_key_loc, "r").read()
|
|
rsakey = RSA.importKey(public_key)
|
|
rsakey = PKCS1_OAEP.new(rsakey)
|
|
encrypted = rsakey.encrypt(message)
|
|
return encrypted.encode('base64')
|
|
|
|
def decrypt_RSA(private_key, package):
|
|
'''
|
|
param: public_key_loc Path to your private key
|
|
param: package String to be decrypted
|
|
return decrypted string
|
|
'''
|
|
#private_key = open(private_key_loc, "r").read()
|
|
rsakey = RSA.importKey(private_key)
|
|
rsakey = PKCS1_OAEP.new(rsakey)
|
|
decrypted = rsakey.decrypt(base64.b64decode(package))
|
|
return decrypted
|
|
|
|
pk = generate_RSA();
|
|
enc = encrypt_RSA (pk['public'], "Howdy there fella");
|
|
print decrypt_RSA (pk['private'], enc);
|
|
|
|
madman12 = base64.b64decode ("6EVdXfSkSX+I15ZXGCRRH4TnpBnt17ivih5Nd7DxkPQ=");
|
|
tar = tarfile.open("json.ionu", "r")
|
|
for tarinfo in tar:
|
|
print tarinfo.name, " ", tarinfo.size, "bytes";
|
|
|
|
ionufek = tar.extractfile(".ionufek");
|
|
buffer = bytes(ionufek.read());
|
|
iv = buffer[1:17];
|
|
fek = buffer[17:65]
|
|
cipher = AES.new (madman12, AES.MODE_CBC, iv);
|
|
fek = cipher.decrypt (fek);
|
|
fek = fek[0:32];
|
|
|
|
meta = tar.extractfile(".ionumeta");
|
|
buffer = meta.read();
|
|
metajson = json.loads (buffer);
|
|
iv = base64.b64decode (metajson["iv"]);
|
|
print metajson["tgisig"];
|
|
for entry in metajson["contents"]:
|
|
print entry["entry"], " ", entry["size"];
|
|
f = tar.extractfile(entry["entry"]);
|
|
buffer = f.read();
|
|
cipher = AES.new (fek, AES.MODE_CBC, iv);
|
|
print cipher.decrypt (buffer);
|
|
|
|
tar.close()
|
|
|