看网上对字符加密的代码很多但是没有对数字加密的(当然解密后也是数字)。不知各位有何高见 异或是最简单的了。

解决方案 »

  1.   

    '这个可以对数字或字母或汉字加密解密
    Public Function Encode(ByVal S As String) As String '加密
    On Error GoTo acd
        If Len(S) = 0 Then Exit Function
        Dim Buff() As Byte
        Buff = StrConv(S, vbFromUnicode)
        Dim i As Long
        Dim j As Byte
        Dim k As Byte, m As Byte
        Dim mstr As String
        mstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
        Dim outs As String
        i = UBound(Buff) + 1
        outs = Space(2 * i)
        Dim Temps As String
        For i = 0 To UBound(Buff)
            Randomize Time
            j = CByte(5 * (Math.Rnd()) + 0) '最大产生的随机数只能是5,不能再大了,再大的话,就要多用一个字节
            Buff(i) = Buff(i) Xor j
            k = Buff(i) Mod Len(mstr)
            m = Buff(i) \ Len(mstr)
            m = m * 2 ^ 3 + j
            Temps = Mid(mstr, k + 1, 1) + Mid(mstr, m + 1, 1)
            Mid(outs, 2 * i + 1, 2) = Temps
         Next
         Encode = outs
    Exit Function
    acd:
    End FunctionPublic Function Decode(ByVal S As String) As String '解密
        On Error GoTo acd
        Dim i As Long
        Dim j As Byte
        Dim k As Byte
        Dim m As Byte
        Dim mstr As String
        mstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
        Dim t1 As String, t2 As String
        Dim Buff() As Byte
        Dim n As Long
        n = 0
        For i = 1 To Len(S) Step 2
            t1 = Mid(S, i, 1)
            t2 = Mid(S, i + 1, 1)
            k = InStr(1, mstr, t1) - 1
            m = InStr(1, mstr, t2) - 1
            j = m \ 2 ^ 3
            m = m - j * 2 ^ 3
            ReDim Preserve Buff(n)
            Buff(n) = j * Len(mstr) + k
            Buff(n) = Buff(n) Xor m
            n = n + 1
         Next
         Decode = StrConv(Buff, vbUnicode)
         Exit Function
    acd:
         Decode = ""
    End Function
      

  2.   

    一些常见的加密算法的VB版本
    http://www.china-askpro.com/msg1/qa88.shtml
    http://dev.csdn.net/develop/article/8/8353.shtm
    http://www.itonline.gd.cn/ittech/list.asp?id=69
    http://dev.csdn.net/develop/article/8/8356.shtm
      

  3.   

    rsa
    md5
    或者简单的做个密码表