您的位置:首页 > 编程语言

加密解密代码,效率方面想改进,各位朋友帮忙看看

2011-01-15 06:05 281 查看
看到的朋友也请顶一下,希望高手看到指点下。下面说下代码原理。
加密原理是取得每个字符的ascii码,然后ascii码在144以下的,用两个字母表示,ascii码在64000下面的,用三个字母表示,更大的用3开头,四个字母表示,超过四个字母的暂时不考虑
我的代码使用目的就是网站上ajax提交数据,还有客户端与服务器提交数据提供一个相对的数据安全。至于javascript,如果大家支持,我过段时间会把代码回复到此贴里。

//网站提交数据专用

public static string s52s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz3";

//数据加密类
public static string s52e(string n)
{
int nl = n.Length;
List<char> t = new List<char>();
int a, x;
for (x = 0; x < nl; x++)
{
a = (int)n[x];
if (a < 144)
{
t.Add(s52s[a / 12]);
t.Add(s52s[a % 12]);
}
else if (a < 64000)
{
t.Add(s52s[a / 1600 + 12]);
t.Add(s52s[(a / 40) % 40 + 12]);
t.Add(s52s[a % 40 + 12]);
}
else
{
t.Add('3');
t.Add(s52s[a / 64000 + 12]);
t.Add(s52s[(a / 1600) % 40 + 12]);
t.Add(s52s[(a / 40) % 40 + 12]);
t.Add(s52s[a % 40 + 12]);
}
}
return "3" + new string(t.ToArray());
}

//数据解密类

public static string s52d(string n)
{
if (!n.StartsWith("3")) return "";
int nl = n.Length;
List<char> t = new List<char>();
int a, x = 1, c;
while (x < nl)
{
a = s52s.IndexOf(n[x]);
x++;
if (a < 12)
{
c = a * 12 + s52s.IndexOf(n[x]);
}
else if (a < 52)
{
c = (a - 12) * 1600 + (s52s.IndexOf(n[x]) - 12) * 40;
x++;
c += s52s.IndexOf(n[x]) - 12;
}
else
{
x++;
a = s52s.IndexOf(n[x]);
x++;
c = (a - 12) * 64000 + (s52s.IndexOf(n[x]) - 12) * 1600;
x++;
c += (s52s.IndexOf(n[x]) - 12) * 40;
x++;
c += s52s.IndexOf(n[x]) - 12;
}
t.Add((char)c);
x++;
}
return new string(t.ToArray());
}

需要加密某字符串的时候,直接调用方法这2个方法就行了,有不足之处请跟帖提出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: