【问题标题】:Java and Php returns different hashesJava 和 Php 返回不同的哈希值
【发布时间】:2020-01-16 12:18:03
【问题描述】:

所以,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 进行,但我不想这样做。

【问题讨论】:

  • 我不是很熟悉,但是phps的hash过程可能和java使用的不一样。
  • 不,它是一样的,它是 SHA256 @Maxim 看看这个问题,如果你有什么不明白的,请告诉我,stackoverflow.com/questions/4680661/…
  • Java 有一些不同的 Whirlpool 哈希库,但我认为 Whirlpool 是 С、Python 或 Java 中的 Whirlpool。我认为存在编码问题...
  • @KHI,所以我尝试了来自 java.nio.charset.StandardCharsets 的to_encode.getBytes(StandardCharsets.UTF_8) 并得到了与我没有重载 getBythes 方法的结果相同的结果。这很奇怪,因为我知道默认情况下 Java 中的所有字符串变量都保存在 UTF8 中,我用来发送传递给 PHP 脚本的 Jquery AJAX 默认情况下是 UTF8 两个。也许来自 Php 的盐是 ANCII?

标签: java php encryption hash whirlpool


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    回复较晚,但以防万一对其他人有所帮助。

    我遇到了几乎完全相同的问题,并在 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>
    

    【讨论】:

      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 2014-08-01
      • 2014-04-08
      • 2013-10-18
      • 1970-01-01
      • 2018-03-23
      • 2018-11-08
      • 1970-01-01
      相关资源
      最近更新 更多