Spaces:
Sleeping
Sleeping
Next
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from cryptography.hazmat.primitives import hashes
|
| 3 |
+
from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
| 4 |
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
| 5 |
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
| 6 |
+
from cryptography.hazmat.primitives import serialization
|
| 7 |
+
import base64
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Caesar Cipher functions
|
| 11 |
+
def caesar_cipher_encrypt(text, shift):
|
| 12 |
+
result = ""
|
| 13 |
+
for i in range(len(text)):
|
| 14 |
+
char = text[i]
|
| 15 |
+
if char.isupper():
|
| 16 |
+
result += chr((ord(char) + shift - 65) % 26 + 65)
|
| 17 |
+
else:
|
| 18 |
+
result += chr((ord(char) + shift - 97) % 26 + 97)
|
| 19 |
+
return result
|
| 20 |
+
|
| 21 |
+
def caesar_cipher_decrypt(text, shift):
|
| 22 |
+
return caesar_cipher_encrypt(text, -shift)
|
| 23 |
+
|
| 24 |
+
# AES functions
|
| 25 |
+
def aes_encrypt(key, plaintext):
|
| 26 |
+
salt = os.urandom(16)
|
| 27 |
+
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000)
|
| 28 |
+
key = kdf.derive(key.encode())
|
| 29 |
+
iv = os.urandom(16)
|
| 30 |
+
cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
|
| 31 |
+
encryptor = cipher.encryptor()
|
| 32 |
+
ciphertext = encryptor.update(plaintext.encode()) + encryptor.finalize()
|
| 33 |
+
return base64.b64encode(salt + iv + ciphertext).decode('utf-8')
|
| 34 |
+
|
| 35 |
+
def aes_decrypt(key, ciphertext):
|
| 36 |
+
raw = base64.b64decode(ciphertext)
|
| 37 |
+
salt, iv, ciphertext = raw[:16], raw[16:32], raw[32:]
|
| 38 |
+
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000)
|
| 39 |
+
key = kdf.derive(key.encode())
|
| 40 |
+
cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
|
| 41 |
+
decryptor = cipher.decryptor()
|
| 42 |
+
return decryptor.update(ciphertext) + decryptor.finalize()
|
| 43 |
+
|
| 44 |
+
# RSA functions
|
| 45 |
+
def rsa_generate_keys():
|
| 46 |
+
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
| 47 |
+
public_key = private_key.public_key()
|
| 48 |
+
pem_private = private_key.private_bytes(
|
| 49 |
+
encoding=serialization.Encoding.PEM,
|
| 50 |
+
format=serialization.PrivateFormat.PKCS8,
|
| 51 |
+
encryption_algorithm=serialization.NoEncryption())
|
| 52 |
+
pem_public = public_key.public_bytes(
|
| 53 |
+
encoding=serialization.Encoding.PEM,
|
| 54 |
+
format=serialization.PublicFormat.SubjectPublicKeyInfo)
|
| 55 |
+
return pem_private.decode('utf-8'), pem_public.decode('utf-8')
|
| 56 |
+
|
| 57 |
+
def rsa_encrypt(public_key_pem, plaintext):
|
| 58 |
+
public_key = serialization.load_pem_public_key(public_key_pem.encode('utf-8'))
|
| 59 |
+
ciphertext = public_key.encrypt(
|
| 60 |
+
plaintext.encode(),
|
| 61 |
+
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)
|
| 62 |
+
)
|
| 63 |
+
return base64.b64encode(ciphertext).decode('utf-8')
|
| 64 |
+
|
| 65 |
+
def rsa_decrypt(private_key_pem, ciphertext):
|
| 66 |
+
private_key = serialization.load_pem_private_key(private_key_pem.encode('utf-8'), password=None)
|
| 67 |
+
decrypted_text = private_key.decrypt(
|
| 68 |
+
base64.b64decode(ciphertext),
|
| 69 |
+
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)
|
| 70 |
+
)
|
| 71 |
+
return decrypted_text.decode('utf-8')
|
| 72 |
+
|
| 73 |
+
def caesar_encrypt_decrypt(text, shift, mode):
|
| 74 |
+
if mode == "Encrypt":
|
| 75 |
+
return caesar_cipher_encrypt(text, int(shift))
|
| 76 |
+
else:
|
| 77 |
+
return caesar_cipher_decrypt(text, int(shift))
|
| 78 |
+
|
| 79 |
+
def aes_encrypt_decrypt(key, text, mode):
|
| 80 |
+
if mode == "Encrypt":
|
| 81 |
+
return aes_encrypt(key, text)
|
| 82 |
+
else:
|
| 83 |
+
return aes_decrypt(key, text)
|
| 84 |
+
|
| 85 |
+
def rsa_encrypt_decrypt(key, text, mode, key_type):
|
| 86 |
+
if mode == "Encrypt":
|
| 87 |
+
return rsa_encrypt(key, text)
|
| 88 |
+
else:
|
| 89 |
+
return rsa_decrypt(key, text)
|
| 90 |
+
|
| 91 |
+
def rsa_keys():
|
| 92 |
+
private_key, public_key = rsa_generate_keys()
|
| 93 |
+
return private_key, public_key
|
| 94 |
+
|
| 95 |
+
# Gradio interface
|
| 96 |
+
caesar = gr.Interface(fn=caesar_encrypt_decrypt, inputs=["text", "number", gr.inputs.Radio(["Encrypt", "Decrypt"])], outputs="text", title="Caesar Cipher")
|
| 97 |
+
aes = gr.Interface(fn=aes_encrypt_decrypt, inputs=["text", "text", gr.inputs.Radio(["Encrypt", "Decrypt"])], outputs="text", title="AES Encryption")
|
| 98 |
+
rsa = gr.Interface(fn=rsa_encrypt_decrypt, inputs=["text", "text", gr.inputs.Radio(["Encrypt", "Decrypt"]), gr.inputs.Radio(["Private Key", "Public Key"])], outputs="text", title="RSA Encryption")
|
| 99 |
+
rsa_key_gen = gr.Interface(fn=rsa_keys, inputs=None, outputs=["text", "text"], title="RSA Key Generation")
|
| 100 |
+
|
| 101 |
+
demo = gr.TabbedInterface([caesar, aes, rsa, rsa_key_gen], ["Caesar Cipher", "AES", "RSA", "RSA Key Generation"])
|
| 102 |
+
|
| 103 |
+
demo.launch()
|