Cryptography,一个C#写的加解密算法的类
一个加解密算法的类,如下:
Code
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SurvIT.Business.UserPrivilege
{
/// <summary>
/// Symmtric Crypto
/// </summary>
public class RijndaelCrypto
{
private SymmetricAlgorithm _objCryptoService;
private string _strKey = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
private string _strIV = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
/// <summary>
/// Conctructor
/// </summary>
public RijndaelCrypto(string key, string IV)
{
_objCryptoService = new RijndaelManaged();
_objCryptoService = new TripleDESCryptoServiceProvider();
_strKey = key;
_strIV = IV;
}
public RijndaelCrypto()
{
_objCryptoService = new RijndaelManaged();
_objCryptoService = new TripleDESCryptoServiceProvider();
}
/// <summary>
/// Get the key
/// </summary>
/// <returns>key</returns>
private byte[] GetLegalKey()
{
string sTemp = _strKey;
_objCryptoService.GenerateKey();
byte[] bytTemp = _objCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// Initialize IV
/// </summary>
/// <returns>Initialize IV</returns>
private byte[] GetLegalIV()
{
_objCryptoService.GenerateIV();
byte[] bytTemp = _objCryptoService.IV;
int IVLength = bytTemp.Length;
if (_strIV.Length > IVLength)
_strIV = _strIV.Substring(0, IVLength);
else if (_strIV.Length < IVLength)
_strIV = _strIV.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(_strIV);
}
/// <summary>
/// Encrypto
/// </summary>
/// <param name="Source">The source string need to encrypto</param>
/// <returns>The string after crypto</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
_objCryptoService.Key = GetLegalKey();
_objCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = _objCryptoService.CreateEncryptor();
using (CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write))
{
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
string strRet = Convert.ToBase64String(bytOut);
cs.Clear();
return strRet;
}
}
/// <summary>
/// Decrypto
/// </summary>
/// <param name="Source">The srouce need to decrypto</param>
/// <returns>The result string after decrypto</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
using (MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length))
{
_objCryptoService.Key = GetLegalKey();
_objCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = _objCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
string strRet = sr.ReadToEnd();
cs.Clear();
return strRet;
}
}
}
}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SurvIT.Business.UserPrivilege
{
/// <summary>
/// Symmtric Crypto
/// </summary>
public class RijndaelCrypto
{
private SymmetricAlgorithm _objCryptoService;
private string _strKey = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
private string _strIV = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
/// <summary>
/// Conctructor
/// </summary>
public RijndaelCrypto(string key, string IV)
{
_objCryptoService = new RijndaelManaged();
_objCryptoService = new TripleDESCryptoServiceProvider();
_strKey = key;
_strIV = IV;
}
public RijndaelCrypto()
{
_objCryptoService = new RijndaelManaged();
_objCryptoService = new TripleDESCryptoServiceProvider();
}
/// <summary>
/// Get the key
/// </summary>
/// <returns>key</returns>
private byte[] GetLegalKey()
{
string sTemp = _strKey;
_objCryptoService.GenerateKey();
byte[] bytTemp = _objCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// Initialize IV
/// </summary>
/// <returns>Initialize IV</returns>
private byte[] GetLegalIV()
{
_objCryptoService.GenerateIV();
byte[] bytTemp = _objCryptoService.IV;
int IVLength = bytTemp.Length;
if (_strIV.Length > IVLength)
_strIV = _strIV.Substring(0, IVLength);
else if (_strIV.Length < IVLength)
_strIV = _strIV.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(_strIV);
}
/// <summary>
/// Encrypto
/// </summary>
/// <param name="Source">The source string need to encrypto</param>
/// <returns>The string after crypto</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
_objCryptoService.Key = GetLegalKey();
_objCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = _objCryptoService.CreateEncryptor();
using (CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write))
{
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
string strRet = Convert.ToBase64String(bytOut);
cs.Clear();
return strRet;
}
}
/// <summary>
/// Decrypto
/// </summary>
/// <param name="Source">The srouce need to decrypto</param>
/// <returns>The result string after decrypto</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
using (MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length))
{
_objCryptoService.Key = GetLegalKey();
_objCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = _objCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
string strRet = sr.ReadToEnd();
cs.Clear();
return strRet;
}
}
}
}
用法如下:
Code
public static string EncryptoPassword(string pwd)
{
RijndaelCrypto rijndael = new RijndaelCrypto();
return rijndael.Encrypto(pwd);
}
public static string DecryptoPassword(string CryptoPwd)
{
RijndaelCrypto rijndael = new RijndaelCrypto();
return rijndael.Decrypto(CryptoPwd);
}
public static string EncryptoPassword(string pwd)
{
RijndaelCrypto rijndael = new RijndaelCrypto();
return rijndael.Encrypto(pwd);
}
public static string DecryptoPassword(string CryptoPwd)
{
RijndaelCrypto rijndael = new RijndaelCrypto();
return rijndael.Decrypto(CryptoPwd);
}