数据库中密码是,用一段VB代码加密的,现在要用Java代码模拟这个加密过程用于一个Web程序的用户验证,但是就是没办法达到相同的加密字符串现在想把使用的这个Key到底是什么打印出来看看,但是lHKey是一个地址,怎么能打印这个用来加密的Key呢?麻烦大家帮忙看看。谢谢!Login8888会加密成:EA4B88AAB9A62B196FA973D18DB33AAA
VB代码:
Private Sub CryptoDecrypt()    Dim lHExchgKey As Long
    Dim lHCryptprov As Long
    Dim lHHash As Long
    Dim lHkey As Long
    Dim lResult As Long    Dim sProvider As String    Dim sCryptBuffer As String
    Dim lCryptBufLen As Long
    Dim lCryptPoint As Long    Dim lPasswordPoint As Long
    Dim lPasswordCount As Long    On Error GoTo decryptError    'Clear the Output buffer
    sOutputBuffer = ""    'Get handle to the default CSP.
    sProvider = vbNullChar
    sProvider = CryptoSvcProvFriendlyName & vbNullChar
    If Not CBool(CryptAcquireContext(lHCryptprov, 0&, sProvider, CryptoServiceProvider, CRYPT_VERIFYCONTEXT)) Then
        If GetLastError = 0 Then
            If Not CBool(CryptAcquireContext(lHCryptprov, 0&, sProvider, CryptoServiceProvider, CRYPT_NEWKEYSET)) Then
                ' MsgBox ("Error " & CStr(GetLastError) & " during CryptAcquireContext!")
                GoTo Finished
            End If
        End If
    End If    'Create a hash object
    If Not CBool(CryptCreateHash(lHCryptprov, HashAlgorithmID, 0, 0, lHHash)) Then
        ' MsgBox ("Error " & CStr(GetLastError) & " during CryptCreateHash!")
        GoTo Finished
    End If    'Hash in the password text
    If Not CBool(CryptHashData(lHHash, sPassword, Len(sPassword), 0)) Then
        ' MsgBox ("Error " & CStr(GetLastError) & " during CryptHashData!")
        GoTo Finished
    End If    'Create a session key from the hash object
    If Not CBool(CryptDeriveKey(lHCryptprov, CryptoAlgorithm, lHHash, 0, lHkey)) Then
        ' MsgBox ("Error " & CStr(GetLastError) & " during CryptDeriveKey!")
        GoTo Finished
    End If    'Destroy the hash object.
    CryptDestroyHash (lHHash)
    lHHash = 0
    '============================================================
    ' CitiAlert Modification:
    ' Convert the Encrypted 'HEX' string back to Hex (held in a String) ...
    '============================================================
    Dim tmpVal As String
    tmpVal = sInputBuffer
    sInputBuffer = ChangeHexToASCII(tmpVal)
    
    'S2S263 - Start
    'Text5.Text = sInputBuffer
    'S2S263 - End
    
    'Prepare sCryptBuffer for CryptDecrypt
    lCryptBufLen = Len(sInputBuffer) * 2 ' old code
    lCryptBufLen = Len(sInputBuffer)
    sCryptBuffer = String(lCryptBufLen, vbNullChar)
    LSet sCryptBuffer = sInputBuffer    'Decrypt data
    If Not CBool(CryptDecrypt(lHkey, 0, 1, 0, sCryptBuffer, lCryptBufLen)) Then
        ' MsgBox ("bytes required:" & CStr(lCryptBufLen))
        ' MsgBox ("Error " & CStr(GetLastError) & " during CryptDecrypt!")
        GoTo Finished
    End If    'Setup output buffer with just decrypted data
    sOutputBuffer = Mid$(sCryptBuffer, 1, lCryptBufLen)Finished:    'Destroy session key
    If (lHkey) Then lResult = CryptDestroyKey(lHkey)    'Destroy key exchange key handle
    If lHExchgKey Then CryptDestroyKey (lHExchgKey)    'Destroy hash object
    If lHHash Then CryptDestroyHash (lHHash)    'Release Context provider handle
    If lHCryptprov Then lResult = CryptReleaseContext(lHCryptprov, 0)    Exit SubdecryptError:
    ' MsgBox ("Decrypt Error: " & Error$)
    GoTo FinishedEnd SubPrivate Function ChangeStringToHexString(strtochange As String)    Dim nextchra 'As Integer
    Dim inptxt$, lenth%, numspc%, nextchr$, newsent$    Let inptxt$ = strtochange
    Let lenth% = Len(inptxt$)    Do While numspc% <= lenth% - 1
        Let numspc% = numspc% + 1
        Let nextchr$ = Mid$(inptxt$, numspc%, 1)
        nextchra = Hex(Asc(nextchr$))
        If Len(nextchra) = 1 Then
            nextchra = "0" & nextchra
        End If
        Let newsent$ = newsent$ + nextchra
    Loop
    ChangeStringToHexString = newsent$End Function

解决方案 »

  1.   

    不懂JAVA,所以不知道。这个问题的重点不在VB,因为VB是调用 advapi32.dll 来完成的。这要看 java 能不能调用 dll 。   如果能,让java也调用 advapi32.dll 来做轻而易举就完成了。   如果不能,那么意味着你必须用 java 来实现 advapi 的功能才能达到你的目的。不过3DES是标准算法,在网上可以找到这个算法,用java来写,虽然麻烦,也不是不能完成的任务。