File Encryptor using TEA (Tiny Encryption Algorithm)

Ankit YadavAnkit Yadav
3 min read

GitHub Repo: https://github.com/ydv-ankit/file-encryptor

Features

  • TEA Encryption/Decryption: Full implementation of the Tiny Encryption Algorithm

  • File-based Operations: Encrypt and decrypt entire files

  • Custom Key Support: Use your own key files (converted to 128-bit hash)

  • Block Processing: Handles files of any size by processing in 8-byte blocks

  • Automatic Padding: Handles files that aren't multiples of 8 bytes

  • SHA-256 Key Hashing: Converts any key file to a 128-bit hash for consistent key size

How it Works

The implementation follows the standard TEA algorithm:

  1. Key Processing: Converts input key to SHA-256 hash and uses first 16 bytes

  2. Block Processing: Processes data in 8-byte blocks

  3. Encryption: 32 rounds of TEA encryption with delta constant

  4. Padding: Automatically adds null bytes to make data length multiple of 8

  5. Decryption: 32 rounds of TEA decryption with automatic padding removal

Core Components

  • TEA Algorithm: Complete implementation of Tiny Encryption Algorithm

  • Key Hashing: SHA-256 hashing for consistent 128-bit keys

  • Block Processing: 8-byte block encryption/decryption

  • Padding Management: Automatic padding during encryption and removal during decryption

Usage

Basic Commands

# Encrypt a file
./file-encryptor -e file=<file to encrypt> key=<keyfile>

# Decrypt a file
./file-encryptor -d file=<file to decrypt> key=<keyfile>

Command Line Arguments

  • -e: Encrypt the specified file

  • -d: Decrypt the specified file

  • file=<path>: Path to the input file

  • key=<path>: Path to the key file

Key File Format

The key file can contain any content. The system will:

  1. Read the entire key file

  2. Generate a SHA-256 hash of the content

  3. Use the first 16 bytes of the hash as the TEA key

Example of creating a key file:

# Create a key file with any content
echo "MySecretKey12345" > mykey.txt

# Or use a random key
openssl rand -base64 32 > mykey.txt

Output Files

  • Encryption: Overwrites the input file with encrypted content

  • Decryption: Overwrites the input file with decrypted content

Implementation Details

TEA Algorithm

The implementation includes all standard TEA operations:

  1. Key Setup: 4 uint32 values derived from 16-byte key

  2. Block Processing: 8-byte blocks with 32 rounds

  3. Delta Constant: Uses 0x9E3779B9 as the delta value

  4. Padding: Null byte padding for non-8-byte aligned data

Key Features

  • Complete TEA Implementation: Full encryption and decryption

  • SHA-256 Key Hashing: Consistent 128-bit key generation

  • Automatic Padding: Handles files of any size

  • Error Handling: Input validation and error reporting

  • Overflow Protection: Proper handling of large calculations

Security Notes

  • Uses TEA algorithm with 32 rounds

  • SHA-256 key hashing for consistent key size

  • Processes data in 8-byte blocks

  • Automatic padding management

Example Workflow

# 1. Create a test file
echo "Hello, this is a secret message!" > secret.txt

# 2. Create a key file
echo "MySecretKey12345" > mykey.txt

# 3. Encrypt the file
./file-encryptor -e file=secret.txt key=mykey.txt

# 4. Verify encryption (file should be unreadable)
cat secret.txt

# 5. Decrypt the file
./file-encryptor -d file=secret.txt key=mykey.txt

# 6. Verify decryption
cat secret.txt

Building this project is a good learning about crytography and encryption terms and techniques.

Thanks

0
Subscribe to my newsletter

Read articles from Ankit Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankit Yadav
Ankit Yadav

I am a developer exploring about tech and its capabilities to maximize outcomes