Java和PHP中的SHA1结果不同 [英] SHA1 in Java and PHP with different results

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

问题描述

我知道周围有几个问题,但是我尝试了在stackoverflow上发现的每个解决方案,但仍然没有得到预期的结果.

I know there are several questions like this around, but I tried every single solution I found on stackoverflow and I still haven't got the expected result.

我正在尝试在Java和PHP中将字符串转换为sha1,但是得到的结果却有所不同.该字符串是随机生成的.我检查了两端的字符串,它们是相同的(甚至尝试使用在线比较工具).

I'm trying to convert a string to sha1 in Java and PHP, but I'm getting different results. The string is generated randomly. I checked the string on both ends and they are the same (even trying a online comparison tool).

这是我在另一个应用程序中使用的相同代码,并且可以在其中运行,但在这种情况下不可用.

This is the same code I use in another app and it's working there, but not in this case.

我尝试用sha1哈希的一个字符串是: UgJaDVYEClRUD1cAAVUBVwRTB1MDAA9SBgcDBwNXAwNZBQdUAAACBA ==

One string I tried to hash with sha1 is: UgJaDVYEClRUD1cAAVUBVwRTB1MDAA9SBgcDBwNXAwNZBQdUAAACBA==

Java结果: 72c9bbe7eed0efe5e82ea9568136d8f52347259e

PHP结果: f720d73d18a7bb9cf36808af17ce40621ebfb405

Java代码

public static String sha1(String toHash)
{
    String hash = null;
    try
    {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        byte[] bytes = toHash.getBytes("ASCII"); //I tried UTF-8, ISO-8859-1...
        digest.update(bytes, 0, bytes.length);
        bytes = digest.digest();
        StringBuilder sb = new StringBuilder();
        for(byte b : bytes)
        {
            sb.append(String.format("%02X", b));
        }
        hash = sb.toString();
    }
    catch(NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }
    catch(UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }
    return hash.toLowerCase(Locale.ENGLISH);
}

PHP代码

sha1("UgJaDVYEClRUD1cAAVUBVwRTB1MDAA9SBgcDBwNXAwNZBQdUAAACBA==");

任何帮助将不胜感激

更新

在Java&中PHP,我正在执行以下操作:

In Java & PHP I was doing the following:

Java

String toHash = "qwerty";
String hash = sha1(toHash); //Prints: b1b3773a05c0ed0176787a4f1574ff0075f7521e

toHash = Base64.encodeToString(toHash.getBytes("ASCII"), Base64.DEFAULT);
hash = sha1(toHash); //Prints: 88bfb2d77c3b42823bab820c1737f03c97d87c1b

PHP

$toHash = "qwerty";
sha1($toHash); //Prints: b1b3773a05c0ed0176787a4f1574ff0075f7521e

sha1(base64_encode($toHash)); //Prints: 278aa0e8dde2af58a4eed613467da219a35c5278

我猜想Base64编码对PHP和Java上的字符串有不同的影响,为什么呢?

I guess that the Base64 encoding is doing something to the string that is different on PHP and Java, any thoughts on why?

更新2

我应该更清楚一些,对不起,我的意思是:

I should have been more clearer, sorry for that, what I mean is:

的输出 Java

sha1(Base64.encodeToString("qwerty".getBytes("ASCII"), Base64.DEFAULT));

与的输出不同 PHP

sha1(base64_encode("qwerty"));

更新3

尽管两个base64编码的字符串都等于 cXdlcnR5 .

although both base64 encoded string are equal cXdlcnR5.

基本上:

- sha1("qwerty") == sha1("qwerty")
- Base64.encodeToString("qwerty".getBytes(), Base64.DEFAULT) == base64_encode("qwerty")
- sha1(Base64.encodeToString("qwerty".getBytes(), Base64.DEFAULT)) != sha1(base64_encode("qwerty"))

我已经在散列的字符串上删除了base64编码,但是我仍然想知道我可以做些什么来使其工作.

I already dropped the base64 encoding on the strings that I hash, but I still would like to know what I could have done to make it work.

推荐答案

三年后,我遇到了同样的问题,但是这次我发现了问题所在.这是为偶然遇到此问题的任何人提供的解决方案:

Over 3 years later I ran into the same issue, but this time I figured out the problem. Here is the solution to anyone that stumbles upon this question:

我正在使用:

sha1("qwerty") == sha1("qwerty")
Base64.encodeToString("qwerty".getBytes(), Base64.DEFAULT) == base64_encode("qwerty")
sha1(Base64.encodeToString("qwerty".getBytes(), Base64.DEFAULT)) != sha1(base64_encode("qwerty"))

此问题是 Base64.DEFAULT ,Base64的默认行为是将字符串包装(将 \ n 添加到字符串).为了获得与PHP方法相同的结果,您应该使用 Base64.NO_WRAP :

The problem with this is the Base64.DEFAULT, the default behavior of Base64 wraps the string (adds \n to string). In order to get the same result as the PHP method you should use Base64.NO_WRAP:

sha1("qwerty") == sha1("qwerty")
Base64.encodeToString("qwerty".getBytes(), Base64.NO_WRAP) == base64_encode("qwerty")
sha1(Base64.encodeToString("qwerty".getBytes(), Base64.NO_WRAP)) == sha1(base64_encode("qwerty"))

进行此更改后,它开始起作用

After I made this change it started to work

这篇关于Java和PHP中的SHA1结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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