CNSA-216-FP/FWA_MAIN/Crypt.cs

77 lines
3.2 KiB
C#

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace FWA_MAIN
{
public class Crypt
{
static private string encryptKey =
"CXvA@xsFmBik#E4#7tf4zY7JK^qaTUwFYw^QuC7sipSyBPfkS$qFFU%ZQmRb5GY85unfCmEw@dK7Bhiyt%3asL62yygk6x6D@@D8CUnJfF8@F$C9vtJgGwNhmSxvo#Lh";
static private string encryptSalt =
"gab-purgatory-studio-atlantic-ladle-challenge-slaw-unshaken-eastward-caring-deftly-devious-crudeness-walrus-glorifier-unsteady-sauciness-feminist-jailbreak-upside";
static public string Encrypt(string cleartext)
{
byte[] plainText = Encoding.UTF8.GetBytes(cleartext);
using (RijndaelManaged rijndaelCipher = new RijndaelManaged())
{
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(Encoding.ASCII.GetBytes(encryptKey),
Encoding.ASCII.GetBytes(encryptSalt));
using (ICryptoTransform encryptor =
rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream =
new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainText, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
string base64 = Convert.ToBase64String(memoryStream.ToArray());
// Generate a string that won't get screwed up when passed as a query string.
string urlEncoded = HttpUtility.UrlEncode(base64);
return urlEncoded;
}
}
}
}
}
static public string Decrypt(string cleartext)
{
byte[] encryptedData = Convert.FromBase64String(cleartext);
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(Encoding.ASCII.GetBytes(encryptKey),
Encoding.ASCII.GetBytes(encryptSalt));
using (RijndaelManaged rijndaelCipher = new RijndaelManaged())
{
using (ICryptoTransform decryptor =
rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream =
new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
cryptoStream.Read(plainText, 0, plainText.Length);
string utf8 = Encoding.UTF8.GetString(plainText);
return utf8.Trim('\0');
}
}
}
}
}
}
}