File Encrypter - C# Programming Exercise

This C# exercise involves creating a program to encrypt a text file into another text file. The program should be able to read the content of a text file, apply an encryption algorithm to it, and save the content into a new file, ensuring that the resulting file is protected and cannot be read without first being decrypted. This exercise is perfect for learning about text file manipulation in C#, as well as the basic concepts of encryption.

The program should be able to read a text file, convert its content into an encrypted format, and save that content into a new file. You can use a simple encryption algorithm like substitution cipher or a more advanced technique, depending on your choice. The key here is to ensure that the resulting file is not easily readable without going through the decryption process. This exercise is great for learning to work with text files and strings in C#, as well as implementing encryption and security techniques.

It is important to note that the .cs file should include a comment with your name, ensuring that the code is authored by you. Furthermore, this exercise will also help you become familiar with reading and writing files in C#, and using simple encryption algorithms to protect information.

 Category

File Management

 Exercise

File Encrypter

 Objective

Create a program to encrypt a text file into another text file.

 Write Your C# Exercise

// Import the System namespace for basic functionality
using System;  

// Import the IO namespace for file handling
using System.IO;  

// Encrypt class to handle the encryption logic
class Encrypter
{
    // Method to encrypt the content of the input file and save to the output file
    public static void EncryptFile(string inputFilePath, string outputFilePath, string key)
    {
        // Read the content of the input file
        string content = File.ReadAllText(inputFilePath);  

        // Encrypt the content using the key
        string encryptedContent = EncryptContent(content, key);  

        // Write the encrypted content to the output file
        File.WriteAllText(outputFilePath, encryptedContent);  
    }

    // Method to encrypt the content of the file using a simple Caesar cipher
    private static string EncryptContent(string content, string key)
    {
        // Convert the key to a number (for simplicity, just use the length of the key)
        int keyLength = key.Length;  

        // Create a character array to store the encrypted content
        char[] encryptedChars = new char[content.Length];  

        // Loop through each character in the content
        for (int i = 0; i < content.Length; i++)  
        {
            // Get the current character
            char currentChar = content[i];  

            // Encrypt the character using the Caesar cipher (shift by keyLength)
            char encryptedChar = (char)(currentChar + keyLength);  

            // Store the encrypted character
            encryptedChars[i] = encryptedChar;  
        }

        // Convert the encrypted character array back to a string
        return new string(encryptedChars);  
    }
}

class FileEncrypter
{
    static void Main(string[] args)
    {
        // Ask the user for the input file path
        Console.WriteLine("Enter the path of the file to encrypt:");  

        // Get the input file path from the user
        string inputFilePath = Console.ReadLine();  

        // Ask the user for the output file path
        Console.WriteLine("Enter the output file path to save the encrypted file:");  

        // Get the output file path from the user
        string outputFilePath = Console.ReadLine();  

        // Ask the user for the encryption key (simple string-based key)
        Console.WriteLine("Enter the encryption key (any string):");  

        // Get the key from the user
        string key = Console.ReadLine();  

        // Start of try block to handle any file or encryption errors
        try
        {
            // Use the Encrypter class to encrypt the input file and save to the output file
            Encrypter.EncryptFile(inputFilePath, outputFilePath, key);  

            // Inform the user that the encryption process is complete
            Console.WriteLine("File has been successfully encrypted and saved as: " + outputFilePath);  
        }
        catch (Exception ex)  // Catch any exceptions that may occur during the process
        {
            // Print the exception message if an error occurs
            Console.WriteLine("An error occurred: " + ex.Message);  
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Count words

    This C# exercise involves creating a program to count the number of words stored in a text file. The program should read the content of a text file, process it, and c...

  •  BMP width and height, BinaryReader

    This C# exercise involves creating a program to display the width and height of a BMP file using a BinaryReader. The program should read the BMP file header an...

  •  TXT to HTML translator

    This C# exercise involves creating a "Text to HTML converter". The program should read a source text file and create an HTML file from its contents. The destination f...

  •  Invert binary file V2

    This C# exercise involves creating a program to "invert" a file using a FileStream. The program should create a file with the same name as the original file, b...

  •  BMP width & height, FileStream

    This C# exercise involves creating a program that reads a BMP file using a FileStream and displays its width and height. The BMP format has a spe...

  •  File copier

    This C# exercise involves creating a program that copies a source file to a destination file using a FileStream and a block size of 512 KB. The program should ...