当前位置:编程学习 > asp >>

猫七ASP版本的数据加密、解密类

在很多场合,特别重要的要加密传输。

使用成熟的加密算法是一个不错的选择,但是~ 有些算法这个语言支持而另一种语言不支持。

或者直接要安装某某组件,实在是太烦琐了,为了方便以后使用,自己抽空写了一个。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'=====================================================================
'=                       猫七数据加密、解密类                         =
'=     Copyright (c) 2011 猫七(QQ:77068320) All rights reserverd.    =
'=              请尊重作者劳动成果,转载请保留代码的完整性             =
'=====================================================================
'= 作者:苗启源(博客:http://www.miaoqiyuan.cn)                        =
'= 发布地址:http://www.zzzyk.com/show/6bb4b497081dbd7b.htm
'= 最新:http://www.miaoqiyuan.cn/products/Catseven.Coding.rar
'=====================================================================
'=  文件名:Class.Catseven.Coding.asp                                =
'=  功  能:猫七数据加密、解密函数                                     =
'=====================================================================
  class Catseven_Coding
    public akey,ekey,keylen,keymax,autolen
  
    '类初始化
    '  akey    编码表
    '  ekey    密钥
    '  keylen  标准编码长度
    '  keymax  最大编码长度,默认1000,代表1000-数据长度必须是3位数字,即数据的长度可以为0-900
    '  autolen 如果不足,是否补全
    public sub class_initialize()
      akey   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
      ekey   = "f67RSTUOPDp02qd34ABbMijQFxyr5szZnot89+Y/=EghkVavwuHCXWmKLJNGIcel1"
      keylen = 900
      keymax = 1000
      autolen= true
    end sub
  
    '函数:randkey
    '功能:创建随机字符
    '参数:rndkeylen  随机字符长度
    private function randkey(byval rndkeylen)
      dim rndnum,keymap,rstr,i
      keymap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
      rstr = ""
      for i = 1 to rndkeylen
        randomize
        rndnum = cLng(len(keymap) - 1) * rnd() + 1
        rstr   = rstr & mid(keymap,rndnum,1)
      next
      randkey = rstr
    end function
  
    '函数:encode
    '功能:加密字符串
    '参数:str    要加密的字符串
    public function encode(byval str)
      dim alen,i,vstr,kstr,vkey
      str  = replace(str,"\","")
      alen = len(str)
      if alen = 0 then
        encode = ""
      else
        vstr = ""
        vkey = randkey(1)
        for i = 1 to len(str)
          kstr = mid(str,i,1)
          vstr = vstr & mid(akey,instr(ekey,kstr),1)
        next
        alen = keymax - alen
        vstr = len(alen) & vkey & randkey(9 - len(keymax)) & alen & UCase(vkey) & vstr
        alen = len(vstr)
        if keylen < alen then keylen = alen
        if keylen > keymax * 0.9 then keylen = keymax * 0.9
        vstr = mid(vstr,1,keylen)
        if alen < keylen and autolen = true then
          vstr = vstr & randkey(keylen - alen)
        end if
        encode = vstr
      end if
    end function
  
    '函数:decode
    '功能:解密字符串
    '参数:str    要解密的字符串
    public function decode(byval str)
      dim alen,i,vstr,kstr,vkey
      alen = mid(str,1,1)
      vkey = mid(str,2,1)
      if not isnumeric(alen) then
        decode = ""
      else
        alen = mid(str,11 - alen,alen)
        if not isnumeric(alen) then
          decode = ""
        else
          alen = keymax - alen + 1
          str = mid(str,11,alen)
          if mid(str,1,1) = UCase(vkey) then
            vstr = ""
            str = mid(str,2)
            for i = 1 to len(str)
              kstr = mid(str,i,1)
              vstr = vstr & mid(ekey,instr(akey,kstr),1)
            next
            decode = vstr
          else
            decode = ""
          end if
        end if
      end if
    end function
  end class

使用的时候比较简单,只支持英文加密,所以使用的时候,请配合base64函数(相关代码:http://www.miaoqiyuan.cn/p/tag/base64)使用。加密的时候用encode。

1
2
3
4
5
6
  set a = new Catseven_Coding
  a.autolen = true
  a.keylen  = 150
  response.write a.encode("5qyi6L+O5ZKM5oiR6K6o6K6677yM5pys5paH5Y6f5paH5Zyw5Z2A77yaaHR0cDovL3d3dy5taWFvcWl5dWFuLmNuL3AvQ2F0c2V2ZW4tQ29kaW5n")
  
'base64编码后的字符

解密的时候直接用decode。

1
2
3
4
  set a = new Catseven_Coding
  response.write a.decode("3kfRpeT888KcNaVB4lHcf3UchVDB3BhB3BBCCaUcKadcKuycmBAcKuycfawcfMRCCauuyDL9Jhv4POPOaciu1Yv91/cO1Yx426x4PRvXMYL9MtMf1QiXMksu1cgkwbbM5c2IZDXHMqcZSnkCb4C6LP")
  
'解码后,为base64编码

可以指定ekey指定对照表

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,