加密解密代码,效率方面想改进,各位朋友帮忙看看
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个方法就行了,有不足之处请跟帖提出。
加密原理是取得每个字符的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个方法就行了,有不足之处请跟帖提出。
相关文章推荐
- 看看软件开发人员在代码编写工作中为什么需要学习代码的优雅性,结构以及效率方面的内容。
- C#加密解密,请各位帮忙!
- 用Dezende解密zend加密后的php代码(转载)
- PHP RSA密文过长加密解密 越过1024的解决代码
- Axis2 Web Service安全之rampart 【加密解密的基本概念以及实例代码】
- base64加密解密c++代码
- 通过改进代码将for循环遍历数组提高效率
- Android使用RSA加密和解密的示例代码
- 请园中的各位朋友看看向日葵甘特图组件还需增加些什么功能?
- Delphi纯代码连SQLite数据库,同时支持数据库的加密解密
- 私钥签名解密公钥加密验签代码
- 由浅入深CIL系列:5.抛砖引玉:判断string是否为空的四种方法的CIL代码看看效率如何?
- 对加密系统下的项目代码如何进行解密
- DES加密解密工具2.1及其代码——支持字符串及文件加密,支持3重DES
- 如何做好程序,卖个好价钱?价格高了没人买,低了技术支持又太麻烦。最要命的是买了拿到代码或软件又申请退款。请各位帮忙?
- AES加密解密代码实现
- Cisco Type 7 Password 加密解密原理和代码
- 各位朋友对" JavaScript挑战赛--随机颜色"的挑战代码