一个不错的javascript加密解密算法源码

作者:aasvvv 来源:51js 时间:2010-03-28 13:12:00 

来炫耀一下,谁看得懂我写的加密算法

写了一整天了,这个代码用于ajax提交,要求就是加密后内容不能变得过长,加密解密需要效率高,至于安全性,被抓取的数据无法被破解就可以了,下面的f23.ts可以算是密钥。现在有点不如意的地方就是英文内容加密后,数据长度增加一倍,中文加密不会增加数据长度,对于ajax提交,其实中文字符长度是大大减少了。


对应的服务器端c#版本,15000字加密再解密不用1毫秒。


//网站提交数据专用
public static string s52s = "8ABC7DLO5MN6Z9EFGdeJfghijkHIVrstuvwWSTUXYabclmnopqKPQRxyz01234";
static bool s52t = true;
static int N, N2;
static int[] s52r = new int[128];
static void s52f()
{
    N = s52s.Length;
    N2 = N * N;
    for (var x = 0; x < s52s.Length; x++)
    {
         s52r[(int)s52s[x]] = x;
    }
    s52t = false;
}

public static string s52e(string n)
{
    if (s52t) s52f();
    int l = n.Length, a, x = 0;
    List<char> t = new List<char>(l * 3);
    for (; x < l; x++)
    {
         a = (int)n[x];
         if (a < N2)
         {
              t.Add(s52s[a / N]);
              t.Add(s52s[a % N]);
         }
         else
         {
              t.Add(s52s[a / N2 + 5]);
              t.Add(s52s[(a / N) % N]);
              t.Add(s52s[a % N]);
         }
    }
    string s = new string(t.ToArray());
    return s.Length.ToString().Length + s.Length.ToString() + s;
}

public static string s52d(string n)
{
    if (s52t) s52f();
    int c;
    if (!int.TryParse(n[0].ToString(), out c)) return "";
    if (!int.TryParse(n.Substring(1, c), out c)) return "";
    int x = c.ToString().Length + 1;
    if (n.Length != c + x) return "";
    int nl = n.Length, a;
    List<char> t = new List<char>(nl * 3);
    for (; x < nl; x++)
    {
         a = s52r[(int)n[x]];
         x++;
         if (a < 5)
         {
              c = a * N + s52r[(int)n[x]];
         }
         else
         {
              c = (a - 5) * N2 + s52r[(int)n[x]] * N;
              x++;
              c += s52r[(int)n[x]];
         }
         t.Add((char)c);
    }
    return new string(t.ToArray());
}


 

标签:加密,解密,算法,源码,javascript
0
投稿

猜你喜欢

  • 详解Windows下PyCharm安装Numpy包及无法安装问题解决方案

    2021-01-27 11:58:32
  • Python 生成VOC格式的标签实例

    2021-09-09 04:39:07
  • 浅析Python 中整型对象存储的位置

    2021-10-06 13:40:20
  • VSCode如何巧用正则表达式快速处理字符段

    2022-06-13 06:44:00
  • 基于python的BP神经网络及异或实现过程解析

    2021-10-29 00:02:01
  • php注册和登录界面的实现案例(推荐)

    2024-04-30 08:48:47
  • python单向循环链表实例详解

    2023-05-25 01:29:40
  • python MNIST手写识别数据调用API的方法

    2021-05-13 19:20:45
  • TIOBE编程语言排行榜前20的语言入门书籍推荐

    2023-04-05 12:19:48
  • select * from sp_who的解决方案

    2024-01-15 09:55:52
  • face_recognition库在python的安装

    2021-06-16 02:29:27
  • 一文带你吃透Golang中的类型转换

    2024-02-20 18:12:28
  • 利用PyQt5模拟实现网页鼠标移动特效

    2022-11-17 10:46:01
  • 基于Python编写一个ISBN查询工具

    2022-02-22 09:43:21
  • Flask response响应的具体使用

    2021-01-30 06:48:27
  • go install和go get的区别实例详解

    2024-05-05 09:30:07
  • (X)HTML的文档结构

    2008-06-30 12:25:00
  • 使用Python解析JSON数据的基本方法

    2021-07-03 13:54:21
  • python读取raw binary图片并提取统计信息的实例

    2023-09-22 12:46:35
  • python按行读取文件,去掉每行的换行符\\n的实例

    2022-06-01 03:49:43
  • asp之家 网络编程 m.aspxhome.com