Here is something else.
It allows you to encrypt and decrypt messages using a password.
/encrypt [password] [message]
/decrypt [password] [message]
The
command method is an example, I'm not sure how you are going to code it exactly into your source, but there is an example of how the if statement will work.
The other methods are used to operate the command.
- Code: Select all
private void command(string message)
{
if (message.IndexOf('/') == 0)
{
message = message.Substring(1).TrimStart();
if (message.Contains(" ") && message.Split(' ').Length >= 2)
{
if (message.Split(' ')[0].ToLower() == "encrypt")
{
onEncrypt(message.Split(' ')[1], message.Split(new char[] { ' ' }, 3)[2]);
}
else if (message.Split(' ')[0].ToLower() == "decrypt")
{
// Can just use Split[2] here as encrypted strings do not have any spaces within them
onDecrypt(message.Split(' ')[1], message.Split(' ')[2]);
}
}
}
command(Console.ReadLine());
}
private void onEncrypt(string code, string toEncrypt)
{
string Encrypted = Encrypt(toEncrypt, code);
Console.WriteLine("Encrypted message: ");
Console.WriteLine(Encrypted);
Clipboard.SetText(Encrypted);
Console.WriteLine("This message has been copied to your clipboard.");
}
private void onDecrypt(string code, string toDecrypt)
{
string Decrypted = Decrypt(toDecrypt, code);
Console.WriteLine("Decrypted message: ");
Console.WriteLine(Decrypted);
}
// If you change this, byte[] array must be >= 8
private readonly byte[] _salt = Encoding.ASCII.GetBytes("Conor12345:D");
private string Encrypt(string toEncrypt, string EncryptionCode)
{
try
{
string toReturn = null;
RijndaelManaged aesAlg = null;
try
{
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(EncryptionCode, _salt);
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(toEncrypt);
}
}
toReturn = Convert.ToBase64String(msEncrypt.ToArray());
}
}
finally
{
if (aesAlg != null)
{
aesAlg.Clear();
}
}
return toReturn;
}
catch { return "Error"; }
}
private string Decrypt(string toDecrypt, string EncryptionCode)
{
try
{
RijndaelManaged aesAlg = null;
string DecryptedString = null;
try
{
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(EncryptionCode, _salt);
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
byte[] bytes = Convert.FromBase64String(toDecrypt);
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
DecryptedString = srDecrypt.ReadToEnd();
}
}
}
}
finally
{
if (aesAlg != null)
{
aesAlg.Clear();
}
}
return DecryptedString;
}
catch { return "Error"; }
}
Dependable references:
- Code: Select all
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
In case you can't figure out why
System.Windows.Forms won't appear; a console application does not automatically add a reference to System.Windows.Forms.dll, right-click your project in Solution Explorer and select Add reference, and then find System.Windows.Forms and add it.