CoolFace
Apppublic

Godfrey123/image_encryption_decryption

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py131 linesDownload Raw Back to root
1import os2import tkinter as tk3from tkinter import filedialog4from PIL import Image5from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes6from cryptography.hazmat.backends import default_backend7 8class CBCEncryption:9    def __init__(self, key, iv):10        self.cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())11        self.encryptor = self.cipher.encryptor()12        self.decryptor = self.cipher.decryptor()13 14    def encrypt(self, image):15        return self.encryptor.update(image)16 17    def decrypt(self, image):18        return self.decryptor.update(image)19 20    def finalize_encrypt(self):21        return self.encryptor.finalize()22 23    def finalize_decrypt(self):24        return self.decryptor.finalize()25 26def EncryptImage(encryption, image_path, output_path):27    image = Image.open(image_path)28    image.save('temp.bmp')29    with open('temp.bmp', 'rb') as reader:30        with open(output_path, 'wb') as writer:31            image_data = reader.read()32            header, body = image_data[:54], image_data[54:]33            body += b'\x35' * (16 - (len(body) % 16))34            body = encryption.encrypt(body) + encryption.finalize_encrypt()35            writer.write(header + body)36    os.remove('temp.bmp')37 38def DecryptImage(decryption, image_path, output_path):39    with open(image_path, 'rb') as reader:40        with open(output_path, 'wb') as writer:41            image_data = reader.read()42            header, body = image_data[:54], image_data[54:]43            body = decryption.decrypt(body)44            unpadded_body = unpad_data(body)45            writer.write(header + unpadded_body)46    decrypted_image = Image.open(output_path)47    decrypted_image.show()48 49def unpad_data(data):50    padding_byte = data[-1]51    padding_length = padding_byte if padding_byte < 16 else 1652    unpadded_data = data[:-padding_length]53    return unpadded_data54 55def main():56    root = tk.Tk()57    root.title("AES CBC Encryption/Decryption")58 59    label_file = tk.Label(root, text="Select Image:")60    label_file.pack()61 62    entry_file = tk.Entry(root)63    entry_file.pack()64 65    def select_file():66        file_path = filedialog.askopenfilename()67        entry_file.delete(0, tk.END)68        entry_file.insert(0, file_path)69 70    button_browse = tk.Button(root, text="Browse", command=select_file)71    button_browse.pack()72 73    label_key = tk.Label(root, text="Enter Key:")74    label_key.pack()75 76    entry_key = tk.Entry(root)77    entry_key.pack()78 79    def action_encrypt():80        key = entry_key.get()81        if len(key) > 32:82            print('Key is too long. Maximum key length is 32 characters.')83            return84        key = key.encode('utf-8')85        key = key.ljust(32, b'\x35')86 87        iv = key[:16]88        iv = bytearray(iv)89        for i in range(16):90            iv[i] = iv[i] ^ 0x3591        iv = bytes(iv)92 93        AesCbc = CBCEncryption(key, iv)94        image_path = entry_file.get()95        output_path = filedialog.asksaveasfilename()96        EncryptImage(encryption=AesCbc, image_path=image_path, output_path=output_path)97        print("Encryption done!")98        print("Please keep your key safe.")99        root.destroy()100 101    def action_decrypt():102        key = entry_key.get()103        if len(key) > 32:104            print('Key is too long. Maximum key length is 32 characters.')105            return106        key = key.encode('utf-8')107        key = key.ljust(32, b'\x35')108 109        iv = key[:16]110        iv = bytearray(iv)111        for i in range(16):112            iv[i] = iv[i] ^ 0x35113        iv = bytes(iv)114 115        AesCbc = CBCEncryption(key, iv)116        image_path = entry_file.get()117        output_path = filedialog.asksaveasfilename()118        DecryptImage(decryption=AesCbc, image_path=image_path, output_path=output_path)119        print("Decryption done!")120 121    button_encrypt = tk.Button(root, text="Encrypt", command=action_encrypt)122    button_encrypt.pack()123 124    button_decrypt = tk.Button(root, text="Decrypt", command=action_decrypt)125    button_decrypt.pack()126 127    root.mainloop()128 129if __name__ == '__main__':130    main()131