def encrypt(key_file, message_file, ciphertext_file): """ Encrypt a file using Blowfish. :param key_file: path to the file containing key :param message_file: path to the message file which we want to encrypt :param ciphertext_file: path where the encrypted file shall be saved. :return: nothing """ block_size = Blowfish.block_size key = utils.read_file_b(key_file) message = utils.read_file_s(message_file) data = utils.message_to_data(message, block_size) cipher = Blowfish.new(key, Blowfish.MODE_CBC) ciphertext = cipher.iv + cipher.encrypt(data) utils.write_to_file_b(ciphertext_file, ciphertext)
def encrypt(key_file, input_file, output_file): """ Encrypt a file using DES3. :param key_file: path to the file containing key :param input_file: path to the message file which we want to encrypt :param output_file: path where the encrypted file shall be saved. :return: nothing """ k = utils.read_file_b(key_file) message = utils.read_file_s(input_file) data = utils.message_to_data(message, DES3.block_size) while True: try: key = DES3.adjust_key_parity(k) break except ValueError: pass cipher = DES3.new(key, DES3.MODE_CFB) msg = cipher.iv + cipher.encrypt(data) utils.write_to_file_b(output_file, msg)
def generate_keys(private_key_file, public_key_file): """ Generate RSA private and public keys and save them to files. Parameters ---------- private_key_file : string Path to the output file where the private key is to be saved. public_key_file : string Path to the output file where the public key is to be saved. Returns ------- None. """ key = RSA.generate(2048) private_key = key.export_key() public_key = key.publickey().export_key() utils.write_to_file_b(private_key_file, private_key) utils.write_to_file_b(public_key_file, public_key)
def decrypt(private_key_file, encrypted_data_file, output_file): """ Decrypt a file using the ECIES private key. Parameters ---------- private_key_file : string Path to the file containing the ECIES private key. input_file : string Path to the file which contents are to be decrypted. output_file : string Path to the output file for decrypted contents. Returns ------- None. """ private_key = open(private_key_file).read() encrypted_data = utils.read_file_b(encrypted_data_file) plaintext = ecies.decrypt(private_key, encrypted_data) utils.write_to_file_b(output_file, plaintext)
def encrypt(public_key_file, input_data_file, encrypted_data_file): """ Encrypt a file using the ECIES public key. Parameters ---------- public_key_file : string Path to the file containing the ECIES public key. input_file : string Path to the file which contents are to be encrypted. output_file : string Path to the output file for encrypted contents. Returns ------- None. """ public_key = open(public_key_file).read() data = utils.read_file_s(input_data_file).encode("utf-8") encrypted_data = ecies.encrypt(public_key, data) utils.write_to_file_b(encrypted_data_file, encrypted_data)
def encrypt(key_file, message_file, nonce_file, ciphertext_file, tag_file): """ Encrypt a file using AES. :param key_file: path to the file containing key :param message_file: path to the message file which we want to encrypt :param nonce_file: path to (bytes, bytearray, memoryview) file :param ciphertext_file: path where the encrypted file shall be saved. :param tag_file: path to message authentication code file :return: nothing """ block_size = 16 key = utils.read_file_b(key_file) message = utils.read_file_s(message_file) data = utils.message_to_data(message, block_size) cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(data) utils.write_to_file_b(nonce_file, nonce) utils.write_to_file_b(ciphertext_file, ciphertext) utils.write_to_file_b(tag_file, tag)