今天,PM 问我可逆函数,当时还真蒙了一会,高中的数学忘的也真可以了。后来详谈后,才反应过来。
在大学的实验室做课题的时候,指导老师也是我院长,曾提供可逆加密解密的函数,起作用不用细说了。
这会整理出来,供大家参考。
我将这两个函数(加密函数 和 解密函数至于一个静态类中):
注意:这里,还是用的种子辅助加解密,以免大家都用的话,岂不是透明了,也就失去其加密的效果了。
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;
}
}
{
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:
<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>
<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#:
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);
}
}
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);
}
}
效果:
希望对大家有用。