Spaces:
Runtime error
Runtime error
File size: 7,536 Bytes
9f8abad 1708c7f 9f8abad 8c40af7 9f8abad 0916c74 9f8abad 673af6a 4bd08b6 9f8abad e741a24 2ce1d82 8c40af7 9f8abad 8c40af7 9f8abad e4a6d59 9f8abad dd65174 6ff7618 d38d0b4 cf04d53 e4a6d59 cf04d53 ca1d20c cf04d53 d94c1fc 8c40af7 9f8abad df77034 f7093ab df77034 9bd48e4 ded7877 9bd48e4 9f8abad e741a24 df77034 35ff3aa 9f8abad df2fb91 35ff3aa 9f8abad d7c61ef bfad97f b3d9108 82cde8d b3d9108 d7c61ef 595f033 b3d9108 093844a 595f033 9f8abad 673af6a 9f8abad 4bd08b6 44845ee 8705cba e741a24 2ce1d82 9f8abad f7093ab e741a24 f7093ab e741a24 9f8abad 9f61503 7ebfffa 9f61503 db9bf66 9f8abad 673af6a 9f8abad f16b61d 9f8abad 673af6a 9f61503 44845ee db9bf66 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
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
'''
with gr.Blocks() as app:
with gr.Row():
gr.Column()
with gr.Column():
with gr.Row():
with gr.Tab("Gen Wal"):
gen_wal_btn=gr.Button()
seed = gr.Textbox(label='Seed Phrase')
img1=gr.Pil(label='Private Key')
out1 = gr.Textbox(label='Private Key',max_lines=4)
img2=gr.Pil(label='Public Key')
out2 = gr.Textbox(label='Public Key',max_lines=4)
img3=gr.Pil(label='Address')
out3 = gr.Textbox(label='Address')
with gr.Tab("Encrypt"):
rsa_to_enc = gr.Textbox(label="txt to encrypt")
pub_key_in = gr.Image(label="Public Key", type="filepath")
rsa_enc_btn = gr.Button("RSA Encrypt")
rsa_enc_mes = gr.Textbox(label="encoded", max_lines=4)
qr_enc_mes = gr.Image(type="filepath")
with gr.Tab("Decrypt"):
mes_in = gr.Image(label="Message", type="filepath")
priv_key_in = gr.Image(label="Private Key", type="filepath")
rsa_dec_btn = gr.Button("RSA Decrypt")
rsa_dec_mes = gr.Textbox(label="decoded")
gr.Column()
gen_wal_btn.click(generate_keys,None,[out2,out1, img3,out3,img1,img2]).then(test_fn,[img1,img2],[priv_key_in,pub_key_in])
rsa_enc_btn.click(encrypt_text,[rsa_to_enc,pub_key_in,out3],[rsa_enc_mes,qr_enc_mes]).then(test_fn2,qr_enc_mes,mes_in)
rsa_dec_btn.click(decrypt_text,[mes_in,priv_key_in],rsa_dec_mes)
app.launch()
''' |