·您的位置: 首页 » 资源教程 » 编程开发 » ASP » 发一个加密的方法

发一个加密的方法

类别: ASP教程  评论数:0 总得分:0
//声明加密字符生成的随机数的位数
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




-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1