一聚教程网:一个值得你收藏的教程网站

热门教程

发一个加密的方法

时间:2022-06-30 09:18:09 编辑:袖梨 来源:一聚教程网

//声明加密字符生成的随机数的位数
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);

热门栏目