Java的MessageDigest SHA1算法返回的结果与php的SHA1函数不同 [英] Java's MessageDigest SHA1-algorithm returns different result than SHA1-function of php

查看:435
本文介绍了Java的MessageDigest SHA1算法返回的结果与php的SHA1函数不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有用户名和密码的SQL表。密码使用MessageDigest的digest()方法进行编码。如果我编码密码 - 让我们说abcdef12 - 使用MessageDigest的digest()方法然后将其转换为十六进制值,则String与使用PHP的SHA1方法执行相同操作时不同。我希望这些值完全相同。

I have a SQL table with usernames and passwords. The passwords are encoded using MessageDigest's digest() method. If I encode a password - let's say "abcdef12" - with MessageDigest's digest() method and then convert it to hexadecimal values, the String is different than if I do the same using PHP's SHA1-method. I'd expect these values to be exactly the same though.

用于对密码进行编码的代码:

Code that is used to encode the passwords:

MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] passbyte;
passbyte = "abcdef12".getBytes("UTF-8");
passbyte = md.digest(passbyte);

使用此方法将String转换为十六进制:

The conversion of the String to hexadecimal is done using this method:

public static String convertStringToHex(String str) {

    char[] chars = str.toCharArray();

    StringBuffer hex = new StringBuffer();
    for (int i = 0; i < chars.length; i++) {
        hex.append(Integer.toHexString((int) chars[i]));
    }

    return hex.toString();
}

密码: abcdef12

这是许多SHA1-hash在线生成器和PHP SHA1()返回的密码 - 函数: d253e3bd69ce1e7ce6074345fd5faa1a3c2e89ef

Here's the password as returned by a lot of SHA1-hash online generators and PHP SHA1()-function: d253e3bd69ce1e7ce6074345fd5faa1a3c2e89ef

这是MessageDigest编码的密码: d253e3bd69ce1e7ce674345fd5faa1a3c2e2030ef

Here's the password as encoded by MessageDigest: d253e3bd69ce1e7ce674345fd5faa1a3c2e2030ef

我忘了什么?

Igor。

编辑:我发现有类似问题的人: C#SHA-1与PHP SHA-1 ......不同的结果?。解决方案是更改编码..但我无法更改服务器端的编码,因为该SQL表中的密码不是由我的应用程序创建的。
我使用JavaScript SHA1类(更确切地说是Google Web Toolkit类)使用客户端SHA1编码。它按预期工作和编码字符串,但显然使用ASCII字符?..

I've found someone with a similar problem: C# SHA-1 vs. PHP SHA-1...Different Results? . The solution was to change encodings.. but I can't change encodings on the server-side since the passwords in that SQL-table are not created by my application. I use client-side SHA1-encoding using a JavaScript SHA1-class (more precisely: a Google Web Toolkit-class). It works and encodes the string as expected, but apparently using ASCII characters?..

推荐答案

它与编码无关。输出完全不同。

It has nothing to do with the encodings. The output would be entirely different.

对于初学者,你的函数 convertStringToHex()不会输出前导零,也就是说, 07 变为 7

For starters, your function convertStringToHex() doesn't output leading zeros, that is, 07 becomes just 7.

休息(将 89 更改为 2030 )也可能与该功能有关。尝试在 passbyte = md.digest(passbyte);

The rest (changing 89 to 2030) is also likely to have something to do with that function. Try looking at the value of passbyte after passbyte = md.digest(passbyte);.

这篇关于Java的MessageDigest SHA1算法返回的结果与php的SHA1函数不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆