baset64加密解密代码
Code
using System;
using System.Security.Cryptography;
using System.Web.Security;
using System.IO;
using System.Text;
/// <summary>
/// CryptTo 实现加密、解密算法
/// </summary>
public class CryptTo
{
/// <summary>
/// 根据任意字符生成Des密钥
/// </summary>
/// <param name="secret">生成密钥的种子字</param>
/// <returns>返回合格的Des密钥</returns>
public static byte[] CreateKey(string secret)
{
if ((secret == null) || (secret.Length == 0))
throw new ArgumentException("Secret is not valid");
byte[] temp;
temp = Hash(new ASCIIEncoding().GetBytes(secret));
byte[] ret = new byte[24];
int i;
if (temp.Length < 24)
{
System.Array.Copy(temp, 0, ret, 0, temp.Length);
for (i = temp.Length; i < 24; i++)
ret[i] = 0;
}
else
System.Array.Copy(temp, 0, ret, 0, 24);
return ret;
}
/// <summary>
/// 根据任意字符生成Des密钥
/// </summary>
/// <param name="secret">生成密钥的种子字</param>
/// <returns>返回合格的Des密钥</returns>
public static string GenerateKey(string secret)
{
return Convert.ToBase64String(CreateKey(secret));
}
/// <summary>
/// 随机生成Des密钥
/// </summary>
/// <returns>返回合格的Des密钥</returns>
public static string GenerateKey()
{
string ret;
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
TDES.GenerateKey();
ret = Convert.ToBase64String(TDES.Key);
return (ret);
}
/// <summary>
/// 使用TripledDES加密
/// </summary>
/// <param name="content">要加密的内容</param>
/// <param name="secret">密钥</param>
/// <returns>加密后的结果</returns>
public static string Encrypt(string content, string secret)
{
if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))
throw new ArgumentNullException("Invalid Argument");
try
{
byte[] Key = Convert.FromBase64String(secret);
byte[] ContentByte = Encoding.Unicode.GetBytes(content);
byte[] ContentCryptByte = Encrypt(ContentByte, Key);
return Convert.ToBase64String(ContentCryptByte);
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 使用TripledDES解密
/// </summary>
/// <param name="content">要解密的内容</param>
/// <param name="secret">密钥</param>
/// <returns>解密后的结果</returns>
public static string Decrypt(string content, string secret)
{
if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))
throw new ArgumentNullException("Invalid Argument");
try
{
byte[] Key = Convert.FromBase64String(secret);
byte[] CryByte = Convert.FromBase64String(content);
byte[] DecByte = Decrypt(CryByte, Key);
return Encoding.Unicode.GetString(DecByte);
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 使用TripledDES加密
/// </summary>
/// <param name="source">要加密的内容</param>
/// <param name="key">密钥</param>
/// <returns>加密后的结果</returns>
public static byte[] Encrypt(byte[] source, byte[] key)
{
if ((source.Length == 0) || (source == null) || (key == null) || (key.Length == 0))
{
throw new ArgumentException("Invalid Argument");
}
TripleDESCryptoServiceProvider dsp = new TripleDESCryptoServiceProvider();
dsp.Mode = CipherMode.ECB;
ICryptoTransform des = dsp.CreateEncryptor(key, null);
try
{
return des.TransformFinalBlock(source, 0, source.Length);
}
catch (Exception e1)
{
throw e1;
}
}
/// <summary>
/// 使用TripledDES解密
/// </summary>
/// <param name="source">要解密的内容</param>
/// <param name="key">密钥</param>
/// <returns>解密后的结果</returns>
public static byte[] Decrypt(byte[] source, byte[] key)
{
if ((source.Length == 0) || (source == null) || (key == null) || (key.Length == 0))
{
throw new ArgumentNullException("Invalid Argument");
}
TripleDESCryptoServiceProvider dsp = new TripleDESCryptoServiceProvider();
dsp.Mode = CipherMode.ECB;
ICryptoTransform des = dsp.CreateDecryptor(key, null);
try
{
byte[] ret = new byte[source.Length + 8];
des.TransformBlock(source, 0, source.Length, ret, 0);
ret = des.TransformFinalBlock(source, 0, source.Length);
ret = des.TransformFinalBlock(source, 0, source.Length);
return ret;
}
catch (Exception e1)
{
throw e1;
}
}
/// <summary>
/// 使用md5计算散列
/// </summary>
/// <param name="source">要加密的内容</param>
/// <returns>加密后的结果</returns>
public static byte[] Hash(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");
MD5 m = MD5.Create();
return m.ComputeHash(source);
}
/// <summary>
/// 使用md5计算散列
/// </summary>
/// <param name="source">要加密的内容</param>
/// <returns>加密后的结果</returns>
public static string Hash(string source)
{
// 两者的执行相同
// return BitConverter.ToString(Hash(Encoding.ASCII.GetBytes(source)));
return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "md5");
}
/// <summary>
/// 原始base64编码
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static byte[] Base64Encode(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");
ToBase64Transform tb64 = new ToBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;
while (pos + 3 < source.Length)
{
buff = tb64.TransformFinalBlock(source, pos, 3);
stm.Write(buff, 0, buff.Length);
pos += 3;
}
buff = tb64.TransformFinalBlock(source, pos, source.Length - pos);
stm.Write(buff, 0, buff.Length);
return stm.ToArray();
}
/// <summary>
/// 原始base64解码
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static byte[] Base64Decode(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");
FromBase64Transform fb64 = new FromBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;
while (pos + 4 < source.Length)
{
buff = fb64.TransformFinalBlock(source, pos, 4);
stm.Write(buff, 0, buff.Length);
pos += 4;
}
buff = fb64.TransformFinalBlock(source, pos, source.Length - pos);
stm.Write(buff, 0, buff.Length);
return stm.ToArray();
}
public static string ToBase64(string source)
{
return Convert.ToBase64String(Encoding.Unicode.GetBytes(source));
}
public static string FromBase64(string source)
{
return Encoding.Unicode.GetString(Convert.FromBase64String(source));
}
}
using System;
using System.Security.Cryptography;
using System.Web.Security;
using System.IO;
using System.Text;
/// <summary>
/// CryptTo 实现加密、解密算法
/// </summary>
public class CryptTo
{
/// <summary>
/// 根据任意字符生成Des密钥
/// </summary>
/// <param name="secret">生成密钥的种子字</param>
/// <returns>返回合格的Des密钥</returns>
public static byte[] CreateKey(string secret)
{
if ((secret == null) || (secret.Length == 0))
throw new ArgumentException("Secret is not valid");
byte[] temp;
temp = Hash(new ASCIIEncoding().GetBytes(secret));
byte[] ret = new byte[24];
int i;
if (temp.Length < 24)
{
System.Array.Copy(temp, 0, ret, 0, temp.Length);
for (i = temp.Length; i < 24; i++)
ret[i] = 0;
}
else
System.Array.Copy(temp, 0, ret, 0, 24);
return ret;
}
/// <summary>
/// 根据任意字符生成Des密钥
/// </summary>
/// <param name="secret">生成密钥的种子字</param>
/// <returns>返回合格的Des密钥</returns>
public static string GenerateKey(string secret)
{
return Convert.ToBase64String(CreateKey(secret));
}
/// <summary>
/// 随机生成Des密钥
/// </summary>
/// <returns>返回合格的Des密钥</returns>
public static string GenerateKey()
{
string ret;
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
TDES.GenerateKey();
ret = Convert.ToBase64String(TDES.Key);
return (ret);
}
/// <summary>
/// 使用TripledDES加密
/// </summary>
/// <param name="content">要加密的内容</param>
/// <param name="secret">密钥</param>
/// <returns>加密后的结果</returns>
public static string Encrypt(string content, string secret)
{
if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))
throw new ArgumentNullException("Invalid Argument");
try
{
byte[] Key = Convert.FromBase64String(secret);
byte[] ContentByte = Encoding.Unicode.GetBytes(content);
byte[] ContentCryptByte = Encrypt(ContentByte, Key);
return Convert.ToBase64String(ContentCryptByte);
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 使用TripledDES解密
/// </summary>
/// <param name="content">要解密的内容</param>
/// <param name="secret">密钥</param>
/// <returns>解密后的结果</returns>
public static string Decrypt(string content, string secret)
{
if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))
throw new ArgumentNullException("Invalid Argument");
try
{
byte[] Key = Convert.FromBase64String(secret);
byte[] CryByte = Convert.FromBase64String(content);
byte[] DecByte = Decrypt(CryByte, Key);
return Encoding.Unicode.GetString(DecByte);
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 使用TripledDES加密
/// </summary>
/// <param name="source">要加密的内容</param>
/// <param name="key">密钥</param>
/// <returns>加密后的结果</returns>
public static byte[] Encrypt(byte[] source, byte[] key)
{
if ((source.Length == 0) || (source == null) || (key == null) || (key.Length == 0))
{
throw new ArgumentException("Invalid Argument");
}
TripleDESCryptoServiceProvider dsp = new TripleDESCryptoServiceProvider();
dsp.Mode = CipherMode.ECB;
ICryptoTransform des = dsp.CreateEncryptor(key, null);
try
{
return des.TransformFinalBlock(source, 0, source.Length);
}
catch (Exception e1)
{
throw e1;
}
}
/// <summary>
/// 使用TripledDES解密
/// </summary>
/// <param name="source">要解密的内容</param>
/// <param name="key">密钥</param>
/// <returns>解密后的结果</returns>
public static byte[] Decrypt(byte[] source, byte[] key)
{
if ((source.Length == 0) || (source == null) || (key == null) || (key.Length == 0))
{
throw new ArgumentNullException("Invalid Argument");
}
TripleDESCryptoServiceProvider dsp = new TripleDESCryptoServiceProvider();
dsp.Mode = CipherMode.ECB;
ICryptoTransform des = dsp.CreateDecryptor(key, null);
try
{
byte[] ret = new byte[source.Length + 8];
des.TransformBlock(source, 0, source.Length, ret, 0);
ret = des.TransformFinalBlock(source, 0, source.Length);
ret = des.TransformFinalBlock(source, 0, source.Length);
return ret;
}
catch (Exception e1)
{
throw e1;
}
}
/// <summary>
/// 使用md5计算散列
/// </summary>
/// <param name="source">要加密的内容</param>
/// <returns>加密后的结果</returns>
public static byte[] Hash(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");
MD5 m = MD5.Create();
return m.ComputeHash(source);
}
/// <summary>
/// 使用md5计算散列
/// </summary>
/// <param name="source">要加密的内容</param>
/// <returns>加密后的结果</returns>
public static string Hash(string source)
{
// 两者的执行相同
// return BitConverter.ToString(Hash(Encoding.ASCII.GetBytes(source)));
return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "md5");
}
/// <summary>
/// 原始base64编码
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static byte[] Base64Encode(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");
ToBase64Transform tb64 = new ToBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;
while (pos + 3 < source.Length)
{
buff = tb64.TransformFinalBlock(source, pos, 3);
stm.Write(buff, 0, buff.Length);
pos += 3;
}
buff = tb64.TransformFinalBlock(source, pos, source.Length - pos);
stm.Write(buff, 0, buff.Length);
return stm.ToArray();
}
/// <summary>
/// 原始base64解码
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static byte[] Base64Decode(byte[] source)
{
if ((source == null) || (source.Length == 0))
throw new ArgumentException("source is not valid");
FromBase64Transform fb64 = new FromBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;
while (pos + 4 < source.Length)
{
buff = fb64.TransformFinalBlock(source, pos, 4);
stm.Write(buff, 0, buff.Length);
pos += 4;
}
buff = fb64.TransformFinalBlock(source, pos, source.Length - pos);
stm.Write(buff, 0, buff.Length);
return stm.ToArray();
}
public static string ToBase64(string source)
{
return Convert.ToBase64String(Encoding.Unicode.GetBytes(source));
}
public static string FromBase64(string source)
{
return Encoding.Unicode.GetString(Convert.FromBase64String(source));
}
}