C# HMAC 到 Java

发布于 2024-10-17 16:12:20 字数 661 浏览 3 评论 0原文

我正在为下面的代码编写等效的java代码。但我可以制作一些为 encodedString 返回相同结果的东西。我可以使用什么 Java 类来达到相同的结果?

//Set the Hash method to SHA1
HMAC hash;
switch (validation)
{
    case MachineKeyValidation.MD5:
        hash = new HMACMD5();
        break;
    case MachineKeyValidation.SHA1:
    default:
        hash = new HMACSHA1();
        break;
}
//Get the hash validation key as an array of bytes
hash.Key = HexToByte(validationKey);
//Encode the password based on the hash key and
//converts the encrypted value into a string
encodedString = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));

提前致谢! :)

I'm making the equivalent java code for the code below. But I can make something that returns the same result for encodedString. What Java class can I use for achieve the same result?

//Set the Hash method to SHA1
HMAC hash;
switch (validation)
{
    case MachineKeyValidation.MD5:
        hash = new HMACMD5();
        break;
    case MachineKeyValidation.SHA1:
    default:
        hash = new HMACSHA1();
        break;
}
//Get the hash validation key as an array of bytes
hash.Key = HexToByte(validationKey);
//Encode the password based on the hash key and
//converts the encrypted value into a string
encodedString = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));

Thanks in advance!
:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沩ん囻菔务 2024-10-24 16:12:20

我找到了翻译代码的解决方案。
有两个主要问题。当请求HMACSHA1时,我不是在谈论SHA1算法,而是HmacSHA1。 Java 和 C# 的编码是有区别的。我使用了正确的密钥和正确的算法,但编码不同。

SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// The big problem is difference between C# and Java encoding
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-16LE"));
result = new String(Base64.encode(rawHmac));

I found a solution for the translation code.
There was two main problem. When a request a HMACSHA1 I'm not talking about a SHA1 algorithm, but a HmacSHA1. And there is a difference between the encoding from Java and C#. I was using the correct key, and the correct algorithm, but the encoding was differente.

SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// The big problem is difference between C# and Java encoding
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-16LE"));
result = new String(Base64.encode(rawHmac));
七颜 2024-10-24 16:12:20

请参阅有关在 Java 中计算哈希函数的问题。

并查看 javadoc 的 java.security.MessageDigest.getInstance(字符串算法)

编辑添加:

尝试运行以下应用程序以查看您注册了哪些提供商。


import java.security.Provider;
import java.security.Security;

public class SecurityTest {

    public static void main(String[] args) {

        Provider[] providers = Security.getProviders();
        for (Provider p : providers) {
            System.out.println(p.toString());
        }
    }
}

您应该至少列出一些 Sun 提供商。如果没有,您可能需要下载一些安全库。

See this question about computing hash functions in Java.

And look at the javadoc for java.security.MessageDigest.getInstance(String algorithm).

Edited to add:

Try running the following app to see what providers you have registered.


import java.security.Provider;
import java.security.Security;

public class SecurityTest {

    public static void main(String[] args) {

        Provider[] providers = Security.getProviders();
        for (Provider p : providers) {
            System.out.println(p.toString());
        }
    }
}

You should have at least a few Sun providers listed. If not, you may need to download some security libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文