05-13-2010, 03:19 PM
Here's the source, Learn from it don't just Copy/Paste.
Have fun
Code:
#region using
using System;
#endregion
namespace EncryptAndDecrypt
{
#region class
class Program
{
#region main
static void Main(string[] args)
{
start:
Console.Clear();
Console.WriteLine("Would you like to encrypt or decrypt?");
String crypt = Console.ReadLine();
if (crypt == "encrypt")
{
Console.Clear();
Console.WriteLine("Text to encrypt:");
String toEncode = Console.ReadLine();
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnvalue = System.Convert.ToBase64String(toEncodeAsBytes);
Console.WriteLine(returnvalue);
Console.WriteLine("\n\n\n");
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
goto start;
}
if (crypt == "decrypt")
{
try
{
Console.Clear();
Console.WriteLine("Text to decrypt:");
String decrypt = Console.ReadLine();
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode = Convert.FromBase64String(decrypt);
int charCount = utf8Decode.GetCharCount(todecode, 0, todecode.Length);
char[] decodedc = new char[charCount];
utf8Decode.GetChars(todecode, 0, todecode.Length, decodedc, 0);
string result = new String(decodedc);
Console.WriteLine(result);
Console.WriteLine("\n\n\n");
Console.WriteLine("Type 'Back' to go back to the start");
string read = Console.ReadLine();
if (read == "back")
{
goto start;
}
}
catch (Exception e)
{
Console.WriteLine("\n\n");
Console.WriteLine("Error decoding");
Console.WriteLine("Type back to go back or info for info on the error.");
string readl = Console.ReadLine();
if (readl == "back")
{
goto start;
}
if (readl == "info")
{
Console.WriteLine("\n\n");
Console.WriteLine(e.ToString());
Console.ReadLine();
Console.WriteLine("\n\n");
Console.WriteLine("Type 'Back' to go back");
string readb = Console.ReadLine();
if (readb == "back")
{
goto start;
}
}
}
#endregion main
}
#endregion class
}
}
}
Have fun