Learn how to add file encryption to your AI app for enhanced security and data protection in easy steps.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import os
def encryptFile(file_path, key):
# Read file content
with open(file_path, 'rb') as f:
data = f.read()
iv = os.urandom(16) // Generate a random initialization vector
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
# Ensure data is padded to block size (padding not shown for brevity)
encrypted_data = encryptor.update(data) + encryptor.finalize()
# Write encrypted data and iv to file or separate storage
with open(file_path + ".enc", 'wb') as f:
f.write(iv + encrypted_data)
return file_path + ".enc"
const crypto = require('crypto');
const fs = require('fs');
function encryptFile(filePath, key) {
// Read file content synchronously
const data = fs.readFileSync(filePath);
const iv = crypto.randomBytes(16); // Generate a random IV
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
// Encrypt and concatenate final block
let encrypted = cipher.update(data);
encrypted = Buffer.concat([encrypted, cipher.final()]);
// Write iv and encrypted data to file
fs.writeFileSync(filePath + '.enc', Buffer.concat([iv, encrypted]));
return filePath + '.enc';
}
Explore the top three use cases of AI app file encryption to enhance security and protect sensitive data effectively.
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â