Sure, here is an example script in Python for encrypting and decrypting files using the Blowfish algorithm:
from Crypto.Cipher import Blowfish from Crypto.Random import get_random_bytes def encrypt_file(input_file, output_file, key): cipher = Blowfish.new(key, Blowfish.MODE_CBC) with open(input_file, 'rb') as f_in, open(output_file, 'wb') as f_out: iv = get_random_bytes(8) f_out.write(iv) cipher.encrypt(iv) while True: chunk = f_in.read(8) if len(chunk) == 0: break elif len(chunk) % 8 != 0: chunk += b' ' * (8 - len(chunk) % 8) f_out.write(cipher.encrypt(chunk)) def decrypt_file(input_file, output_file, key): cipher = Blowfish.new(key, Blowfish.MODE_CBC) with open(input_file, 'rb') as f_in, open(output_file, 'wb') as f_out: iv = f_in.read(8) cipher.decrypt(iv) while True: chunk = f_in.read(8) if len(chunk) == 0: break f_out.write(cipher.decrypt(chunk).rstrip(b' ')) # Usage example key = b'0123456789abcdef' encrypt_file('input.txt', 'encrypted.txt', key) decrypt_file('encrypted.txt', 'decrypted.txt', key)
You will need to have the pycryptodome library installed to use this script. You can install it using pip install pycryptodome.
This script reads the content of an input file, encrypts it using the Blowfish algorithm with a given key, and writes the encrypted data to an output file. It then reads the encrypted file, decrypts it using the same key, and writes the decrypted data to another output file.
Make sure to replace 'input.txt' with the path of your input file and 'encrypted.txt', 'decrypted.txt' with the paths where you want to save the encrypted and decrypted files. You can also change the key to any 16-byte (or less) value.
Sure, here is an example script in Python for encrypting and decrypting files using the Blowfish algorithm:
from Crypto.Cipher import Blowfishfrom Crypto.Random import get_random_bytes
def encrypt_file(input_file, output_file, key):
cipher = Blowfish.new(key, Blowfish.MODE_CBC)
with open(input_file, 'rb') as f_in, open(output_file, 'wb') as f_out:
iv = get_random_bytes(8)
f_out.write(iv)
cipher.encrypt(iv)
while True:
chunk = f_in.read(8)
if len(chunk) == 0:
break
elif len(chunk) % 8 != 0:
chunk += b' ' * (8 - len(chunk) % 8)
f_out.write(cipher.encrypt(chunk))
def decrypt_file(input_file, output_file, key):
cipher = Blowfish.new(key, Blowfish.MODE_CBC)
with open(input_file, 'rb') as f_in, open(output_file, 'wb') as f_out:
iv = f_in.read(8)
cipher.decrypt(iv)
while True:
chunk = f_in.read(8)
if len(chunk) == 0:
break
f_out.write(cipher.decrypt(chunk).rstrip(b' '))
# Usage example
key = b'0123456789abcdef'
encrypt_file('input.txt', 'encrypted.txt', key)
decrypt_file('encrypted.txt', 'decrypted.txt', key)
You will need to have the pycryptodome library installed to use this script. You can install it using pip install pycryptodome.
This script reads the content of an input file, encrypts it using the Blowfish algorithm with a given key, and writes the encrypted data to an output file. It then reads the encrypted file, decrypts it using the same key, and writes the decrypted data to another output file.
Make sure to replace 'input.txt' with the path of your input file and 'encrypted.txt', 'decrypted.txt' with the paths where you want to save the encrypted and decrypted files. You can also change the key to any 16-byte (or less) value.