File encrypter Learn programming C# Sharp

Lesson:

File Management


Exercise:

File encrypter 12


Objetive:

Create a program to encrypt a text file into another text file. It must include the encrypter class you have created previously (in January 17th)


Code:

using System.IO;
using System;
class TextFileEncrypter
{
    static void Main(string[] args)
    {
        StreamReader inputFile;
        StreamWriter outputFile;
        string line;
        string name;

        if (args.Length < 1)
        {
            Console.WriteLine("Not enough parameters!");
            Console.WriteLine("Enter file name: ");
            name = Console.ReadLine();
        }
        else
            name = args[0];

        inputFile = File.OpenText(name);
        outputFile = File.CreateText(name + ".encrypted");

        do
        {

            line = inputFile.ReadLine();
            if (line != null)
            {
                string newText = Encrypter.Encrypt(line);
                outputFile.WriteLine(newText);
            }

        } while (line != null);

        inputFile.Close();
        outputFile.Close();
    }
}

class Encrypter
{
    public static string Encrypt(string originalString)
    {
        string result = "";

        foreach (char letter in originalString)
        {
            char newLetter = letter;
            newLetter++;
            result += newLetter;
        }
        return result;
    }

    public static string Decrypt(string originalString)
    {
        string result = "";

        foreach (char letter in originalString)
        {
            char newLetter = letter;
            newLetter--;
            result += newLetter;
        }
        return result;
    }
}