请求将Java带盐SHA512加密代码转换为VBA代码
Convert Java SHA-512 Salted Iterated Hashing to VBA
Got it, let's translate your Java code into VBA while keeping the exact same logic: 100,000 iterations of hashing the input + salt, followed by a final SHA-512 hash. Here's a complete, working implementation:
VBA Code Implementation
Option Explicit ' Windows CryptoAPI declarations (64-bit compatible) Private Declare PtrSafe Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" ( _ ByRef phProv As LongPtr, _ ByVal pszContainer As String, _ ByVal pszProvider As String, _ ByVal dwProvType As Long, _ ByVal dwFlags As Long _ ) As Boolean Private Declare PtrSafe Function CryptCreateHash Lib "advapi32.dll" ( _ ByVal hProv As LongPtr, _ ByVal Algid As Long, _ ByVal hKey As LongPtr, _ ByVal dwFlags As Long, _ ByRef phHash As LongPtr _ ) As Boolean Private Declare PtrSafe Function CryptHashData Lib "advapi32.dll" ( _ ByVal hHash As LongPtr, _ ByVal pbData As String, _ ByVal dwDataLen As Long, _ ByVal dwFlags As Long _ ) As Boolean Private Declare PtrSafe Function CryptGetHashParam Lib "advapi32.dll" ( _ ByVal hHash As LongPtr, _ ByVal dwParam As Long, _ ByVal pbData As String, _ ByRef pdwDataLen As Long, _ ByVal dwFlags As Long _ ) As Boolean Private Declare PtrSafe Function CryptDestroyHash Lib "advapi32.dll" ( _ ByVal hHash As LongPtr _ ) As Boolean Private Declare PtrSafe Function CryptReleaseContext Lib "advapi32.dll" ( _ ByVal hProv As LongPtr, _ ByVal dwFlags As Long _ ) As Boolean ' CryptoAPI constants Private Const PROV_RSA_AES As Long = 24 Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000 Private Const CALG_SHA_512 As Long = &H800C Private Const HP_HASHVAL As Long = 2 Public Function GetSHA512WithSalt(ByVal plainText As String, ByVal salt As String) As String Dim currentHash As String Dim i As Long ' Start with plaintext + salt (matches Java's initial iteration input) currentHash = plainText & salt ' Run 100,000 iterations of hashing (each time append salt to previous hash) For i = 1 To 100000 currentHash = ComputeSHA512(currentHash & salt) ' Exit early if any hashing error occurs If currentHash = "Error" Then GetSHA512WithSalt = "Error" Exit Function End If Next i ' Final SHA-512 hash to match Java's final return statement GetSHA512WithSalt = ComputeSHA512(currentHash) End Function Private Function ComputeSHA512(ByVal inputString As String) As String Dim hProv As LongPtr Dim hHash As LongPtr Dim hashBytes() As Byte Dim hashLen As Long Dim result As String Dim i As Long ' Acquire system cryptographic context If Not CryptAcquireContext(hProv, vbNullString, vbNullString, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) Then ComputeSHA512 = "Error" Exit Function End If ' Create SHA-512 hash object If Not CryptCreateHash(hProv, CALG_SHA_512, 0, 0, hHash) Then CryptReleaseContext hProv, 0 ComputeSHA512 = "Error" Exit Function End If ' Hash the input (convert to UTF-8 to match Java's getBytes() default behavior) If Not CryptHashData(hHash, StrConv(inputString, vbFromUnicode), LenB(StrConv(inputString, vbFromUnicode)), 0) Then CryptDestroyHash hHash CryptReleaseContext hProv, 0 ComputeSHA512 = "Error" Exit Function End If ' Get the length of the generated hash hashLen = 0 If Not CryptGetHashParam(hHash, HP_HASHVAL, vbNullString, hashLen, 0) Then CryptDestroyHash hHash CryptReleaseContext hProv, 0 ComputeSHA512 = "Error" Exit Function End If ' Retrieve the hash bytes ReDim hashBytes(hashLen - 1) If Not CryptGetHashParam(hHash, HP_HASHVAL, hashBytes(0), hashLen, 0) Then CryptDestroyHash hHash CryptReleaseContext hProv, 0 ComputeSHA512 = "Error" Exit Function End If ' Convert byte array to lowercase hex string (matches Java's toHexString output) result = "" For i = LBound(hashBytes) To UBound(hashBytes) result = result & Right("0" & Hex(hashBytes(i)), 2) Next i ' Clean up cryptographic resources CryptDestroyHash hHash CryptReleaseContext hProv, 0 ComputeSHA512 = LCase(result) End Function ' Test the function to verify it matches your Java code Sub TestSHA512Hashing() Debug.Print GetSHA512WithSalt("123", "AAZZ") ' This should output the same value as your Java main method End Sub
Key Details to Note
- Iteration Logic: The
GetSHA512WithSaltfunction exactly mirrors your JavagetSHA512method: 100,000 iterations of hashing the previous result + salt, then one final hash. - UTF-8 Conversion: We convert input strings to UTF-8 bytes to match Java's default
getBytes()behavior, ensuring consistent cross-language results. - Hex Case: The final hex string is converted to lowercase to match Java's
Integer.toHexStringoutput. - 64-Bit Support: Uses
PtrSafedeclarations so it works in both 32-bit and 64-bit versions of Excel/VBA. - Error Handling: Includes basic error checking to catch issues with cryptographic context acquisition or hashing.
To test, just run the TestSHA512Hashing subroutine—you'll see the same output as your Java code in the VBA Immediate Window.
内容的提问来源于stack exchange,提问作者YasserKhalil
相关产品推荐
相关产品推荐

