我想加密文件。我使用異或來加密它。但是,問題是,我可以加密,但是當我要解密它,它給了我不同的結果(而不是原始文件)XOR加密在加密和解密時給予不同的結果
這裏是我使用
Public Function Encrypt(ByVal txt As String, ByVal pass As String) As String
Dim mem() As Byte
mem = StrConv(txt, vbFromUnicode)
For i = 0 To UBound(mem)
mem(i) = Asc(mem(i)) Xor Asc(Mid(pass, (i Mod Len(pass)) + 1, 1))
Next i
Encrypt = StrConv(mem, vbUnicode)
Erase mem
End Function
Public Function Decrypt(ByVal txt As String, ByVal pass As String) As String
Dim mem() As Byte
mem = StrConv(txt, vbFromUnicode)
For i = 0 To UBound(mem)
mem(i) = Asc(mem(i)) Xor Asc(Mid(pass, (i Mod Len(pass)) + 1, 1))
Next i
Decrypt = StrConv(mem, vbUnicode)
Erase mem
End Function
Private Sub Form_Load()
Enkrip = Encrypt(txt.LoadFile("C:\mom.txt"), "FROM YOUR SON")
dekrip = Decrypt(Enkrip, "FROM YOUR SON")
MsgBox Enkrip & vbCrLf & dekrip
End Sub
值的代碼dekrip的應該是C的原始內容:\ mom.txt,但它返回不同的結果
我也試過這個代碼,它工作得很好
Private Sub Form_Load()
Data = "A"
pass = "B"
enkrip = Chr(Asc(Data) Xor Asc(pass))
dekrip = Chr(Asc(enkrip) Xor Asc(pass))
MsgBox enkrip & vbCrLf & dekrip
End Sub
我應該怎麼做才能修復我的代碼?
感謝
單步執行代碼並檢查值,最好在十六進制中檢查它們。花點時間瞭解字符編碼和值。 – zaph