当前位置: 代码迷 >> ASP.NET >> 有人可以帮小弟我改个asp涵数吗
  详细解决方案

有人可以帮小弟我改个asp涵数吗

热度:194   发布时间:2013-02-25 00:00:00.0
有人可以帮我改个asp涵数吗
Function passwordEncrypt(origPassword)
Dim Stemp, I, L, X, Y
  Randomize( hour(time()) * minute(time()) * second(time()) )
  Stemp = Trim(origPassword)
  L = Len(Stemp)
  If L > 15 Or L = 0 Then
  passwordEncrypt = Stemp
  Else
  passwordEncrypt = Chr(100 + L)
  For I = 2 To (16 - L)
  passwordEncrypt = passwordEncrypt & Chr(Fix(24 * Rnd(1)) + 98)
  Next
  For I = (16 - L + 1) To 16
  X = Asc(Mid(Stemp, (I + L - 16), 1))
  If X >= 48 And X <= 57 Or X >= 113 And X <= 122 Then
  Y = 170 - X
  ElseIf X >= 65 And X <= 80 Or X >= 97 And X <= 112 Then
  Y = 177 - X
  Else
  Y = 171 - X
  End If
  passwordEncrypt = passwordEncrypt & Chr(Y)
  Next
  End If
End Function

我是asp.net C#的,有人可以帮我把上面那个涵数改成C#吗?谢谢了

------解决方案--------------------------------------------------------
我是用下列代码测试的,如果输入16位以内的字母和数字进行加密和解密是正确的.
C# code
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    static public string PasswordEncrypt(string origPassword)    {        string stemp;        int i;        int l;        int x;        int y;        Random rand = new Random(DateTime.Now.Hour * DateTime.Now.Minute * DateTime.Now.Second);        stemp = origPassword.Trim();        l = stemp.Length;        if (l > 15 || l == 0)        {            return stemp;        }        else        {            string returnV = null;            returnV = Convert.ToChar(l + 100).ToString();            for (i = 2; i <= 16 - l; i++)            {                returnV += Convert.ToChar(Convert.ToInt32(Math.Truncate((double)(24 * rand.NextDouble()))) + 98);            }            for (i = 17 - l; i <= 16; i++)            {                //注意Substring的索引是从0开始,而asp是从1开始                x = Convert.ToInt32(Convert.ToChar(stemp.Substring(i + l - 17, 1)));                if ((x >= 48 && x <= 57) || (x >= 113 && x <= 122))                {                    y = 170 - x;                }                else if ((x >= 65 && x <= 80) || (x >= 97 && x <= 112))                {                    y = 177 - x;                }                else                {                    y = 171 - x;                }                returnV += Convert.ToChar(y);            }            return returnV;        }    }    static public string passwordDeEncrypt(string XPassword)    {        string passwordDeEncrypt = "";        int L = XPassword.Length;        int X, Y;        if (L != 16)        {            return XPassword;        }        else        {            L = (int)XPassword[0] - 100;            for (int I = (16 - L + 1); I < 17; I++)            {                X = (int)XPassword[I - 1];                if ((X >= 48 && X <= 57) || (X >= 113 && X <= 122))                {                    Y = 170 - X;                }                else if ((X >= 65 && X <= 80) || (X >= 97 && X <= 112))                {                    Y = 177 - X;                }                else                {                    Y = 171 - X;                }                passwordDeEncrypt = passwordDeEncrypt + ((char)Y).ToString();            }        }        return passwordDeEncrypt;    }    protected void Button1_Click(object sender, EventArgs e)    {        Response.Write("Encrypt: " + PasswordEncrypt(TextBox1.Text) + "<br />");        Response.Write("DeEncrypt: " + passwordDeEncrypt(PasswordEncrypt(TextBox1.Text)));    }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>无标题页</title></head><body>    <form id="form1" runat="server">    <div>        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>    </div>    </form></body></html>
  相关解决方案