0
我正在開發一個Windows應用程序,並且需要使用一些以前的java代碼中的一些auth函數。我有權訪問Java源代碼,但似乎無法正確使用它。可能是因爲我有限的密碼學知識。將Java HmacMD5函數轉換爲VB6
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
所以,我希望有人能夠指出我在正確的方向嗎?
謝謝提前尋求幫助。
Potman100
我已經通過跟蹤所有的代碼,我看不出這將表明不同的是怎麼回事什麼什麼東西。如下面所提到的,有一個進口管線
import android.util.Base64;
創建哈希呼叫是:
String hMACHash = new MASecurity().getHMACHash(str, str2);
MASecurity類是:
import android.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;
}
輸入值是正確的,因爲它們在應用程序運行時記錄。
希望這有助於?
什麼是'str1'和什麼是'str2'。請像VB中那樣使用像「text」和「secretKey」這樣有意義的變量名,以便人們可以關注和理解你的代碼。 –
嗨,我剛剛添加的代碼,它是在舊的Java代碼, str = SharedKey str2 = TextToHash 我將更新上面的代碼。 – Potman100
從VB6我得到'0MQLYL4A9fH2 + o2Pb0PYJw =='和從Java我得到相同的:https://ideone.com/yHmaoQ - 唯一的區別是我使用'Base64.getEncoder()。encodeToString(bArr)'爲你還沒有說明你的代碼裏有什麼'Base64'類型。因此,無論您使用的編碼例程是做了一件意想不到的事情,還是'pOZNkg077OdvhyeMMPIX2w =='都是不同輸入集合的散列。 –