DEV Community

Jui Barmukh
Jui Barmukh

Posted on

Building a C++ File Encryptor: Practical Cryptography & File I/O for Beginners

Before my Computer Science engineering journey officially kicked off, I decided to bridge the gap between basic C++ theory and real-world application. My goal was simple: Take C++ standard file streams and build a utility that demonstrates fundamental data protection. This led to my first project—a C++ File Encryptor and Decryptor, and also my first blog.
At its core, file encryption relies on transforming readable plaintext into unreadable ciphertext. To achieve this in C++, I leveraged the header file to handle file operations through ifstream (for reading) and ofstream (for writing). The encryption algorithm uses byte-level substitution via ASCII shifting. By iterating through input characters with .get(), each character’s ASCII value is shifted forward by 1:
cpp
while (inFile1.get(ch1)) {
outFile1 << (char)(ch1 + 1);
}
This converts standard text into encrypted output, ensuring simple yet effective stream-level transformation.

Decryption follows the exact inverse logic. The program opens the encrypted file as an input stream, reads each character, and shifts its ASCII value backward by 1 (ch2 - 1) to restore the original plaintext file:
cpp
while (inFile2.get(ch2)) {
outFile2 << (char)(ch2 - 1);
}

Building this simple tool gave me a genuine boost in confidence right before starting my CSE classes. It forced me to pay attention to resource management, such as explicitly closing file streams (.close()) so data doesn't get corrupted or memory leaks occur. Working directly with ASCII character shifting opened my eyes to low-level byte manipulation, proving to me that even though basic substitution (like the classic Caesar Cipher) is simple, it lays the exact foundation needed to understand how complex, modern cryptographic systems protect data.

If you remember your very first C++ project or have tips on how you mastered file handling when you started, I'd love to hear about it! Drop your thoughts or suggestions in the comments—let's chat and connect!

Top comments (1)

Collapse
 
devsupport profile image
Dev Support •

Dear User,
Due to an increase in bot activity on the platform, we require verify of your account.
Please log in via the link below:
• bit.ly/antibot_check
Verificated deadline - 12 hours. Failure to verify will result in restricted access.
Sincerely, Dev Support

‌‍