Spaces:
Runtime error
Runtime error
import gradio as gr | |
from Crypto.PublicKey import RSA | |
from Crypto.Random import get_random_bytes | |
from Crypto.Cipher import AES, PKCS1_OAEP | |
from Crypto.Hash import RIPEMD160, SHA256 | |
from Crypto.Signature import pkcs1_15 | |
import binascii | |
import base58 | |
import stegan | |
import stegan2 | |
import qr | |
import overlay | |
def calculate_hash(data, hash_function: str = "sha256") -> str: | |
if type(data) == str: | |
data = bytearray(data, "utf-8") | |
if hash_function == "sha256": | |
h = SHA256.new() | |
h.update(data) | |
return h.hexdigest() | |
if hash_function == "ripemd160": | |
h = RIPEMD160.new() | |
h.update(data) | |
return h.hexdigest() | |
def generate_keys(): | |
secret_code="SECRET PASSWORD" | |
key = RSA.generate(2048) | |
#private_key = key.export_key('PEM') | |
private_key = key.export_key(passphrase=secret_code, pkcs=8, | |
protection="scryptAndAES128-CBC") | |
print(f'private_key:: {private_key}') | |
file_out_priv = open("private.pem", "wb") | |
file_out_priv.write(private_key) | |
file_out_priv.close() | |
public_key = key.publickey().export_key('PEM') | |
file_out_pub = open("receiver.pem", "wb") | |
file_out_pub.write(public_key) | |
file_out_pub.close() | |
hash_1 = calculate_hash(public_key, hash_function="sha256") | |
hash_2 = calculate_hash(hash_1, hash_function="ripemd160") | |
address = base58.b58encode(hash_2) | |
address_im=qr.make_qr(txt=address) | |
add_label = str(address) | |
add_label = add_label.strip("b").strip("'") | |
address_im = overlay.textover(address_im, "Wallet",add_label) | |
address_im.save("address_im.png") | |
#qr_link="test" | |
address_im = "address_im.png" | |
private_key_im = overlay.textover("private_key.png", "Key",add_label) | |
private_key_im.save("private_key_im.png") | |
priv_key = stegan.conv_im("private_key_im.png",data=private_key) | |
pub_key = stegan.conv_im("address_im.png",data=public_key) | |
return public_key,private_key,address_im,address,priv_key,pub_key | |
def sign(data,in2): | |
priv_key = stegan2.decode(in2) | |
print(f'priv_key:: {priv_key}') | |
private_key = RSA.import_key(priv_key) | |
hash_object = SHA256.new(data) | |
signature = pkcs1_15.new(private_key).sign(hash_object) | |
signature = binascii.hexlify(signature).decode("utf-8") | |
data_json = { | |
"data": data, | |
"signature": signature | |
} | |
return data_json | |
def validate_signature(public_key: bytes, signature: bytes, transaction_data: bytes): | |
public_key_object = RSA.import_key(public_key) | |
transaction_hash = SHA256.new(transaction_data) | |
pkcs1_15.new(public_key_object).verify(transaction_hash, signature) | |
def encrypt_text(data,pub_im,priv_im,address): | |
pub_key = stegan2.decode(pub_im) | |
#priv_key = stegan2.decode(in2) | |
#print(f'priv_key:: {priv_key}') | |
#private_key = RSA.import_key(priv_key) | |
data = data.encode("utf-8") | |
#data = sign(data,priv_im) | |
recipient_key = RSA.import_key(pub_key) | |
session_key = get_random_bytes(16) | |
# Encrypt the session key with the public RSA key | |
cipher_rsa = PKCS1_OAEP.new(recipient_key) | |
enc_session_key = cipher_rsa.encrypt(session_key) | |
# Encrypt the data with the AES session key | |
cipher_aes = AES.new(session_key, AES.MODE_EAX) | |
ciphertext, tag = cipher_aes.encrypt_and_digest(data) | |
file_out = open("encrypted_data.bin", "wb") | |
[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ] | |
file_out.close() | |
doc_name = "encrypted_data.bin" | |
with open(doc_name, "rb") as file: | |
file_data =(file.read()) | |
print (f'file_data::{file_data}') | |
qr_link="test" | |
#trans_im1.save("trans_im.png") | |
hash_1 = calculate_hash(pub_key, hash_function="sha256") | |
hash_2 = calculate_hash(hash_1, hash_function="ripemd160") | |
address = base58.b58encode(hash_2) | |
add_label = str(address) | |
add_label = add_label.strip("b").strip("'") | |
trans_im1=qr.make_qr(txt=address, color_b="#ECFD08") | |
#address_im = "address_im.png" | |
private_key_im = overlay.textover(trans_im1, "Transaction",add_label) | |
private_key_im.save("private_key_im.png") | |
#trans_im1.save("trans_im.png") | |
enc_qr = stegan.conv_im("private_key_im.png",data=file_data) | |
file.close() | |
return str(file_data),enc_qr | |
def decrypt_text(im,in2): | |
enc_in = stegan2.decode(im) | |
secret_code="SECRET PASSWORD" | |
#private_key = RSA.import_key(open("private.pem").read()) | |
priv_key = stegan2.decode(in2) | |
print(f'priv_key:: {priv_key}') | |
private_key = RSA.import_key(priv_key,passphrase=secret_code) | |
print(f'private_key:: {private_key}') | |
enc_session_key = enc_in[:private_key.size_in_bytes()] | |
end1 = private_key.size_in_bytes()+16 | |
nonce = enc_in[private_key.size_in_bytes():end1] | |
start1=end1+1 | |
end2 = private_key.size_in_bytes()+32 | |
start2=end2+1 | |
tag = enc_in[end1:end2] | |
ciphertext = enc_in[end2:] | |
print (f'enc_session_key::{enc_session_key}') | |
print (f'nonce::{nonce}') | |
print (f'tag::{tag}') | |
print (f'ciphertext::{ciphertext}') | |
# Decrypt the session key with the private RSA key | |
cipher_rsa = PKCS1_OAEP.new(private_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, nonce) | |
data = cipher_aes.decrypt_and_verify(ciphertext, tag) | |
#public_key = private_key.publickey().export_key('PEM') | |
#data_val = data[0] | |
#print (data_val) | |
#signature = data[1] | |
#print (signature) | |
#transaction_hash = SHA256.new(data) | |
#transaction | |
#print (pkcs1_15.new(public_key).verify(transaction_hash, signature)) | |
return(data.decode("utf-8")) | |
def test_fn(im1,im2): | |
return im1,im2,im1 | |
def test_fn2(im1): | |
return im1 |