简体   繁体   English

Php 和 Java Sha1 方法给出不同的结果

[英]Php and Java Sha1 methods gives different results

I am trying to convert a php code to java code but at sha1, i am getting different strings from php and java.For example,I wrote a code for php; I am trying to convert a php code to java code but at sha1, i am getting different strings from php and java.For example,I wrote a code for php;

$password = 'pass123';
$hassedPasswordInDB = 'e1NTSEF9ZWxtNWlJcjJOVkVaenphZjA5dnlnUWFYdVlHaEU3dzQ=';
$sPasswordHash = base64_decode(substr(base64_decode($hassedPasswordInDB), 6));
$sSecretSalt = substr($sPasswordHash, 20); // $sSecretSalt is ��8 here
$sPasswordHash = substr($sPasswordHash, 0, 20);
$hashedPassword = sha1($password.$sSecretSalt); // $hashedPassword is 7a59b9888af6355119cf369fd3dbf2810697b981

And I wrote code for java too;我也为 java 编写了代码;

    String password = "pass123";
    String hassedPasswordInDB = "e1NTSEF9ZWxtNWlJcjJOVkVaenphZjA5dnlnUWFYdVlHaEU3dzQ=";
    String subFirstDecode = new String(Base64Coder.decode(hassedPasswordInDB)).substring(6);
    String sPasswordHash = new String(Base64Coder.decode(subFirstDecode));
    String secretSalt = sPasswordHash.substring(19); // secretSalt is ��8
    sPasswordHash = sPasswordHash.substring(0, 19);
    String hashedPassword = sha1(password + secretSalt); //hashedPassword is b15fe1e7e0abce8284d3695af6c57d7540387ae4

And Java sha1 method;和Java sha1方法;

private static String sha1(String input) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
        digest.update(input.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++)
            hexString.append(String.format("%02X", 0xFF & messageDigest[i]));
        return hexString.toString().toLowerCase();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

As a result,when i tried with Utf-8 string like "abcabc" it works but same codes with complicated inputs,which is like "��8", gives two different outputs.结果,当我尝试使用 Utf-8 字符串(例如“abcabc”)时,它可以工作,但是具有复杂输入的相同代码(例如“��8”)会给出两个不同的输出。

Java : b15fe1e7e0abce8284d3695af6c57d7540387ae4,
PHP  : 7a59b9888af6355119cf369fd3dbf2810697b98

Why this is happening?为什么会这样?

The problem is in your Java code: while in PHP a string is just a sequence of bytes (cf. documentation ), in Java it is a sequence of characters .问题出在您的 Java 代码中:而在 PHP 中,字符串只是一个字节序列(参见文档),在 Java 中它是一个字符序列。 Characters in Java are UTF-16 code points. Java 中的字符是 UTF-16 代码点。

All transformations between byte[] and String use an encoding, so you should avoid such transformations if you can and specify the encoding when you do (use the String(byte[], Charset) constructor or equivalent). byte[]String之间的所有转换都使用编码,因此您应该尽可能避免此类转换,并在这样做时指定编码(使用String(byte[], Charset)构造函数或等效项)。 Otherwise you'll end up using your system's default encoding (probably UTF-8 in your case).否则,您最终将使用系统的默认编码(在您的情况下可能是 UTF-8)。

What happens in your case is:您的情况是:

  • the sPasswordHash string has length 23, while the original byte array had 24 bytes: there are a lot of encoding errors, so many sequences of bytes are replaced by the replacement character , sPasswordHash字符串的长度为 23,而原始字节数组有 24 个字节:有很多编码错误,所以很多字节序列被替换字符 替换
  • to compensate you call sPassword.substring(19) (should be 20), which excludes the first 19 characters.为了补偿你调用sPassword.substring(19) (应该是 20),它不包括前 19 个字符。 You end up with a slightly different salt.你最终会得到一种稍微不同的盐。

You should rather work on byte arrays: it is less comfortable, but safer.您应该使用byte arrays:它不太舒服,但更安全。

      String password = "pass123";
      String hashedPasswordInDB = "e1NTSEF9ZWxtNWlJcjJOVkVaenphZjA5dnlnUWFYdVlHaEU3dzQ=";
      // This should be ASCII
      final String firstDecode = new String(Base64.getDecoder().decode(hashedPasswordInDB), StandardCharsets.US_ASCII);
      final byte[] hashAndSalt = Base64.getDecoder().decode(firstDecode.substring("{SSHA}".length()));
      final byte[] passwordHash = Arrays.copyOf(hashAndSalt, 20);
      final byte[] salt = Arrays.copyOfRange(hashAndSalt, 20, hashAndSalt.length);
      final byte[] passwordBytes = password.getBytes();
      final byte[] passwordAndSalt = Arrays.copyOf(passwordBytes, passwordBytes.length + salt.length);
      System.arraycopy(salt, 0, passwordAndSalt, passwordBytes.length, salt.length);
      System.out.println(sha1(passwordAndSalt));

(I slightly modified the sha1 method to accept a byte array instead of String ) (我稍微修改了sha1方法以接受字节数组而不是String

Remark: Since Java 8 there is a base 64 encoder/decoder in the JRE (cf.Base64 ).备注:自 Java 8 以来,JRE 中有一个 base 64 编码器/解码器(参见Base64 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM