Android stores many personal documents, photos, and backups on the internal storage. If you need to move a file to a cloud service, share it via email, or simply keep it safe from prying eyes, encryption is the most reliable protection. Using OpenSSL inside Termux gives you full control over the cryptographic algorithm, key length, and password handling—all without rooting the device.
Note: This tutorial works on Android 8 (Oreo) and newer. Older devices may lack the required libc support for the OpenSSL binaries shipped with Termux.
pkg update && pkg upgrade -y
This may take a few minutes. Do not skip the upgrade – newer OpenSSL builds depend on recent libraries.
pkg install openssl
The command pulls the openssl binary and its dependencies.
openssl version
You should see something like OpenSSL 1.1.1l 24 Aug 2021. If an error appears, run pkg install libcrypt and retry.
OpenSSL supports many symmetric algorithms. For most everyday use, AES‑256‑CBC offers a good balance of security and speed. This tutorial uses the enc sub‑command, which handles key derivation from a password automatically.
If you need compliance with specific standards (e.g., FIPS), you can switch to AES‑256‑GCM. The steps are identical; just replace the algorithm name.
~/storage/downloads folder:
termux-setup-storage
This command asks for storage permission and creates a storage shortcut inside Termux.
myfile.pdf with your actual filename):
cd ~/storage/downloads
openssl enc -aes-256-cbc -salt -in myfile.pdf -out myfile.pdf.enc
Explanation of flags:
-aes-256-cbc – selects the cipher.-salt – adds a random 8‑byte salt, improving resistance against dictionary attacks.-in – source file.-out – destination encrypted file.After you enter the password, myfile.pdf.enc will appear in the same directory.
Warning: Forgetting the password makes the data unrecoverable. Store the password securely before proceeding.
ls -lh myfile.pdf myfile.pdf.enc
openssl enc -d -aes-256-cbc -in myfile.pdf.enc -out /tmp/check.pdf
If the password is correct, /tmp/check.pdf will be a valid copy. You can open it with any PDF viewer to confirm.
openssl enc -d -aes-256-cbc -in myfile.pdf.enc -out myfile_decrypted.pdf
You will be asked for the same password you used during encryption.
sha256sum myfile.pdf myfile_decrypted.pdf
The two hashes should match exactly.
If you regularly need to protect a batch of files, a tiny shell script saves time. Create encrypt.sh in your ~/bin (create the folder if it does not exist):
mkdir -p ~/bin
cat > ~/bin/encrypt.sh <<'EOF'
#!/data/data/com.termux/files/usr/bin/bash
# Simple wrapper for OpenSSL AES‑256‑CBC encryption
if [ $# -lt 1 ]; then
echo "Usage: $0 file1 [file2 ...]"
exit 1
fi
for src in "$@"; do
if [ ! -f "$src" ]; then
echo "File not found: $src"
continue
fi
dst="${src}.enc"
echo "Encrypting $src → $dst"
openssl enc -aes-256-cbc -salt -in "$src" -out "$dst"
done
EOF
chmod +x ~/bin/encrypt.sh
Now you can encrypt any number of files with a single command:
~/bin/encrypt.sh ~/storage/downloads/report.docx ~/storage/downloads/photo.jpg
The script prompts for the password once per file. For a single password across all files, you can add the -pass pass:YOUR_PASSWORD flag (but keep in mind that storing passwords in plain text is insecure).
Some users prefer asymmetric encryption (public‑key cryptography) so that the decryption key never travels with the encrypted data. OpenSSL can encrypt a symmetric key with an RSA public key, then store the encrypted key alongside the ciphertext.
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in private_key.pem -out public_key.pem
openssl rsautl -encrypt -inkey public_key.pem -pubin -in myfile.pdf -out myfile.pdf.key.enc
openssl enc -aes-256-cbc -salt -in myfile.pdf -out myfile.pdf.enc -pass file:myfile.pdf.key.enc
The first command encrypts a random symmetric key with the RSA public key. The second command uses that encrypted key as the password source.
openssl rsautl -decrypt -inkey private_key.pem -in myfile.pdf.key.enc -out myfile.key
openssl enc -d -aes-256-cbc -in myfile.pdf.enc -out myfile_decrypted.pdf -pass file:myfile.key
After decryption, securely erase the temporary myfile.key file:
shred -u myfile.key
Handling private keys on a non‑rooted phone carries risk. If the key file is exposed, anyone can decrypt your data. Store the private key in a password‑protected archive or a dedicated secure app.
pkg install openssl again.termux-setup-storage and grant the permission dialog that appears.split command) and encrypt each chunk separately.pkg upgrade openssl periodically.AES‑256‑GCM. The command syntax changes only the algorithm name.With Termux and OpenSSL you gain a full‑featured, cross‑platform encryption toolbox on Android without needing root access. The steps above let you protect single files, automate batch encryption, and even employ public‑key cryptography for extra safety. Because the process runs entirely on the device, your data never leaves the phone unless you deliberately upload the .enc file to the cloud.
Feel free to adapt the scripts to your workflow, experiment with different ciphers, or combine this method with other Termux utilities like rsync for secure, encrypted backups.









