Java和Php返回不同的哈希值

问题描述 投票:0回答:2

所以,PHP代码:

$result = hash("whirlpool","xxx".$password."zzz");

JAVA:

import gnu.crypto.Registry;
import gnu.crypto.hash.HashFactory;
import gnu.crypto.hash.IMessageDigest;

public class WhirlpoolHash {

    String result;

    WhirlpoolHash(String pass) {

        String to_encode = "xxx"+pass+"zzz";

        IMessageDigest old_encoder = HashFactory.getInstance(Registry.WHIRLPOOL_HASH);
        byte[] input = to_encode.getBytes();
        old_encoder.update(input, 0, input.length);
        byte[] digest = old_encoder.digest();
        this.result = gnu.crypto.util.Util.toString(digest).toLowerCase();

    }

    public String Get() {

        return this.result;
    }
}

并且结果变量不同。我需要java类来返回与php相同的值。我将密码存储在PHP生成的MySQL DB UTF-8中,需要将其与JavaFX应用发送的数据进行比较。

当然,我可以发送未加密的密码,并使用php进行密码,但我不愿意这样做。

java php encryption hash whirlpool
2个回答
0
投票

所以Java example for encrypting pwd with whirlpool algorithm using gnu-crypto jar是一个答案。

我不知道为什么,但是jonelo.jacksum.JacksumAPI给出的结果与PHP相同。


0
投票

最新回应,但以防万一。

我几乎遇到了完全相同的问题,并在Java中使用了Bouncy Castle。经过一番尝试和错误之后,我使用Whirlpool进行了哈希处理以匹配我的PHP哈希,该哈希看起来与您的哈希相似。假设您输入密码:

    WhirlpoolDigest messageDigest = new WhirlpoolDigest();

    final String convertedHash = "xxx" + password + "yyy";
    messageDigest.reset();
    final byte[] bytes = convertedHash.getBytes();
    messageDigest.update(bytes, 0, bytes.length);

    byte[] hash = new byte[messageDigest.getDigestSize()];

    messageDigest.doFinal(hash, 0);

    System.out.println(Hex.toHexString(hash));

对我来说最大的问题是最后的步骤-doFinal()和Hex.toHexString()...

我的Maven依赖项看起来像这样:

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-ext-jdk15on</artifactId>
        <version>1.64</version>
    </dependency>
© www.soinside.com 2019 - 2024. All rights reserved.