使用PHP方法解密Java中的哈希码

时间:2018-09-12 15:41:56

标签: java php encryption

我需要在Java中做类似的事情

final String secretKey = "****";
final String secretIv = "****";

final String decryptedString = decrypt(decodedHashCode, secretKey, secretIv);

我知道 string 是一个包含一些信息的哈希码,我知道 secret_key secret_iv (都是字符串)。

我在此处尝试了许多示例,但是没有人能正常工作。

我试图自己做。但是我需要一个执行 openssl_decrypt 的函数。这是我现在的代码:

private String decrypt(final String hashCode, final String key, final String iv) throws Exception
{
    InputStream cipherInputStream = null;
    try
    {
        final StringBuilder output = new StringBuilder();
        final byte[] secretKey = DigestUtils.sha256Hex(key).getBytes();
        final byte[] initVector = DigestUtils.sha256Hex(iv).substring(0, 16).getBytes();
        final byte[] decodedBytes = Base64.getDecoder().decode(hashCode);
        final String decodedString = new String(decodedBytes);
        final Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey, "AES"),
                new IvParameterSpec(initVector, 0, cipher.getBlockSize()));
        cipherInputStream = new CipherInputStream(new FileInputStream(decodedString), cipher);

        final String charsetName = "UTF-8";

        final byte[] buffer = new byte[8192];
        int read = cipherInputStream.read(buffer);

        while (read > -1)
        {
            output.append(new String(buffer, 0, read, charsetName));
            read = cipherInputStream.read(buffer);
        }

        System.out.println(output);
        return output.toString();
    }
    finally
    {
        if (cipherInputStream != null)
        {
            cipherInputStream.close();
        }
    }

}

我正在尝试编写解密函数。

我写了这个:

ID   | swimming | running | rock climbing | learning to program.

user1       T         F            T                  T

user2       F         T            T                  F 

但是在密码初始化期间出现错误。 错误为: java.security.InvalidKeyException:无效的AES密钥长度:64个字节

如何用Java做到这一点?

0 个答案:

没有答案