using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Security.Cryptography;
using System.IO;
namespace WebLibrary
{
public class Utility
{
private static byte[] DEFAULT_SALT = Encoding.ASCII.GetBytes("n0s4l7!!");
private static Dictionary _accountTypes = null;
///
/// Gets a string description for a value on an enumerator making it friendly for
/// display purposes. This method was derived an MSDN blog found at:
/// http://blogs.msdn.com/b/abhinaba/archive/2005/10/20/483000.aspx
///
///
///
public static string GetDescription(string name, Type t)
{
MemberInfo[] memberInfo = t.GetMember(name);
if (memberInfo != null && memberInfo.Length > 0)
{
object[] attrs = memberInfo[0].GetCustomAttributes(
typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute)attrs[0]).Description;
}
return name;
}
///
/// Encrypt the given string using AES. This code has been adapted from a blog post from StackOverflow
/// which adapted the encryption method from another Rijndael example.
/// Source: http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net
///
/// The text to encrypt.
/// A password used to generate a key for encryption.
///
/// This call uses a default salt value and should only be used for less-secure encryption.
///
public static string EncryptStringAES(string plainText, string sharedSecret)
{
return Utility.EncryptStringAES(plainText, sharedSecret, DEFAULT_SALT);
}
///
/// Encrypt the given string using AES. This code has been adapted from a blog post from StackOverflow
/// which adapted the encryption method from another Rijndael example.
/// Source: http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net
///
/// The text to encrypt.
/// A password used to generate a key for encryption.
/// The salt to use for encryption.
public static string EncryptStringAES(string plainText, string sharedSecret, byte[] salt)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
string outStr = null;
RijndaelManaged aesAlg = null;
try
{
// Generate the key from the shared secret and the salt.
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, salt);
// Create a RijndaelManaged object with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
// Create a decryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
swEncrypt.Write(plainText);
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
return outStr;
}
///
/// Decrypt the given string. This code has been adapted from a blog post from StackOverflow
/// which adapted the encryption method from another Rijndael example.
/// Source: http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net
///
/// The text to decrypt.
/// A password used to generate a key for decryption.
///
/// In order for decryption to succeed the cipherText must have been encrypted using
/// the exact same sharedSecret and salt as it was encrypted with. This method uses
/// a default salt value.
///
public static string DecryptStringAES(string cipherText, string sharedSecret)
{
return Utility.DecryptStringAES(cipherText, sharedSecret, DEFAULT_SALT);
}
///
/// Decrypt the given string. This code has been adapted from a blog post from StackOverflow
/// which adapted the encryption method from another Rijndael example.
/// Source: http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net
///
/// The text to decrypt.
/// A password used to generate a key for decryption.
/// The salt to use for decryption.
///
/// In order for decryption to succeed the cipherText must have been encrypted using
/// the exact same sharedSecret and salt as it was encrypted with.
///
public static string DecryptStringAES(string cipherText, string sharedSecret, byte[] salt)
{
if (string.IsNullOrEmpty(cipherText))
throw new ArgumentNullException("cipherText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
if (salt == null || salt.Length == 0)
throw new ArgumentNullException("salt");
RijndaelManaged aesAlg = null;
string plaintext = null;
try
{
// Generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, salt);
// Create a RijndaelManaged object with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
byte[] bytes = Convert.FromBase64String(cipherText);
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
}
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
return plaintext;
}
}
}