Rijndael with 2 16byte strings :
Code: Select all
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace HWREncryption
{
//class implements Rijndael algorithm
public class HwrEncryptor
{
public static string EncryptData(string original, string key)
{
byte[] toEncrypt;
byte[] encrypted;
ASCIIEncoding textConverter = new ASCIIEncoding();
RijndaelManaged myRijndael = new RijndaelManaged();
//Set the key and IV.
myRijndael.Key = Encoding.ASCII.GetBytes("key1000000000000");
myRijndael.IV = Encoding.ASCII.GetBytes("key2000000000000");
//Get an encryptor.
ICryptoTransform encryptor = myRijndael.CreateEncryptor(myRijndael.Key, myRijndael.IV);
//Encrypt the data.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
//Convert the data to a byte array.
toEncrypt = textConverter.GetBytes(original);
//Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
//Get encrypted array of bytes.
encrypted = msEncrypt.ToArray();
csEncrypt.Close();
msEncrypt.Close();
return ASCIIEncoding.Unicode.GetString(encrypted);
}
public static string DecryptData(string data, string key)
{
byte[] encrypted = ASCIIEncoding.Unicode.GetBytes(data);
byte[] fromEncrypt = new byte[encrypted.Length]; ;
ASCIIEncoding textConverter = new ASCIIEncoding();
RijndaelManaged myRijndael = new RijndaelManaged();
//Set the key and IV.
myRijndael.Key = Encoding.ASCII.GetBytes("key1000000000000"); ;
myRijndael.IV = Encoding.ASCII.GetBytes("key2000000000000");
MemoryStream msDescrypt = new MemoryStream(encrypted);
//Get an descryptor.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV);
CryptoStream csDecrypt = new CryptoStream(msDescrypt, decryptor, CryptoStreamMode.Read);
//Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
msDescrypt.Close();
csDecrypt.Close();
//Convert the byte array back into a string.
return textConverter.GetString(fromEncrypt);;
}
}
}