Asp.net---可逆加解密函数

发布时间:2014-10-25 2:22:54
来源:分享查询网

  注意:这里,还是用的种子辅助加解密,以免大家都用的话,岂不是透明了,也就失去其加密的效果了。 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->    public static class Util    {        public static string m_strKey = "Microsoft";   // 加密解密种子,这个很重要哦        public static string Encode(string strKey, string strSource)        {            int iKeyLen = 0, iKeyPos = 0, iOffset = 0, iSrcPos = 0, iSrcAsc = 0, iRange = 0;            string strDest = "";    //加密后的字符串            char cTmp;            iKeyLen = strKey.Trim().Length;            if (iKeyLen == 0)                strKey = m_strKey;    //如果参数为空值把key设置成Infoshare,即默认的种子            iKeyPos = 0;            iRange = 255;            Random rdObj = new Random();            iOffset = rdObj.Next(0, iRange);    //将0-256范围内取随机数            strDest = iOffset.ToString("X2");   //将随机数转换成16进制数            for (iSrcPos = 0; iSrcPos < strSource.Trim().Length; iSrcPos++)            {                cTmp = strSource[iSrcPos];                iSrcAsc = ((short)cTmp + iOffset) % 255;                if (iKeyPos < iKeyLen)                    iKeyPos = iKeyPos + 1;                else                    iKeyPos = 1;                cTmp = strKey[iSrcPos];                iSrcAsc = iSrcAsc ^ (short)cTmp;                strDest = strDest + iSrcAsc.ToString("X2");                iOffset = iSrcAsc;            }            return strDest;        }        public static string Decode(string strKey, string strSource)        {            int iKeyLen, iKeyPos, iOffset, iSrcPos, iSrcAsc, iTmpSrcAsc;            string strDest = "";            iKeyLen = strKey.Trim().Length;            if (iKeyLen == 0)            {                strKey = m_strKey;                iKeyLen = strKey.Length;            }            iKeyPos = 0;            iOffset = Int32.Parse(strSource.Trim().Substring(0, 2), System.Globalization.NumberStyles.HexNumber);            iSrcPos = 2;            do            {                iSrcAsc = Int32.Parse(strSource.Trim().Substring(iSrcPos, 2), System.Globalization.NumberStyles.HexNumber);                if (iKeyPos < iKeyLen)                    iKeyPos = iKeyPos + 1;                else                    iKeyPos = 1;                iTmpSrcAsc = iSrcAsc ^ (short)strKey[iKeyPos - 1];                if (iTmpSrcAsc <= iOffset)                    iTmpSrcAsc = 255 + iTmpSrcAsc - iOffset;                else                    iTmpSrcAsc = iTmpSrcAsc - iOffset;                strDest = strDest + (char)iTmpSrcAsc;                iOffset = iSrcAsc;                iSrcPos = iSrcPos + 2;            }            while (iSrcPos < strSource.Length);            return strDest;        }    }   简单使用一下: ASPX: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><div>        <asp:TextBox ID="txtNumber1" runat="server" />         <br />        <br />        <asp:Button ID="btnEncode" Text="Encode" runat="server"             onclick="btnEncode_Click" /><asp:Label ID="lblEnCode" runat="server" />        <br />        <br />        <asp:TextBox ID="txtNumber2" runat="server" />        <br />        <br />        <asp:Button ID="btnDecode" Text="Decode" runat="server"             onclick="btnDecode_Click" /><asp:Label ID="lblDecode" runat="server" />    </div> C#: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->string m_Num;string m_Source;protected void Page_Load(object sender, EventArgs e){    }protected void btnEncode_Click(object sender, EventArgs e){    m_Num = this.txtNumber1.Text.Trim();    Session["Source"] = m_Source= Util.Encode("", m_Num);    lblEnCode.Text = m_Source;}protected void btnDecode_Click(object sender, EventArgs e){    m_Source = Session["Source"].ToString();    if (this.txtNumber2.Text.Trim() != "")    {        m_Source = this.txtNumber2.Text.Trim();        lblDecode.Text = Util.Decode("", m_Source);    }    else    {        lblDecode.Text = Util.Decode("", m_Source);    }}

返回顶部
查看电脑版