Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java散列自";PBKDF2带HMACSHA512“;is不同于python密码(digest#u alg=';pbkdf2(1000,20,sha512)';salt=True)(密码)[0])_Java_Python_Sha512_Pbkdf2 - Fatal编程技术网

java散列自";PBKDF2带HMACSHA512“;is不同于python密码(digest#u alg=';pbkdf2(1000,20,sha512)';salt=True)(密码)[0])

java散列自";PBKDF2带HMACSHA512“;is不同于python密码(digest#u alg=';pbkdf2(1000,20,sha512)';salt=True)(密码)[0]),java,python,sha512,pbkdf2,Java,Python,Sha512,Pbkdf2,我有一个数据库,其中包含使用以下python代码散列的密码: result = str(CRYPT(digest_alg='pbkdf2(1000,20,sha512)', salt=True)(password)[0]) (详情请参阅) 它为password='123'生成 pbkdf2(1000,20,sha512)$b3c56f341284f4be$54297564f7a3be8c6e9c10b27821f8105e0a8120 我需要使用java验证密码。我使用以下代码: v

我有一个数据库,其中包含使用以下python代码散列的密码:

result = str(CRYPT(digest_alg='pbkdf2(1000,20,sha512)', salt=True)(password)[0])
(详情请参阅)

它为password='123'生成

pbkdf2(1000,20,sha512)$b3c56f341284f4be$54297564f7a3be8c6e9c10b27821f8105e0a8120
我需要使用java验证密码。我使用以下代码:

    validatePassword("123", "pbkdf2(1000,20,sha512)$b3c56f341284f4be$54297564f7a3be8c6e9c10b27821f8105e0a8120");



    private static boolean validatePassword(String originalPassword, String storedPassword) throws NoSuchAlgorithmException, InvalidKeySpecException
    {
        String[] parts = storedPassword.split("\\$");
        byte[] salt = fromHex(parts[1]);
        byte[] hash = fromHex(parts[2]);

        PBEKeySpec spec = new PBEKeySpec(originalPassword.toCharArray(), salt, 1000, hash.length * 8);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
        byte[] testHash = skf.generateSecret(spec).getEncoded();

        System.out.println(toHex(testHash));
        System.out.println(toHex(hash));

        return true;
    }


    private static byte[] fromHex(String hex) throws NoSuchAlgorithmException
    {
        byte[] bytes = new byte[hex.length() / 2];
        for(int i = 0; i<bytes.length ;i++)
        {
            bytes[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
        }
        return bytes;
    }

    private static String toHex(byte[] array)
    {
        StringBuilder sb = new StringBuilder();
        for(int i=0; i< array.length ;i++)
        {
            sb.append(Integer.toString((array[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }
请帮帮我,我做错了什么?

web2py的代码中有一种“bug”

散列看起来像一个十六进制字符串,但它被发送到hashlib.pbkdf2_hmac(openssl方法的代理)作为十六进制字符串的字符表示。意思是你不应该使用

byte[] salt = fromHex(parts[1]);
但是

此外,您需要将KEYLENGTH而不是salt长度传递到PBEKeySpec的构造函数中

更正部分应为:

byte[] salt = parts[1].getBytes("utf-8");
byte[] hash = fromHex(parts[2]);
PBEKeySpec spec = new PBEKeySpec(originalPassword.toCharArray(), salt, 1000, 20*8);

替换它,代码就会工作。花了一段时间才发现这一点;)

我看不出任何人在不知道什么是
CRYPT
的情况下,怎么可能回答这个问题。class glion.validators.CRYPT(key=None,digest_alg='pbkdf2(1000,20,sha512'),min_length=0,error_message='Too short',salt=True,max_length=1024)你可以在这里找到它,非常感谢!!!它真的有效!我为这个bug道歉,因为我是Java新手。不需要任何借口,@krtl-你没有bug,看起来来自Web2py的Python代码有一个;)是的,我已经明白了,只是不久之后。不管怎样,你太棒了!再次感谢你!
byte[] salt = parts[1].getBytes("utf-8");
byte[] salt = parts[1].getBytes("utf-8");
byte[] hash = fromHex(parts[2]);
PBEKeySpec spec = new PBEKeySpec(originalPassword.toCharArray(), salt, 1000, 20*8);