def main(target, prv_key, pass_phrase): aes_util = AESUtil() rsa_util = RSAUtil() with open(prv_key) as f: s = f.read() pkey = binascii.unhexlify(s) rsa_util.import_prv_key(pkey, pass_phrase) with open(target) as f2: s = f2.read() target_data = json.loads(s) p_version = target_data["version"] file_name = target_data["file_name"] c_key = target_data["content_key"] e_content = target_data["content"] sender_pub_key = target_data["sender"] encrypted_key = base64.b64decode(binascii.unhexlify(c_key)) decrypted_key = rsa_util.decrypt_with_private_key(encrypted_key) enc_data = base64.b64decode(binascii.unhexlify(e_content)) content = aes_util.decrypt_with_key(enc_data, decrypted_key) signature = target_data["signature"] target_strings = p_version + file_name + c_key + e_content + sender_pub_key sender_pubkey = RSA.importKey(binascii.unhexlify(sender_pub_key)) v_result = rsa_util.verify_signature(target_strings, signature, sender_pubkey) if(v_result): with open(file_name, mode='wb') as f3: f3.write(content)
def main(target_file_name, path_to_rsa_pub_key, result_file_name, prv_key, pass_phrase): p_version = "0.1.0" aes_util = AESUtil() with open(target_file_name, 'rb') as f: target = f.read() enc_content = aes_util.encrypt(target) content_key = aes_util.get_aes_key() with open(path_to_rsa_pub_key, 'r') as f2: pkey_data = f2.read() with open(prv_key, 'r') as f3: s = f3.read() my_prvkey = binascii.unhexlify(s) rsa_util = RSAUtil() rsa_util.import_prv_key(my_prvkey, pass_phrase) enc_key = rsa_util.encrypt_with_pubkey(content_key, pkey_data) result = {} result["version"] = p_version result["file_name"] = target_file_name content_key_txt = binascii.hexlify( base64.b64encode(enc_key)).decode('ascii') result["content_key"] = content_key_txt content_txt = binascii.hexlify( base64.b64encode(enc_content)).decode('ascii') result["content"] = content_txt sender_pub_key = binascii.hexlify(rsa_util.get_my_pubkey()).decode('ascii') result["sender"] = sender_pub_key signature = rsa_util.compute_digital_signature(p_version + target_file_name + content_key_txt + content_txt + sender_pub_key) result["signature"] = signature with open(result_file_name, 'w') as f4: json.dump(result, f4, indent=4)
def main(target, prv_key, pass_phrase): aes_util = AESUtil() rsa_util = RSAUtil() with open(prv_key) as f: s = f.read() pkey = binascii.unhexlify(s) rsa_util.import_prv_key(pkey, pass_phrase) with open(target) as f2: s = f2.read() target_data = json.loads(s) encrypted_key = base64.b64decode(binascii.unhexlify(target_data["content_key"])) decrypted_key = rsa_util.decrypt_with_private_key(encrypted_key) enc_data = base64.b64decode(binascii.unhexlify(target_data["content"])) content = aes_util.decrypt_with_key(enc_data, decrypted_key) result_file_name = target_data["file_name"] with open(result_file_name, mode='wb') as f3: f3.write(content)