有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将Java HmacMD5函数转换为VB6

我正在开发一个windows应用程序,需要使用一些以前java代码中的auth函数。我可以访问Java源代码,但似乎仍然无法正确获取它。可能是因为我对密码学的知识有限

我需要转换的Java函数有:

public String getHMACHash(String SharedSecretKey, String TextToHash) {
    return base64EncodedStringFromBytes(hmacMD5(SharedSecretKey, TextToHash));
}

private String base64EncodedStringFromBytes(byte[] bArr) {
    return Base64.encodeToString(bArr, 2);
}


public byte[] hmacMD5(String SharedSecretKey, String TextToHash) {
    byte[] bArr = null;
    try {
        Mac instance = Mac.getInstance("HmacMD5");
        instance.init(new SecretKeySpec(SharedSecretKey.getBytes(), "HmacMD5"));
        bArr = instance.doFinal(TextToHash.getBytes());
    } catch (NoSuchAlgorithmException e) {
        Log.m8401e(TAG, e.getLocalizedMessage());
    } catch (InvalidKeyException e2) {
        Log.m8401e(TAG, e2.getLocalizedMessage());
    }
    return bArr;
}

因此,在输入值时:

SharedSecretKey = "497n9x98jK06gf7S3T7wJ2k455Qm192Q"
TextToHash = "1502322764327/customerservice.svc/buybackcartPOST8e802a045c1e60e"

生成的哈希为:

pOZNkg077OdvhyeMMPIX2w==

尽管我可能会尝试,但在VB6中使用相同的值时,我无法接近哈希键。我尝试了几种不同的方法来创建哈希:

Private Function hash_HMACMD5(ByVal sTextToHash As String, ByVal 
sSharedSecretKey As String)

Dim asc As Object, enc As Object
Dim TextToHash() As Byte
Dim SharedSecretKey() As Byte
Set asc = CreateObject("System.Text.UTF8Encoding")
Set enc = CreateObject("System.Security.Cryptography.HMACMD5")

 TextToHash = asc.Getbytes_4(sTextToHash)
 SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
 enc.Key = SharedSecretKey

 Dim bytes() As Byte
 bytes = enc.ComputeHash_2((TextToHash))
 hash_HMACMD5 = Base64Encode(bytes)

 Set asc = Nothing
 Set enc = Nothing

 End Function

我希望有人能给我指出正确的方向

提前感谢您的帮助

波特曼100

我已经追踪了所有的代码,但我看不到任何东西表明正在发生一些不同的事情。如下所述,有一个导入行

import 安卓.util.Base64;

创建哈希的调用是:

String hMACHash = new MASecurity().getHMACHash(str, str2);

安全等级为:

import 安卓.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class MASecurity {
private static final String TAG = "MASecurity";

public String getHMACHash(String str, String str2) {
    return base64EncodedStringFromBytes(hmacMD5(str, str2));
}

private String base64EncodedStringFromBytes(byte[] bArr) {
    return Base64.encodeToString(bArr, 2);
}

public byte[] hmacMD5(String str, String str2) {
    byte[] bArr = null;
    try {
        Mac instance = Mac.getInstance("HmacMD5");
        instance.init(new SecretKeySpec(str.getBytes(), "HmacMD5"));
        bArr = instance.doFinal(str2.getBytes());
    } catch (NoSuchAlgorithmException e) {
        MALog.m8401e(TAG, e.getLocalizedMessage());
    } catch (InvalidKeyException e2) {
        MALog.m8401e(TAG, e2.getLocalizedMessage());
    }
    return bArr;
}

输入值正确,因为它们在应用程序运行时被记录

希望这有帮助


共 (1) 个答案

  1. # 1 楼答案

    感谢Alex K.,Java代码似乎在向其中一个参数添加更多数据,而我的调试遗漏了这个参数,我添加了额外的数据,它创建了一个有效的散列