诸位大拿帮我看下这个加密算法

oWangPangZi 2012-07-31 02:43:49
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace BLL
{
public class stringBLL
{
//对密码进行加密时附加的后缀字符
private string PassExt = "TY";

/// <summary>
/// 过滤SQL注入字符
/// </summary>
/// <param name="Str">要过滤的字符串</param>
/// <returns></returns>
#region 过滤SQL注入字符
public string SqlEncode(string Str)
{
return SqlEncode(Str, 0);
}
/// <param name="Mode">过滤模式:0全部过滤,1过滤英文,2过滤特殊字符</param>
public string SqlEncode(string Str, int Mode)
{
if (string.IsNullOrEmpty(Str))
return "";

string SqlStr = "";
string SqlStr1 = "select|update|delete|insert|drop";
string SqlStr2 = "'|%|#|<|>|*";
switch (Mode)
{
case 0:
SqlStr = SqlStr1 + "|" + SqlStr2;
break;
case 1:
SqlStr = SqlStr1;
break;
case 2:
SqlStr = SqlStr2;
break;
}
string[] SqlSplit = SqlStr.Split('|');
for (int i = 0; i < SqlSplit.Length; i++)
{
Str = Str.Replace(SqlSplit[i], "");
}
return Str;
}
#endregion

/// <summary>
/// 验证字符是否为数字
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public bool CheckNum(string Str)
{
if (string.IsNullOrEmpty(Str))
{
return false;
}

bool Check = false;
try
{
int a = int.Parse(Str);
Check = true;
}
catch
{ }
return Check;
}
public void Show(System.Web.UI.Page page, string msgInfo)
{
page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(),
"<script>alert('" + msgInfo + "')</script>");
}

/// <summary>
/// 获取字符串左侧字数,按字节数计算
/// </summary>
/// <param name="Str">要截取的字符</param>
/// <param name="Num">要截取的长度</param>
/// <returns></returns>
#region 获取字符串左侧字数,按字节数计算
public string GetLeftStr(string Str, int Len)
{
return GetLeftStr(Str, Len, "");
}
public string GetLeftStr(string Str, int Len, string Ext)
{
string temp = string.Empty;
if (System.Text.Encoding.Default.GetByteCount(Str) <= Len)//如果长度比需要的长度n小,返回原字符串
{
return Str;
}

int t = 0;
char[] q = Str.ToCharArray();
for (int i = 0; i < q.Length && t < Len; i++)
{
if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5) //是否汉字
{
temp += q[i];
t += 2;
}
else
{
temp += q[i];
t++;
}
}
return (temp + Ext);

}
#endregion

/// <summary>
/// 对信息进行html编码处理,防止攻击
/// </summary>
/// <param name="Str">要处理的文本信息</param>
/// <param name="IsAdmin">是否为管理员要进行的操作,如果为管理员操作,对某些特定内容不进行替换</param>
/// <returns></returns>
public string HtmlEncode(string Str,bool IsAdmin)
{
if (string.IsNullOrEmpty(Str))
{
return "";
}
Str = Str.Replace("'", "'");
//Str = Str.Replace("\", """);
if (IsAdmin == false)
{
Str = System.Text.RegularExpressions.Regex.Replace(Str, @"<script[^>]*?>.*?</script>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Str = System.Text.RegularExpressions.Regex.Replace(Str, @"<iframe[^>]*?>.*?</iframe>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
return Str;
}

/// <summary>
/// 前台用户提交时进行html编码处理,防止攻击
/// 处理过程使用正则表达式对script等脚本进行处理
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public string UserHtmlEncode(string Str)
{
return HtmlEncode(Str, false);
}

/// <summary>
/// 对编码过的文本信息进行解码处理,用于编辑
/// </summary>
/// <param name="Str">要解码的文本信息</param>
/// <returns></returns>
public string HtmlDecode(string Str)
{
Str = Str.Replace("'", "'");
Str = Str.Replace("\"", """);
Str = Str.Replace("&", "&");
Str = Str.Replace("<", "<");
Str = Str.Replace(">", ">");
return "";
}

/// <summary>
/// 对密码进行MD5加密
/// </summary>
/// <param name="Str">明文</param>
/// <returns>添加密码后缀字符串后的密文</returns>
public string MD5(string Str)
{
return MD5(Str, false);
}
/// <param name="Old">是否判断旧密码,旧密码判断不加入密码后缀字符串</param>
public string MD5(string Str,bool Old)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] bytes = new byte[16];
System.Text.ASCIIEncoding asc = new System.Text.ASCIIEncoding();
if (Old == true)
{
bytes = md5.ComputeHash(asc.GetBytes(Str));
}
else
{
bytes = md5.ComputeHash(asc.GetBytes(Str + PassExt));
}
return Convert.ToBase64String(bytes);
}

/// <summary>
/// 使用SHA1算法求加密
/// </summary>
/// <param name="text">明文</param>
/// <returns>添加密码后缀字符串后的密文</returns>
public string SHA1(string Str)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Str + PassExt, "SHA1");
}

/// <summary>
/// 返回脚本类型提示信息,补全javascript代码
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public void ShowMsg(string Msg)
{
ShowMsg(Msg, "",false);
}
public void ShowMsg(string Msg, bool Back)
{
ShowMsg(Msg, "", Back);
}
public void ShowMsg(string Msg, string Url)
{
ShowMsg(Msg, Url, false);
}
public void ShowMsg(string Msg, string Url, bool Back)
{
HttpContext.Current.Response.Write(string.Format("<script type=\"text/javascript\">alert('{0}');", Msg));
if (Url.Length > 0)
{
HttpContext.Current.Response.Write(string.Format("window.location='{0}';", Url));
}
else
{
if (Back == true)
{
HttpContext.Current.Response.Write("history.back();");
}
}
HttpContext.Current.Response.Write("</script>");
}

public void ShowError(string Msg)
{
ShowError(Msg, "");
}
public void ShowError(string Msg,string Url)
{
HttpContext.Current.Response.Write(string.Format("<script type=\"text/javascript\">alert('{0}');", Msg));
if (Url.Length > 0)
{
HttpContext.Current.Response.Write("window.location='" + Url + "';");
}
else
{
HttpContext.Current.Response.Write("history.back();");
}
HttpContext.Current.Response.Write("</script>");
}
}
}


我只懂得HTML,这个看不懂
最后出来的密码是sWm7vu4iJUdazAsD5PRaLw==这样的BASE64
具体流程是怎样的呢,我最后没看到有BASE64的计算啊
...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
goright12 2012-07-31
  • 打赏
  • 举报
回复
先找点加密解密有东东看下吧
孟子E章 2012-07-31
  • 打赏
  • 举报
回复
先md5.ComputeHash,然后 Convert.ToBase64String(bytes)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧