加密
//声明加密字符生成的随机数的位数
public const int LENGTH = 32;
//声明加密字符前后的分隔长度
public const int LEN = 4;
///方法作用:根据参数对其加密
///参数:
/// string strEncrypt;欲加密的字符
///返回结果:加密后的字符
public static byte[] SourceEncrypt(string strEncrypt)
{
string source = Encrypt(strEncrypt);
//first
string first = Encrypt(source.Substring(0,LEN));
//second
string second = Encrypt(source.Substring(LEN-1,source.Length-LEN));
//third
string third = Encrypt(source.Substring(source.Length-LEN,LEN));
//four
byte[] Random = new byte[LENGTH];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(Random);
string four = Encrypt(System.Text.Encoding.ASCII.GetString(Random));
string all = first+second+third+four;
return System.Text.Encoding.ASCII.GetBytes(all);
} // end SourceEncrypt
///方法作用:对参数进行加密
///参数:
/// string strEncrypt:欲加密的字符
/// 返回结果:加密后的字符
public static string Encrypt(string strEncrypt)
{
SHA256Managed sha = new SHA256Managed();
byte[] VALUE = sha.ComputeHash(System.Text.Encoding.ASCII.GetBytes(strEncrypt));
return System.Text.Encoding.ASCII.GetString(VALUE);
} // end Encrypt
///方法作用:对参数进行比较
///参数:
/// string strSource:原字符
/// string strObject:目标字符
/// 返回结果:bool是否相等
public static bool CompareEncrypt(byte[] strSource,string strobject)
{
bool result = true;
byte[] strObject = SourceEncrypt(strobject);
//strObject = SourceEncrypt(strObject);
if((strSource==null) || (strObject==null) || (strSource.Length != strObject.Length))
result = false;
else
{
//strSource = strSource.Substring(0,strSource.Length-LENGTH);
//strObject = strObject.Substring(0,strObject.Length-LENGTH);
//byte[] Source = System.Text.Encoding.ASCII.GetBytes(strSource);
//byte[] Obj = System.Text.Encoding.ASCII.GetBytes(strObject);
byte[] Source = strSource;
byte[] Obj = strObject;
if(Source.Length != Obj.Length)
result = false;
else
{
for(int i = 0; i < Source.Length-LENGTH; i++)
{
if(Source[i] != Obj[i])
{
result = false;
break;
}
}
}
}
return result;
} //end CompareyEncrypt