Skip to content

Instantly share code, notes, and snippets.

@ambujraj
Created May 8, 2024 14:59
Show Gist options
  • Save ambujraj/fd5130eb984840bfed738a53c0985831 to your computer and use it in GitHub Desktop.
Save ambujraj/fd5130eb984840bfed738a53c0985831 to your computer and use it in GitHub Desktop.
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os
def decrypt_file(password, input_file, output_file):
# Open the input file and read the salt, IV, and ciphertext
with open(input_file, 'rb') as f:
salt = f.read(16)
iv = f.read(16)
ciphertext = f.read()
# Derive the key from the password and salt using PBKDF2
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = kdf.derive(password.encode())
# Create AES cipher in CBC mode with the given IV
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
# Decrypt the ciphertext
decrypted_padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
# Unpad the decrypted plaintext
unpadder = padding.PKCS7(128).unpadder()
decrypted_plaintext = unpadder.update(decrypted_padded_plaintext) + unpadder.finalize()
# Write the decrypted plaintext to the output file
with open(output_file, 'wb') as f:
f.write(decrypted_plaintext)
# Example usage
password = "your_password"
input_file = "encrypted_file.enc"
output_file = "decrypted_file.txt"
decrypt_file(password, input_file, output_file)
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os
def encrypt_file(password, input_file, output_file):
# Derive a key from the password using PBKDF2
salt = os.urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = kdf.derive(password.encode())
# Generate a random initialization vector
iv = os.urandom(16)
# Create AES cipher in CBC mode with PKCS7 padding
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
# Open input and output files
with open(input_file, 'rb') as f:
plaintext = f.read()
# Pad the plaintext
padder = padding.PKCS7(128).padder()
padded_plaintext = padder.update(plaintext) + padder.finalize()
# Encrypt the padded plaintext
ciphertext = encryptor.update(padded_plaintext) + encryptor.finalize()
# Write the salt, IV, and ciphertext to the output file
with open(output_file, 'wb') as f:
f.write(salt)
f.write(iv)
f.write(ciphertext)
# Example usage
password = "your_password"
input_file = "input.txt"
output_file = "encrypted_file.enc"
encrypt_file(password, input_file, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment