Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,40 +35,7 @@ def rsa_encrypt_eth():
|
|
| 35 |
print("Address:", acct.address)
|
| 36 |
|
| 37 |
|
| 38 |
-
'''
|
| 39 |
-
from Crypto.PublicKey import RSA
|
| 40 |
-
from Crypto.Cipher import PKCS1_OAEP
|
| 41 |
-
from Crypto.Random import get_random_bytes
|
| 42 |
-
|
| 43 |
-
def generate_keys():
|
| 44 |
-
key = RSA.generate(2048)
|
| 45 |
-
private_key = key.export_key()
|
| 46 |
-
public_key = key.publickey().export_key()
|
| 47 |
-
return public_key, private_key
|
| 48 |
-
|
| 49 |
-
def encrypt_text(text, public_key):
|
| 50 |
-
rsa_public_key = RSA.import_key(public_key)
|
| 51 |
-
rsa_public_key = PKCS1_OAEP.new(rsa_public_key)
|
| 52 |
-
encrypted_text = rsa_public_key.encrypt(text)
|
| 53 |
-
return encrypted_text
|
| 54 |
|
| 55 |
-
def decrypt_text(encrypted_text, private_key):
|
| 56 |
-
rsa_private_key = RSA.import_key(private_key)
|
| 57 |
-
rsa_private_key = PKCS1_OAEP.new(rsa_private_key)
|
| 58 |
-
decrypted_text = rsa_private_key.decrypt(encrypted_text)
|
| 59 |
-
return decrypted_text
|
| 60 |
-
|
| 61 |
-
# Generate RSA keys
|
| 62 |
-
public_key, private_key = generate_keys()
|
| 63 |
-
|
| 64 |
-
# Encrypt text
|
| 65 |
-
text = "Hello, World!".encode()
|
| 66 |
-
encrypted_text = encrypt_text(text, public_key)
|
| 67 |
-
print(f"Encrypted text: {encrypted_text}")
|
| 68 |
-
|
| 69 |
-
# Decrypt text
|
| 70 |
-
decrypted_text = decrypt_text(encrypted_text, private_key)
|
| 71 |
-
print(f"Decrypted text: {decrypted_text.decode()}")'''
|
| 72 |
|
| 73 |
def generate_keys1():
|
| 74 |
key = RSA.generate(2048)
|
|
@@ -106,8 +73,13 @@ def encrypt_text1(data,in2):
|
|
| 106 |
return str(out)
|
| 107 |
|
| 108 |
def decrypt_text1(in1,in2):
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
private_key = RSA.import_key(open("private.pem").read())
|
| 112 |
|
| 113 |
enc_session_key, nonce, tag, ciphertext = \
|
|
@@ -124,94 +96,7 @@ def decrypt_text1(in1,in2):
|
|
| 124 |
return(data.decode("utf-8"))
|
| 125 |
|
| 126 |
|
| 127 |
-
def generate_keys():
|
| 128 |
-
key = RSA.generate(2048)
|
| 129 |
-
private_key = key.export_key('PEM')
|
| 130 |
-
public_key = key.publickey().export_key('PEM')
|
| 131 |
-
f = open('keyfile_private.pem', 'wb')
|
| 132 |
-
f.write(private_key)
|
| 133 |
-
f.close()
|
| 134 |
-
#public_key = key.publickey()
|
| 135 |
-
f = open('keyfile_public.pem', 'wb')
|
| 136 |
-
f.write(public_key)
|
| 137 |
-
f.close()
|
| 138 |
-
return public_key, private_key
|
| 139 |
-
|
| 140 |
-
def save_keys(public_key, private_key, filename):
|
| 141 |
-
with open(filename, 'w') as f:
|
| 142 |
-
f.write(public_key)
|
| 143 |
-
f.write('\n')
|
| 144 |
-
f.write(private_key)
|
| 145 |
-
|
| 146 |
-
def read_keys(filename):
|
| 147 |
-
with open(filename, 'r') as f:
|
| 148 |
-
return f.read()
|
| 149 |
-
|
| 150 |
-
def encrypt_text(text, public_key):
|
| 151 |
-
f = open('keyfile_public.pem', 'rb')
|
| 152 |
-
public_key = RSA.importKey(f.read())
|
| 153 |
-
#rsa_public_key = RSA.import_key(public_key)
|
| 154 |
-
rsa_cipher = PKCS1_OAEP.new(public_key)
|
| 155 |
-
encrypted_text = rsa_cipher.encrypt(text.encode())
|
| 156 |
-
return base64.b64encode(encrypted_text)
|
| 157 |
-
|
| 158 |
-
def decrypt_text(encrypted_text, private_key):
|
| 159 |
-
|
| 160 |
-
fa = open('keyfile_private.pem', 'rb')
|
| 161 |
-
private_key = RSA.importKey(fa.read())
|
| 162 |
-
encrypted_text = base64.b64decode(encrypted_text)
|
| 163 |
-
#rsa_private_key = RSA.import_key(private_key.decode())
|
| 164 |
-
|
| 165 |
-
rsa_cipher = PKCS1_OAEP.new(private_key)
|
| 166 |
-
decrypted_text = rsa_cipher.decrypt(encrypted_text)
|
| 167 |
-
return decrypted_text
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
def rsa_generate():
|
| 171 |
-
key = RSA.generate(1024)
|
| 172 |
-
f = open('keyfile_private.pem', 'wb')
|
| 173 |
-
f.write(key.exportKey('PEM'))
|
| 174 |
-
f.close()
|
| 175 |
-
public_key = key.publickey()
|
| 176 |
-
f = open('keyfile_public.pem', 'wb')
|
| 177 |
-
f.write(public_key.exportKey('PEM'))
|
| 178 |
-
f.close()
|
| 179 |
-
|
| 180 |
-
private_key = key.exportKey()
|
| 181 |
-
output = re.search('[0-9a-fA-F]{32}', str(public_key))
|
| 182 |
-
qr_wal=qrx.make(output)
|
| 183 |
-
qr_wal.save("qr_wal_tmp.png")
|
| 184 |
-
|
| 185 |
-
return private_key,public_key,output,"qr_wal_tmp.png",output
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
def rsa_encrypt(msg,public_key):
|
| 192 |
-
f = open('keyfile_public.pem', 'rb')
|
| 193 |
-
#key = RSA.importKey(f.read())
|
| 194 |
-
key = RSA.importKey(public_key)
|
| 195 |
-
print (public_key)
|
| 196 |
-
#key = RSA.importKey(key)
|
| 197 |
-
|
| 198 |
-
message = msg.encode()
|
| 199 |
-
print(message)
|
| 200 |
-
|
| 201 |
-
cipher = PKCS1_OAEP.new(key)
|
| 202 |
-
ciphertext = cipher.encrypt(message)
|
| 203 |
-
return (ciphertext)
|
| 204 |
-
|
| 205 |
-
def rsa_decrypt(encrypted,keyPair):
|
| 206 |
-
f = open('keyfile_private.pem', 'rb')
|
| 207 |
-
#key = RSA.importKey(f.read())
|
| 208 |
-
key = RSA.importKey(private_key)
|
| 209 |
-
|
| 210 |
-
print (key)
|
| 211 |
-
decryptor = PKCS1_OAEP.new(key)
|
| 212 |
-
decrypted = decryptor.decrypt(encrypted)
|
| 213 |
-
print('Decrypted:', decrypted)
|
| 214 |
-
return decrypted
|
| 215 |
|
| 216 |
def eth_create(seed = None):
|
| 217 |
priv,pub,addr,seed = eth_wal(seed)
|
|
|
|
| 35 |
print("Address:", acct.address)
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def generate_keys1():
|
| 41 |
key = RSA.generate(2048)
|
|
|
|
| 73 |
return str(out)
|
| 74 |
|
| 75 |
def decrypt_text1(in1,in2):
|
| 76 |
+
in1= in1.encode("utf-8")
|
| 77 |
+
file_out = open("encrypted_data2.bin","wb")
|
| 78 |
+
file_out.write(in1)
|
| 79 |
+
#[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
|
| 80 |
+
file_out.close()
|
| 81 |
+
file_in = open("encrypted_data1.bin", "rb")
|
| 82 |
+
|
| 83 |
private_key = RSA.import_key(open("private.pem").read())
|
| 84 |
|
| 85 |
enc_session_key, nonce, tag, ciphertext = \
|
|
|
|
| 96 |
return(data.decode("utf-8"))
|
| 97 |
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
def eth_create(seed = None):
|
| 102 |
priv,pub,addr,seed = eth_wal(seed)
|