Created
August 2, 2019 20:45
-
-
Save hclivess/85663ee9956cd6df8a9fb930d619c280 to your computer and use it in GitHub Desktop.
bismuth encryption interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Cryptodome.Cipher import AES, PKCS1_OAEP | |
from Cryptodome.PublicKey import RSA | |
from Cryptodome.Random import get_random_bytes | |
import base64 | |
import ast | |
from bisbasic import essentials | |
from bisbasic.simplecrypt import decrypt | |
import getpass | |
key, public_key_readable, private_key_readable, encrypted, unlocked, _, myaddress, keyfile = essentials.keys_load_new("wallet.der") | |
def sk_decrypt(data): | |
try: | |
# msg_received_digest = key.decrypt(ast.literal_eval(msg_received_digest)).decode("utf-8") | |
(cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(data) | |
# Decrypt the session key with the public RSA key | |
cipher_rsa = PKCS1_OAEP.new(key) | |
session_key = cipher_rsa.decrypt(enc_session_key) | |
# Decrypt the data with the AES session key | |
cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) | |
data = cipher_aes.decrypt_and_verify(ciphertext, tag).decode("utf-8") | |
except: | |
data = "Could not decrypt message" | |
return data | |
def pk_encrypt(data): | |
recipient_key = RSA.importKey(base64.b64decode(public_key_hashed).decode("utf-8")) | |
data = data.encode("utf-8") | |
# print (open("pubkey.der").read()) | |
session_key = get_random_bytes(16) | |
cipher_aes = AES.new(session_key, AES.MODE_EAX) | |
# Encrypt the session key with the public RSA key | |
cipher_rsa = PKCS1_OAEP.new(recipient_key) | |
# Encrypt the data with the AES session key | |
ciphertext, tag = cipher_aes.encrypt_and_digest(data) | |
enc_session_key = (cipher_rsa.encrypt(session_key)) | |
return str([x for x in (cipher_aes.nonce, tag, ciphertext, enc_session_key)]) | |
function = input("Do you want to encrypt or decrypt? (e/d) ") | |
if function == "e": | |
public_key_hashed = input("Enter hashed public key of recipient: ") | |
data = input("Enter data to encrypt: ") | |
encrypted_data = pk_encrypt(data) | |
with open("encrypted.txt","w+") as outfile: | |
outfile.write(encrypted_data) | |
print("data saved to encrypted.txt") | |
else: | |
if encrypted: | |
password = getpass.getpass(prompt='Password: ', stream=None) | |
decrypted_privkey = decrypt(password, base64.b64decode(private_key_readable)) # decrypt privkey | |
key = RSA.importKey(decrypted_privkey) # be able to sign | |
with open("encrypted.txt") as infile: | |
encrypted_data = infile.read() | |
print("data loaded from encrypted.txt") | |
decrypted_data = sk_decrypt(encrypted_data) | |
with open("decrypted.txt", "w+") as outfile: | |
outfile.write(decrypted_data) | |
print("data saved to decrypted.txt") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment