Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cryptography.hazmat.primitives.asymmetric import rsa, padding
- from cryptography.hazmat.primitives import serialization, hashes
- from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
- from cryptography.hazmat.primitives import padding as sym_padding
- import os
- # Step 1: Generate and Serialize RSA Keys
- def generate_key():
- # Generate a new RSA private key
- private_key = rsa.generate_private_key(
- public_exponent=65537, # Commonly used public exponent for RSA
- key_size=2048 # Key size of 2048 bits, providing a balance between security and performance
- )
- public_key = private_key.public_key() # Generate the corresponding public key from the private key
- # Serialize the private key to PEM format (PKCS8, no encryption)
- pem_private_key = private_key.private_bytes(
- encoding=serialization.Encoding.PEM,
- format=serialization.PrivateFormat.PKCS8,
- encryption_algorithm=serialization.NoEncryption() # No encryption on the private key itself
- )
- # Serialize the public key to PEM format
- pem_public_key = public_key.public_bytes(
- encoding=serialization.Encoding.PEM,
- format=serialization.PublicFormat.SubjectPublicKeyInfo
- )
- # Save the private key to a file
- with open('Attackers_PC/private_key.pem', 'wb') as f:
- f.write(pem_private_key)
- # Save the public key to a file
- with open('Attackers_PC/public_key.pem', 'wb') as f:
- f.write(pem_public_key)
- # Step 2: Encrypt a File
- def encrypt_file():
- # Load the public key from the PEM file
- with open('Attackers_PC/public_key.pem', 'rb') as f:
- pem_public_key = f.read() # Read the PEM file content
- public_key = serialization.load_pem_public_key(pem_public_key) # Load the public key from the PEM content
- # Read the file to encrypt
- with open('example.txt', 'rb') as f:
- file_data = f.read() # Read the file content into memory
- # os.remove('example.txt') # Deletes the file
- # Generate a random AES key for encryption (AES-256)
- aes_key = os.urandom(32) # Generate a random 256-bit key for AES
- # Encrypt the file data with AES
- iv = os.urandom(16) # Generate a random Initialization Vector (IV) for AES
- cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv)) # Set up AES in CBC mode with the key and IV
- encryptor = cipher.encryptor() # Create an encryptor object
- padder = sym_padding.PKCS7(
- algorithms.AES.block_size).padder() # Create a padder to pad the data to a multiple of the block size
- padded_data = padder.update(file_data) + padder.finalize() # Pad the file data
- encrypted_data = encryptor.update(padded_data) + encryptor.finalize() # Encrypt the padded data
- # Encrypt the AES key with the RSA public key
- encrypted_aes_key = public_key.encrypt(
- aes_key, # Encrypt the AES key itself
- padding.OAEP( # Use OAEP padding for the RSA encryption
- mgf=padding.MGF1(algorithm=hashes.SHA256()), # Mask Generation Function based on SHA-256
- algorithm=hashes.SHA256(), # Hash algorithm for OAEP
- label=None # No label used
- )
- )
- # Save the encrypted file data and IV to a file
- with open('encrypted_file.bin', 'wb') as f:
- f.write(iv + encrypted_data)
- # Save the encrypted AES key to a file
- with open('Attackers_PC/encrypted_key.bin', 'wb') as f:
- f.write(encrypted_aes_key)
- # Print statements for debugging (to see the original, encrypted data, and encrypted AES key)
- print("Original file data:", file_data)
- print("Encrypted file data:", encrypted_data)
- print("Encrypted AES key:", encrypted_aes_key)
- # Step 3: Decrypt the Encrypted File
- def decrypt_file():
- # Load the private key from the PEM file
- with open('Attackers_PC/private_key.pem', 'rb') as f:
- pem_private_key = f.read()
- private_key = serialization.load_pem_private_key(pem_private_key, password=None)
- # Read the encrypted file data and the IV
- with open('encrypted_file.bin', 'rb') as f:
- iv_and_encrypted_data = f.read()
- iv = iv_and_encrypted_data[:16]
- encrypted_data = iv_and_encrypted_data[16:]
- # Read the encrypted AES key
- with open('Attackers_PC/encrypted_key.bin', 'rb') as f:
- encrypted_aes_key = f.read()
- # Decrypt the AES key with the RSA private key
- aes_key = private_key.decrypt(
- encrypted_aes_key,
- padding.OAEP(
- mgf=padding.MGF1(algorithm=hashes.SHA256()),
- algorithm=hashes.SHA256(),
- label=None
- )
- )
- # Decrypt the file data with AES
- cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv))
- decryptor = cipher.decryptor()
- decrypted_padded_data = decryptor.update(encrypted_data) + decryptor.finalize()
- unpadder = sym_padding.PKCS7(algorithms.AES.block_size).unpadder()
- decrypted_data = unpadder.update(decrypted_padded_data) + unpadder.finalize()
- # Save the decrypted data to a new file
- with open('decrypted_example.txt', 'wb') as f:
- f.write(decrypted_data)
- # Example usage
- generate_key()
- input("GENERATING KEYS...")
- encrypt_file()
- input("ENCRYPTING FILE...")
- decrypt_file()
- input("DECRYPTING FILE")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement