import gradio as gr from cryptography.fernet import Fernet from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from Crypto.PublicKey import RSA from Crypto.Random import get_random_bytes from Crypto.Cipher import AES, PKCS1_OAEP import binascii from base64 import b64decode import base58 import os import base64 import hashlib from pathlib import Path import qrcode as qrx import qr import stegan import stegan2 import json import re import os from Crypto.Hash import RIPEMD160, SHA256 #import pickle 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() eth_wal = gr.Interface.load("spaces/Omnibus/decentral-test-mod") def rsa_encrypt_eth(): priv = secrets.token_hex(32) #print (secrets) private_key = "0x" + priv print ("SAVE BUT DO NOT SHARE THIS:", private_key) acct = Account.from_key(private_key) print("Address:", acct.address) def generate_keys1(): key = RSA.generate(2048) private_key = key.export_key('PEM') file_out = open("private.pem", "wb") file_out.write(private_key) file_out.close() public_key = key.publickey().export_key('PEM') file_out = open("receiver.pem", "wb") file_out.write(public_key) file_out.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) return public_key,private_key,address_im,address def encrypt_text1(data,in2,address): data = data.encode("utf-8") #data = "I met aliens in UFO. Here is the map.".encode("utf-8") file_out = open("encrypted_data.bin", "wb") recipient_key = RSA.import_key(open("receiver.pem").read()) 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.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ] file_out.close() ################################ #doc_name = doc.name #doc_name = doc_name.split("/",4)[4] doc_name = "encrypted_data.bin" with open(doc_name, "rb") as file: # read all file data file_data =(file.read()) print (f'file_data::{file_data}') qr_link="test" enc_qr = stegan2.conv_im(qr_link=qr_link,data=file_data) file.close() #enc_doc = fernet.encrypt(file_data) #og_name = doc.name #og_end = og_name.split(".",1)[1] #og_front=og_name.split(".",1)[0] #enc_file=f'{doc.name}.ocrpt' #with open(enc_file, "wb") as file: # file.write(enc_doc) ######################################## #output1 = pickle.dumps(output) #enc_qr = stegan2.conv_im(qr_link=address,data=out_file) #out = output #output.close() return str(file_data),enc_qr def decrypt_text1(im,in2): mes1="" mes2="" mes3="" mes4="" mes_bytes1 =b'' mes_bytes2 =b'' mes_bytes3 =b'' mes_bytes4 =b'' enc_in = stegan2.decode(im) ''' #enc_in=enc_in.replace('"',"'") print (f'enc_in::{enc_in}') if "+aaa+" in enc_in: mes1=enc_in.split("+aaa+",1)[0] mes_bytes1=mes1 print (f'mes_bytes1::{mes_bytes1}') #mes_bytes1 = bytes(mes1) #mes_dec = fernet.decrypt(mes_bytes).decode() if "+bbb+" in enc_in: mes2=enc_in.split("+aaa+",1)[1] mes2=mes2.split("+bbb+",1)[0] #mes2=mes2.strip('b"').strip('"') mes_bytes2=mes2 # mes_bytes2 = bytes(mes2) if "+ccc+" in enc_in: mes3=enc_in.split("+bbb+",1)[1] mes3=mes3.split("+ccc+",1)[0] #mes3=mes3.strip('b"').strip('"') mes_bytes3=mes3 #mes_bytes3 = bytes(mes3) if "+ddd+" in enc_in: mes4=enc_in.split("+ccc+",1)[1] mes4=mes4.split("+ddd+",1)[0] #mes4=mes4.strip('b"').strip('"') mes_bytes4=mes4 #mes_bytes4 = bytes(mes4) print(f'mes_bytes1::{mes_bytes1}') print(f'mes_bytes2::{mes_bytes2}') print(f'mes_bytes3::{mes_bytes3}') print(f'mes_bytes4::{mes_bytes4}') #file_out = open("encrypted_data.bin", "w") #[ file_out.write(x) for x in (mes_bytes1, mes_bytes2, mes_bytes3, mes_bytes4) ] #file_out.close() #enc_in = stegan2.decode(im) #enc_in.replace('"','') #enc_in = base64.b64decode(enc_in) #with open("encrypted_data1.bin", "wb") as file: # file.write(enc_in) #file.close() enc_in=enc_in.strip('"') #enc_in=enc_in.strip("b").strip("'") #enc_in = enc_in() print (f'enc_in 1:: {enc_in}') #enc_in = bytes(enc_in,"utf-8") #enc_in = f"b'{enc_in}'" #enc_in = base64.b64encode(enc_in) print (f'enc_in 2:: {enc_in}') ''' private_key = RSA.import_key(open("private.pem").read()) 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:] #enc_session_key, nonce, tag, cipher_text =[ x for x in (enc_in[:private_key.size_in_bytes()],enc_in[private_key.size_in_bytes():private_key.size_in_bytes()+16],enc_in[private_key.size_in_bytes+16():private_key.size_in_bytes()+32],enc_in[-1]) ] print (f'enc_session_key::{enc_session_key}') print (f'nonce::{nonce}') print (f'tag::{tag}') print (f'ciphertext::{ciphertext}') file_in = open("encrypted_data1.bin", "rb") #file_in=file_in.strip('"') #enc_session_key, nonce, tag, ciphertext = \ # [ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ] file_in.close() 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) return(data.decode("utf-8")) def eth_create(seed = None): priv,pub,addr,seed = eth_wal(seed) qr_wal=qrx.make(addr) qr_wal.save("qr_wal_tmp.png") return priv,pub,addr,"qr_wal_tmp.png",seed def trans_block(sender,recipient,amount): transaction = [{ 'sender': sender, 'recipient': recipient, 'amount': amount }] json_object = json.dumps(transaction, indent=4) print (json_object) return json_object def create_key(passw): key = passw.encode() h = hashlib.new('sha256') h.update(key) key = h.hexdigest() key = key.encode() #salt = os.urandom(16) salt = key kdf = PBKDF2HMAC( algorithm=hashes.SHA256(), length=32, salt=salt, iterations=480000, ) key = base64.urlsafe_b64encode(kdf.derive(key)) return key def encrypt(passw,trans_s=None,trans_r=None,trans_a=None): #key = Fernet.generate_key() key = create_key(passw) fernet = Fernet(key) enc_mes="" enc_file=None if trans_s != None and trans_r != None and trans_a != None and trans_s != "" and trans_r != "" and trans_a != "": trans = trans_block(trans_s,trans_r,trans_a) bytes_j = trans.encode() recipient_key = RSA.import_key(open("receiver.pem").read()) 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.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ] #file_out.close() out_list=[enc_session_key,cipher_aes.nonce,tag,ciphertext] json_form = { 'enc_session_key':f"{enc_session_key}", 'cipher_aes':f"{cipher_aes.nonce}", 'tag':f"{tag}", 'ciphertext':f"{ciphertext}" } #out_json.append(json_form) recipient_key = RSA.import_key(open("receiver.pem").read()) #cipher_rsa = PKCS1_OAEP.new(recipient_key) #json_bytes = cipher_rsa.encrypt(bytes_j) #json_key = cipher_rsa.encrypt(key) #json_bytes = fernet.encrypt(bytes_j) enc_mes = f'{json_bytes}+ccc+{out_list}+ddd+' print(f'enc_mes out:: {enc_mes}') #enc_qr = qr.make_qr(txt=(enc_mes.strip('"'))) m = hashlib.sha256() bytes_enc_mes = enc_mes.encode() m.update(bytes_enc_mes) out = m.hexdigest() out = out.encode() print(out) qr_link = out enc_qr = stegan2.conv_im(qr_link=qr_link,data=enc_mes) return enc_mes,enc_qr,enc_qr def decrypt(passw,enc_in=None): key = create_key(passw) fernet = Fernet(key) dec_im = None mes_dec= None dec_json=None enc_in=enc_in.strip('"') print (f'enc_in :::: {enc_in}') if "+aaa+" in enc_in: mes1=enc_in.split("+aaa+",1)[0] mes1=mes1.strip("b'").strip("'") mes_bytes = bytes(mes1,'utf-8') mes_dec = fernet.decrypt(mes_bytes).decode() if "+bbb+" in enc_in: mes12=enc_in.split("+aaa+",1)[1] mes2=mes12.split("+bbb+",1)[0] mes2=mes2.strip("b'").strip("'") im_bytes = bytes(mes2,'utf-8') print(f'im_bytes::{im_bytes}') mes2 = fernet.decrypt(mes2).decode() #base = bytes(decMessage, 'utf-8') with open(f"finished_im.png", "wb") as fh: #fh.write(base64.decodebytes(im_bytes)) fh.write(base64.decodebytes(bytes(mes2, 'utf-8'))) fh.close dec_im = "finished_im.png" if "+ccc+" in enc_in: mes13=enc_in.split("+bbb+",1)[1] mes3=mes13.split("+ccc+",1)[0] mes3=mes3.strip("b'").strip("'") json_bytes = bytes(mes3,'utf-8') print(f'im_bytes::{json_bytes}') dec_json = fernet.decrypt(json_bytes).decode() if not "+bbb+" in enc_in: if "+ccc+" in enc_in: mes14=enc_in.split("+aaa+",1)[1] mes4=mes14.split("+ccc+",1)[0] mes4=mes4.strip("b'").strip("'") json_bytes = bytes(mes4,'utf-8') print(f'im_bytes::{json_bytes}') dec_json = fernet.decrypt(json_bytes).decode() if not "+aaa+" in enc_in: if "+bbb+" in enc_in: mes2 = enc_in.split("+bbb+",1)[0] mes2=mes2.strip("b'").strip("'") im_bytes = bytes(mes2,'utf-8') print(f'im_bytes2::{im_bytes}') mes2 = fernet.decrypt(mes2).decode() #base = bytes(decMessage, 'utf-8') with open(f"finished_im.png", "wb") as fh: #fh.write(base64.decodebytes(im_bytes)) fh.write(base64.decodebytes(bytes(mes2, 'utf-8'))) fh.close dec_im = "finished_im.png" if "+ccc+" in enc_in: mes13=enc_in.split("+bbb+",1)[1] mes3=mes13.split("+ccc+",1)[0] mes3=mes3.strip("b'").strip("'") json_bytes = bytes(mes3,'utf-8') print(f'im_bytes::{json_bytes}') dec_json = fernet.decrypt(json_bytes).decode() if not "+bbb+" in enc_in: if "+ccc+" in enc_in: mes4=enc_in.split("+ccc+",1)[0] mes14=mes4.strip("b'").strip("'") json_bytes = bytes(mes14,'utf-8') print(f'im_bytes::{json_bytes}') #private_key = RSA.import_key(open("private.pem").read()) #cipher_rsa = PKCS1_OAEP.new(private_key) #dec_json = cipher_rsa.decrypt(json_bytes).decode() #print("done") dec_json = fernet.decrypt(json_bytes).decode() return(dec_im, mes_dec, dec_json) def decrypt_json(im,enc_in=None): enc_in = stegan2.decode(im) #key = create_key(passw) #fernet = Fernet(key) dec_im = None mes_dec= None dec_json=None enc_in=enc_in.strip('"') print (f'enc_in :::: {enc_in}') if "+ccc+" in enc_in: if "+ddd+" in enc_in: mes15=enc_in.split("+ccc+",1)[1] mes5=mes15.split("+ddd+",1)[0] mes5=mes5.strip('"') mes5=mes5.strip("b'").strip("'") mes5=mes5.strip("[").strip("]") #session_key = json_bytes = bytes(mes5,'utf-8') json_dump = JSON.dumps(json_bytes) file_out = open("encrypted_data.bin", "wb") [ file_out.write(json_bytes) ] file_out.close() file_in = open("encrypted_data.bin", "rb") json_bytes = \ [ file_in.read() ] file_in.close() private_key = RSA.import_key(open("private.pem").read()) #cipher_rsa = PKCS1_OAEP.new(private_key) #key = cipher_rsa.decrypt(json_bytes).decode() #print (f'New Key:: {key}') #fernet = Fernet(key) mes4=enc_in.split("+ccc+",1)[0] mes14=mes4.strip("b'").strip("'") json_bytes = bytes(mes14,'utf-8') print(f'json_bytes22::{json_bytes}') # 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) return(data.decode("utf-8")) #print("done") #dec_json = fernet.decrypt(json_bytes).decode() return(dec_json) def decode_doc(passw,doc=None): key = create_key(passw) fernet = Fernet(key) doc_name = doc.name doc_name = doc_name.split("/",4)[4] #bytes_d = doc.encode() #doc = Path(doc) with open(doc.name, "rb") as file: # read all file data file_data = file.read() dec_doc = fernet.decrypt(file_data) og_name = doc.name og_end = og_name.split(".",1)[1] og_front=og_name.split(".",1)[0] dec_file = doc.name.strip(".ocrpt") #enc_file=f'{doc.name}.ocrpt' with open(dec_file, "wb") as file: file.write(dec_doc) return dec_file def decode_qr(im,passw): with open(f'{im}', "rb") as image_file: bytes_i = base64.b64encode(image_file.read()) decode_qr = stegan.decode(im) print(f'decode_qr:: {decode_qr}') dec_im,mes_dec,dec_json = decrypt(passw, enc_in=decode_qr) return(dec_im,mes_dec,dec_json) def predict(text, url_params): print(url_params) return ["Hello " + text + "!!", url_params] get_window_url_params = """ function(text_input, url_params) { console.log(text_input, url_params); const params = new URLSearchParams(window.location.search); url_params = Object.fromEntries(params); return [text_input, url_params]; } """ with gr.Blocks() as app: with gr.Row(): gr.Column() with gr.Column(): with gr.Tab("Encrypt"): pass_in=gr.Textbox(label="Set Password") with gr.Row(): with gr.Column(): send=gr.Textbox(label="Sender") rec=gr.Textbox(label="Recipient") am=gr.Textbox(label="Amount") en_btn = gr.Button("Encrypt") enc_out = gr.Textbox(label="Encrypted Bytes") enc_qr_out = gr.Image(label = "Encrypted QR") with gr.Tab("Decrypt"): pass_out = gr.Textbox(label="Enter Password") dec_qr_im = gr.Image(type="filepath") d_json = gr.JSON(label="Decrypted JSON") dec_qr_txt = gr.Textbox(label="Decrypted Message") dec_qr_im_out =gr.Image(label="Decrytped Image") dec_qr_btn = gr.Button("Decrypt") with gr.Tab("URL"): url_params = gr.JSON({}, visible=True, label="URL Params") url_btn = gr.Button("Get URL") text_input = gr.Text(label="Input",visible=False) text_output = gr.Text(label="Output",visible=False) with gr.Tab("Gen Wal"): gen_wal_btn=gr.Button() seed = gr.Textbox(label='Seed Phrase') with gr.Box(): with gr.Row(): with gr.Column(): out1 = gr.Textbox(label='Private Key') out2 = gr.Textbox(label='Public Key') img3=gr.Pil() out3 = gr.Textbox(label='Address') rsa_to_enc = gr.Textbox(label="txt to encrypt") rsa_enc_btn = gr.Button("RSA Encrypt") rsa_enc_mes = gr.Textbox(label="encoded") qr_enc_mes = gr.Image(type="filepath") rsa_dec_btn = gr.Button("RSA Decrypt") rsa_dec_mes = gr.Textbox(label="decoded") gr.Column() rsa_enc_btn.click(encrypt_text1,[rsa_to_enc,out2,out3],[rsa_enc_mes,qr_enc_mes]) rsa_dec_btn.click(decrypt_text1,[qr_enc_mes,out1],rsa_dec_mes) gen_wal_btn.click(generate_keys1,None,[out2,out1, img3,out3]) dec_qr_btn.click(decrypt_json,[dec_qr_im,pass_out],[dec_qr_im_out,dec_qr_txt,d_json]) en_btn.click(encrypt,[pass_in,send,rec,am],[enc_out,enc_qr_out,dec_qr_im]) url_btn.click(fn=predict, inputs=[text_input,url_params],outputs=[text_output,url_params], _js=get_window_url_params) app.launch()