UnicodeEncoding 加密
privatebyte[] _power;
///<summary>
/// 用户权限
///</summary>
publicbyte[] Power
{
set { _power = value; }
get { return _power; }
}
sql:
Power binary 4000
解密:
Encoding.Unicode.GetString(model.Power);
加密:
byte[] Power =new UnicodeEncoding().GetBytes(this.HidPowers.Value);
model.Power = Power;
///<summary>
/// 用户权限
///</summary>
publicbyte[] Power
{
set { _power = value; }
get { return _power; }
}
sql:
Power binary 4000
解密:
Encoding.Unicode.GetString(model.Power);
加密:
byte[] Power =new UnicodeEncoding().GetBytes(this.HidPowers.Value);
model.Power = Power;
代码
using System;
using System.Security.Cryptography;
using System.Text;
namespace BeidouWY.Common.DEncrypt
{
///<summary>
/// Encrypt 的摘要说明。
/// LiTianPing
///</summary>
publicclass DEncrypt
{
///<summary>
/// 构造方法
///</summary>
public DEncrypt()
{
}
#region 使用 缺省密钥字符串 加密/解密string
///<summary>
/// 使用缺省密钥字符串加密string
///</summary>
///<param name="original">明文</param>
///<returns>密文</returns>
publicstaticstring Encrypt(string original)
{
return Encrypt(original,"MATICSOFT");
}
///<summary>
/// 使用缺省密钥字符串解密string
///</summary>
///<param name="original">密文</param>
///<returns>明文</returns>
publicstaticstring Decrypt(string original)
{
return Decrypt(original,"MATICSOFT",System.Text.Encoding.Default);
}
#endregion
#region 使用 给定密钥字符串 加密/解密string
///<summary>
/// 使用给定密钥字符串加密string
///</summary>
///<param name="original">原始文字</param>
///<param name="key">密钥</param>
///<param name="encoding">字符编码方案</param>
///<returns>密文</returns>
publicstaticstring Encrypt(string original, string key)
{
byte[] buff = System.Text.Encoding.Default.GetBytes(original);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return Convert.ToBase64String(Encrypt(buff,kb));
}
///<summary>
/// 使用给定密钥字符串解密string
///</summary>
///<param name="original">密文</param>
///<param name="key">密钥</param>
///<returns>明文</returns>
publicstaticstring Decrypt(string original, string key)
{
return Decrypt(original,key,System.Text.Encoding.Default);
}
///<summary>
/// 使用给定密钥字符串解密string,返回指定编码方式明文
///</summary>
///<param name="encrypted">密文</param>
///<param name="key">密钥</param>
///<param name="encoding">字符编码方案</param>
///<returns>明文</returns>
publicstaticstring Decrypt(string encrypted, string key,Encoding encoding)
{
byte[] buff = Convert.FromBase64String(encrypted);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return encoding.GetString(Decrypt(buff,kb));
}
#endregion
#region 使用 缺省密钥字符串 加密/解密/byte[]
///<summary>
/// 使用缺省密钥字符串解密byte[]
///</summary>
///<param name="encrypted">密文</param>
///<param name="key">密钥</param>
///<returns>明文</returns>
publicstaticbyte[] Decrypt(byte[] encrypted)
{
byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT");
return Decrypt(encrypted,key);
}
///<summary>
/// 使用缺省密钥字符串加密
///</summary>
///<param name="original">原始数据</param>
///<param name="key">密钥</param>
///<returns>密文</returns>
publicstaticbyte[] Encrypt(byte[] original)
{
byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT");
return Encrypt(original,key);
}
#endregion
#region 使用 给定密钥 加密/解密/byte[]
///<summary>
/// 生成MD5摘要
///</summary>
///<param name="original">数据源</param>
///<returns>摘要</returns>
publicstaticbyte[] MakeMD5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 =new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 =null;
return keyhash;
}
///<summary>
/// 使用给定密钥加密
///</summary>
///<param name="original">明文</param>
///<param name="key">密钥</param>
///<returns>密文</returns>
publicstaticbyte[] Encrypt(byte[] original, byte[] key)
{
TripleDESCryptoServiceProvider des =new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
///<summary>
/// 使用给定密钥解密数据
///</summary>
///<param name="encrypted">密文</param>
///<param name="key">密钥</param>
///<returns>明文</returns>
publicstaticbyte[] Decrypt(byte[] encrypted, byte[] key)
{
TripleDESCryptoServiceProvider des =new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
#endregion
}
}
using System.Security.Cryptography;
using System.Text;
namespace BeidouWY.Common.DEncrypt
{
///<summary>
/// Encrypt 的摘要说明。
/// LiTianPing
///</summary>
publicclass DEncrypt
{
///<summary>
/// 构造方法
///</summary>
public DEncrypt()
{
}
#region 使用 缺省密钥字符串 加密/解密string
///<summary>
/// 使用缺省密钥字符串加密string
///</summary>
///<param name="original">明文</param>
///<returns>密文</returns>
publicstaticstring Encrypt(string original)
{
return Encrypt(original,"MATICSOFT");
}
///<summary>
/// 使用缺省密钥字符串解密string
///</summary>
///<param name="original">密文</param>
///<returns>明文</returns>
publicstaticstring Decrypt(string original)
{
return Decrypt(original,"MATICSOFT",System.Text.Encoding.Default);
}
#endregion
#region 使用 给定密钥字符串 加密/解密string
///<summary>
/// 使用给定密钥字符串加密string
///</summary>
///<param name="original">原始文字</param>
///<param name="key">密钥</param>
///<param name="encoding">字符编码方案</param>
///<returns>密文</returns>
publicstaticstring Encrypt(string original, string key)
{
byte[] buff = System.Text.Encoding.Default.GetBytes(original);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return Convert.ToBase64String(Encrypt(buff,kb));
}
///<summary>
/// 使用给定密钥字符串解密string
///</summary>
///<param name="original">密文</param>
///<param name="key">密钥</param>
///<returns>明文</returns>
publicstaticstring Decrypt(string original, string key)
{
return Decrypt(original,key,System.Text.Encoding.Default);
}
///<summary>
/// 使用给定密钥字符串解密string,返回指定编码方式明文
///</summary>
///<param name="encrypted">密文</param>
///<param name="key">密钥</param>
///<param name="encoding">字符编码方案</param>
///<returns>明文</returns>
publicstaticstring Decrypt(string encrypted, string key,Encoding encoding)
{
byte[] buff = Convert.FromBase64String(encrypted);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return encoding.GetString(Decrypt(buff,kb));
}
#endregion
#region 使用 缺省密钥字符串 加密/解密/byte[]
///<summary>
/// 使用缺省密钥字符串解密byte[]
///</summary>
///<param name="encrypted">密文</param>
///<param name="key">密钥</param>
///<returns>明文</returns>
publicstaticbyte[] Decrypt(byte[] encrypted)
{
byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT");
return Decrypt(encrypted,key);
}
///<summary>
/// 使用缺省密钥字符串加密
///</summary>
///<param name="original">原始数据</param>
///<param name="key">密钥</param>
///<returns>密文</returns>
publicstaticbyte[] Encrypt(byte[] original)
{
byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT");
return Encrypt(original,key);
}
#endregion
#region 使用 给定密钥 加密/解密/byte[]
///<summary>
/// 生成MD5摘要
///</summary>
///<param name="original">数据源</param>
///<returns>摘要</returns>
publicstaticbyte[] MakeMD5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 =new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 =null;
return keyhash;
}
///<summary>
/// 使用给定密钥加密
///</summary>
///<param name="original">明文</param>
///<param name="key">密钥</param>
///<returns>密文</returns>
publicstaticbyte[] Encrypt(byte[] original, byte[] key)
{
TripleDESCryptoServiceProvider des =new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
///<summary>
/// 使用给定密钥解密数据
///</summary>
///<param name="encrypted">密文</param>
///<param name="key">密钥</param>
///<returns>明文</returns>
publicstaticbyte[] Decrypt(byte[] encrypted, byte[] key)
{
TripleDESCryptoServiceProvider des =new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
#endregion
}
}
代码
using System;
using System.Security.Cryptography;
using System.Text;
namespace BeidouWY.Common.DEncrypt
{
///<summary>
/// DES加密/解密类。
/// LiTianPing
///</summary>
publicclass DESEncrypt
{
public DESEncrypt()
{
}
#region ========加密========
///<summary>
/// 加密
///</summary>
///<param name="Text"></param>
///<returns></returns>
publicstaticstring Encrypt(string Text)
{
return Encrypt(Text,"MATICSOFT");
}
///<summary>
/// 加密数据
///</summary>
///<param name="Text"></param>
///<param name="sKey"></param>
///<returns></returns>
publicstaticstring Encrypt(string Text,string sKey)
{
DESCryptoServiceProvider des =new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret=new StringBuilder();
foreach( byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}",b);
}
return ret.ToString();
}
#endregion
#region ========解密========
///<summary>
/// 解密
///</summary>
///<param name="Text"></param>
///<returns></returns>
publicstaticstring Decrypt(string Text)
{
return Decrypt(Text,"MATICSOFT");
}
///<summary>
/// 解密数据
///</summary>
///<param name="Text"></param>
///<param name="sKey"></param>
///<returns></returns>
publicstaticstring Decrypt(string Text,string sKey)
{
DESCryptoServiceProvider des =new DESCryptoServiceProvider();
int len;
len=Text.Length/2;
byte[] inputByteArray =newbyte[len];
int x,i;
for(x=0;x<len;x++)
{
i = Convert.ToInt32(Text.Substring(x *2, 2), 16);
inputByteArray[x]=(byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
#endregion
}
}
using System.Security.Cryptography;
using System.Text;
namespace BeidouWY.Common.DEncrypt
{
///<summary>
/// DES加密/解密类。
/// LiTianPing
///</summary>
publicclass DESEncrypt
{
public DESEncrypt()
{
}
#region ========加密========
///<summary>
/// 加密
///</summary>
///<param name="Text"></param>
///<returns></returns>
publicstaticstring Encrypt(string Text)
{
return Encrypt(Text,"MATICSOFT");
}
///<summary>
/// 加密数据
///</summary>
///<param name="Text"></param>
///<param name="sKey"></param>
///<returns></returns>
publicstaticstring Encrypt(string Text,string sKey)
{
DESCryptoServiceProvider des =new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret=new StringBuilder();
foreach( byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}",b);
}
return ret.ToString();
}
#endregion
#region ========解密========
///<summary>
/// 解密
///</summary>
///<param name="Text"></param>
///<returns></returns>
publicstaticstring Decrypt(string Text)
{
return Decrypt(Text,"MATICSOFT");
}
///<summary>
/// 解密数据
///</summary>
///<param name="Text"></param>
///<param name="sKey"></param>
///<returns></returns>
publicstaticstring Decrypt(string Text,string sKey)
{
DESCryptoServiceProvider des =new DESCryptoServiceProvider();
int len;
len=Text.Length/2;
byte[] inputByteArray =newbyte[len];
int x,i;
for(x=0;x<len;x++)
{
i = Convert.ToInt32(Text.Substring(x *2, 2), 16);
inputByteArray[x]=(byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
#endregion
}
}
代码
using System;
using System.Text;
using System.Security.Cryptography;
namespace BeidouWY.Common.DEncrypt
{
///<summary>
/// 得到随机安全码(哈希加密)。
///</summary>
publicclass HashEncode
{
public HashEncode()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///<summary>
/// 得到随机哈希加密字符串
///</summary>
///<returns></returns>
publicstaticstring GetSecurity()
{
string Security = HashEncoding(GetRandomValue());
return Security;
}
///<summary>
/// 得到一个随机数值
///</summary>
///<returns></returns>
publicstaticstring GetRandomValue()
{
Random Seed =new Random();
string RandomVaule = Seed.Next(1, int.MaxValue).ToString();
return RandomVaule;
}
///<summary>
/// 哈希加密一个字符串
///</summary>
///<param name="Security"></param>
///<returns></returns>
publicstaticstring HashEncoding(string Security)
{
byte[] Value;
UnicodeEncoding Code =new UnicodeEncoding();
byte[] Message = Code.GetBytes(Security);
SHA512Managed Arithmetic =new SHA512Managed();
Value = Arithmetic.ComputeHash(Message);
Security ="";
foreach(byte o in Value)
{
Security += (int) o +"O";
}
return Security;
}
}
}
using System.Text;
using System.Security.Cryptography;
namespace BeidouWY.Common.DEncrypt
{
///<summary>
/// 得到随机安全码(哈希加密)。
///</summary>
publicclass HashEncode
{
public HashEncode()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///<summary>
/// 得到随机哈希加密字符串
///</summary>
///<returns></returns>
publicstaticstring GetSecurity()
{
string Security = HashEncoding(GetRandomValue());
return Security;
}
///<summary>
/// 得到一个随机数值
///</summary>
///<returns></returns>
publicstaticstring GetRandomValue()
{
Random Seed =new Random();
string RandomVaule = Seed.Next(1, int.MaxValue).ToString();
return RandomVaule;
}
///<summary>
/// 哈希加密一个字符串
///</summary>
///<param name="Security"></param>
///<returns></returns>
publicstaticstring HashEncoding(string Security)
{
byte[] Value;
UnicodeEncoding Code =new UnicodeEncoding();
byte[] Message = Code.GetBytes(Security);
SHA512Managed Arithmetic =new SHA512Managed();
Value = Arithmetic.ComputeHash(Message);
Security ="";
foreach(byte o in Value)
{
Security += (int) o +"O";
}
return Security;
}
}
}
代码
using System;
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace BeidouWY.Common.DEncrypt
{
///<summary>
/// MD5Helper 的摘要说明。
///</summary>
publicclass MD5Helper
{
///<summary>
/// 得到一个加密的字符串
///</summary>
///<param name="strIn">原始字符串</param>
///<returns>加密后字符串</returns>
publicstaticstring GetMD5String(string strIn)
{
byte[] b=Encoding.Default.GetBytes(strIn);
b=new MD5CryptoServiceProvider().ComputeHash(b);
string strOut="";
for(int i=0;i<b.Length;i++)
{
strOut+=b[i].ToString("x").PadLeft(2,'2');
}
return strOut;
}
}
}
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace BeidouWY.Common.DEncrypt
{
///<summary>
/// MD5Helper 的摘要说明。
///</summary>
publicclass MD5Helper
{
///<summary>
/// 得到一个加密的字符串
///</summary>
///<param name="strIn">原始字符串</param>
///<returns>加密后字符串</returns>
publicstaticstring GetMD5String(string strIn)
{
byte[] b=Encoding.Default.GetBytes(strIn);
b=new MD5CryptoServiceProvider().ComputeHash(b);
string strOut="";
for(int i=0;i<b.Length;i++)
{
strOut+=b[i].ToString("x").PadLeft(2,'2');
}
return strOut;
}
}
}
代码