#region (C) 1998-2009 AUTUMOON LAB.
/* *************************************************
* Solution: Autumoon Code Library - Team Edition.
*
* Project: Common Foundation.
*
* Description: The common tool class.
*
* Author: ZeroCool.
*
* Created: 03/02/2008
*
* (C) 1998-2009 Autumoon Lab.
* ************************************************/
#endregion
#region ALL REFERENCE
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
#endregion
namespace Autumoon.CodeLibrary.CommonFoundation
{
public class Utilities
{
// Properties and fields.
#region Properties and fields.
private static byte[] _bytes = ASCIIEncoding.ASCII.GetBytes("ACLT2008");
#endregion
// Constructors.
#region Constructors
#endregion
// Public methods.
#region Public Methods
///
/// Encrypt a string.
///
/// The original string.
/// The encrypted string.
/// This exception will be thrown when the original string is null or empty.
public static string Encrypt(string originalString)
{
if (String.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException("The string which needs to be encrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(_bytes, _bytes), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
///
/// Decrypt a crypted string.
///
/// The crypted string.
/// The decrypted string.
/// This exception will be thrown when the crypted string is null or empty.
public static string Decrypt(string cryptedString)
{
if (String.IsNullOrEmpty(cryptedString))
{
throw new ArgumentNullException("The string which needs to be decrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(_bytes, _bytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
///
/// Handle SQL sensitive charactors in original text.
///
/// The orginal code content.
/// The SQL storable text.
public static string HandleSQLSensitiveChars(string originalText)
{
if (String.IsNullOrEmpty(originalText))
{
return String.Empty;
}
return originalText.Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace(" ", " ").Replace("©", "©").Replace("®", "®").Replace("&", "&").Replace("\'", "'");
}
///
/// Handle the SQL stored text for resuming SQL sensitive charactors.
///
/// The SQL stored text.
/// The orginal code text.
public static string ResumeSQLStoredText(string storableText)
{
if (String.IsNullOrEmpty(storableText))
{
return String.Empty;
}
return storableText.Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace(" ", " ").Replace("©", "©").Replace("®", "®").Replace("&", "&").Replace("'", "\'");
}
#endregion
}
}