File size: 5,189 Bytes
9f8abad
 
 
 
 
 
8c40af7
9f8abad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673af6a
9f8abad
 
2ce1d82
 
8c40af7
 
 
9f8abad
 
8c40af7
 
 
9f8abad
8c40af7
64fa4cd
 
9f8abad
 
 
 
 
8c40af7
9f8abad
673af6a
35ff3aa
 
9f8abad
 
 
35ff3aa
 
 
9f8abad
 
 
 
 
 
 
 
 
35ff3aa
 
 
9f8abad
 
 
 
 
 
 
 
 
 
 
 
673af6a
9f8abad
8705cba
44845ee
8705cba
 
 
2ce1d82
9f8abad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673af6a
9f8abad
 
 
f16b61d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f8abad
673af6a
e6357b8
35ff3aa
44845ee
8be4bf9
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
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
import base58
import stegan
import stegan2
import qr


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():
    key = RSA.generate(2048)
    private_key = key.export_key('PEM')
    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()

    qr_link="test"
    priv_key = stegan.conv_im("private_key.png",data=private_key)
    pub_key = stegan.conv_im("public_key.png",data=public_key)
    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,priv_key,pub_key

def encrypt_text(data,in2,address):
    pub_key = stegan2.decode(in2)
    
    data = data.encode("utf-8")
    #data = "I met aliens in UFO. Here is the map.".encode("utf-8")
    
    #recipient_key = RSA.import_key(open("receiver.pem").read())
    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"
    enc_qr = stegan2.conv_im(qr_link=qr_link,data=file_data)
    
    file.close()
    return str(file_data),enc_qr

def decrypt_text(im,in2):
    enc_in = stegan2.decode(im)
    
    #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)
    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)
    return(data.decode("utf-8"))



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])
    rsa_enc_btn.click(encrypt_text,[rsa_to_enc,pub_key_in,out3],[rsa_enc_mes,qr_enc_mes])
    rsa_dec_btn.click(decrypt_text,[mes_in,priv_key_in],rsa_dec_mes)
app.launch()