我用下面函数把STR(包括汉字)加密后,保存到INI文件里,再读取出来,然后还原.有些信息老错误.不知道什么原因.我没有进行编码转换,改怎么转换编码啊?Public Function EnDeCode(CharString, key As Integer)    Dim X As Single, i As Long    Dim CharNum As Integer, RandomInteger As Integer    Dim CharSingle As String * 1    On Local Error GoTo EDcodeError    EnDeCode = ""    If Len(CharString) = 0 Then        'EnDeCode = "1"        Exit Function    End If    X = Rnd(-key)    For i = 1 To Len(CharString)        CharSingle = Mid$(CharString, i, 1)        CharNum = Asc(CharSingle)        RandomInteger = Int(256 * Rnd) And &H7F        CharNum = CharNum Xor RandomInteger        CharSingle = Chr$(CharNum)        EnDeCode = EnDeCode + CharSingle    Next i    Exit FunctionEDcodeError:    EnDeCode = "0"
    '////////////////////////
End Function

解决方案 »

  1.   

    EnDeCode()完成的是什麼樣的功能? 各參數分別代表什麼意思? 返回什麼?——許多適合英文的算法因為沒有考慮雙字節文字的情況,會不適合中文的。
      

  2.   

    EnDeCode() 是一个对STR加密解密的函数
    使用方法
    加密EnDeCode(待加密字符串,密码[只能有数字])
    解密EnDeCode(待加密字符串,密码[只能有数字])测试方法,建立新窗体,个TEXT1,text2 command1,command2生成的样式为:BG?.VI:]HLLOption ExplicitPublic Function EnDeCode(CharString, key As Integer)        Dim X     As Single, i       As Long        Dim CharNum     As Integer, RandomInteger       As Integer        Dim CharSingle     As String * 1        On Local Error GoTo EDcodeError        EnDeCode = ""        If Len(CharString) = 0 Then                'EnDeCode   =   "1"                Exit Function        End If        X = Rnd(-key)        For i = 1 To Len(CharString)                CharSingle = Mid$(CharString, i, 1)                CharNum = Asc(CharSingle)                RandomInteger = Int(256 * Rnd) And &H7F                CharNum = CharNum Xor RandomInteger                CharSingle = Chr$(CharNum)                EnDeCode = EnDeCode + CharSingle        Next i        Exit FunctionEDcodeError:        EnDeCode = "0"
            '////////////////////////
    End FunctionPrivate Sub Command1_Click()
    Text2.Text = EnDeCode(Text1.Text, 21)
    Text1.Text = ""
    End SubPrivate Sub Command2_Click()
    Text1.Text = EnDeCode(Text2.Text, 21)
    Text2.Text = ""
    End SubPrivate Sub Form_Load()
    MsgBox "Command1为加密,Command2为解密"
    Text1.Text = "等待加密的字符串,Test"
    End Sub
      

  3.   

    VB string是unicode的,除了开头是4个字节的长度信息外,后面的每个字符都是unicode编码。
    asc函数恐怕不行吧,
    你需要把字符串转成byte数组。
      

  4.   

    Public Function EnDeCode(CharString, key As Integer)
        Dim X As Single, i As Long
        Dim CharNum As Integer, RandomInteger As Integer
        Dim CharSingle As String * 1
        Dim a() As Byte
        On Local Error GoTo EDcodeError
        
        EnDeCode = ""
        If Len(CharString) = 0 Then
            'EnDeCode = "1"
            Exit Function
        End If
        X = Rnd(-key)
        a = StrConv(CharString, vbFromUnicode) '这里将Unicode转成DBCS
        For i = 0 To UBound(a)
            CharNum = a(i)
            RandomInteger = Int(256 * Rnd) And &H7F
            CharNum = CharNum Xor RandomInteger
            CharSingle = Chr$(CharNum)
            EnDeCode = EnDeCode & CharSingle
        Next i
        Exit Function
    EDcodeError:
        EnDeCode = "0"
        '////////////////////////
    End Function