Programming Course in C# ¡Free!

File encrypter

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

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)

Output



Solution


using System.IO;
using System;

class FileEncrypter
{
static void Main()
{            
string name = Console.ReadLine();

StreamReader fileR = File.OpenText(name);
StreamWriter fileW = File.CreateText(name + ".encrypted");

string line;
do 
{
line = fileR.ReadLine();
if (line != null)
{
string newText = Encrypter.Encrypt(line);
fileW.WriteLine(newText);
}
} 
while(line != null);

fileR.Close();
fileW.Close();
}
}


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

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

return result;
}

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

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

return result;
}

}