2
 Public Function EncryptString(theString As String, TheKey As String) As String
    Dim X As Long
    Dim eKey As Byte, eChr As Byte, oChr As Byte, tmp$
    For i = 1 To Len(TheKey)
         'generate a key
          eKey = Asc(Mid$(TheKey, i, 1)) Xor eKey
    Next

    'reset random function
    Rnd -1
    'initilize our key as the random seed
    Randomize eKey
    'generate a pseudo old char
    oChr = Int(Rnd * 256)
    'start encryption
    For X = 1 To Len(theString)
        pp = pp + 1
        If pp > Len(TheKey) Then pp = 1
        eChr = Asc(Mid$(theString, X, 1)) Xor _
                   Int(Rnd * 256) Xor Asc(Mid$(TheKey, pp, 1)) Xor oChr
        tmp$ = tmp$ & Chr(eChr)
        oChr = eChr
    Next
    EncryptString = AsctoHex(tmp$)    

End Function


Public Function DecryptString(theString As String, TheKey As String) As String

Dim X As Long
Dim eKey As Byte, eChr As Byte, oChr As Byte, tmp$
For i = 1 To Len(TheKey)
     'generate a key
     eKey = Asc(Mid$(TheKey, i, 1)) Xor eKey
Next
'reset random function
Rnd -1
'initilize our key as the random seed
Randomize eKey
'generate a pseudo old char
oChr = Int(Rnd * 256)
'start decryption
tmp$ = HexToAsc(theString)
    DecryptString = ""
    For X = 1 To Len(tmp$)
    pp = pp + 1
    If pp > Len(TheKey) Then pp = 1
    If X > 1 Then oChr = Asc(Mid$(tmp$, X - 1, 1))
    eChr = Asc(Mid$(tmp$, X, 1)) Xor Int(Rnd * 256) Xor _
           Asc(Mid$(TheKey, pp, 1)) Xor oChr
        DecryptString = DecryptString & Chr$(eChr)
Next

End Function


Private Function AsctoHex(ByVal astr As String)

For X = 1 To Len(astr)
hc = Hex$(Asc(Mid$(astr, X, 1)))
nstr = nstr & String(2 - Len(hc), "0") & hc
Next
AsctoHex = nstr

End Function
4

2 回答 2

6

您永远不应该尝试自己实施这样的加密。正确地做到这一点非常困难,而且很容易意外地构建漏洞。

找到一个已被证明有效且经过大量测试的现有解决方案会更容易、更安全。可能是一个更好的解决方案。

于 2012-05-24T03:12:12.547 回答
4

这有几个缺陷:

如果您想在与加密位置不同的系统上解密(或者如果您更新系统,......),您将遇到麻烦:Randomize eKey使用以下一组调用Rnd不能保证在重新启动后返回相同的序列。它肯定不会在不同的系统上返回相同的序列。

您将密码减少到单个 8 位值 (eKey),因此您的加密具有 8 位的实际密钥长度。

简而言之:任何有权访问与您的系统足够相似的系统(即,您的解密实际上会产生明文的系统)只需要克隆您的DecryptString函数并使用 eKey=0..255 运行它。

算了,用有用的东西吧。阅读关于自制加密的 Schneier

于 2012-05-24T03:13:13.263 回答