我正在尝试创建一个glassfish自定义的JDBCRealm,在对它进行一些测试时,我在使用JDBCRealm函数时得到了一个MalformatedInputException。
因此,我决定将函数中抛出此错误的部分具体化,以测试它并了解它的起始位置。
恢复职能:
public void justATestFunction()
throws Exception
{
final char[] password = "myP4ssW0rd42".toCharArray();
MessageDigest md = MessageDigest.getInstance("SHA-256");
// according to the Utility doc, if the Charset parameter is null or empty,
// it will call the Charset.defaultCharset() function to define the charset to use
byte[] hashedPassword= Utility.convertCharArrayToByteArray(password, null);
hashedPassword = md.digest(hashedPassword);
Utility.convertByteArrayToCharArray(hashedPassword, null); // throw a MalformatedInputException
}提前谢谢你的答复。
发布于 2014-06-11 18:12:43
让我们看看每一步:
byte[] hashedPassword= Utility.convertCharArrayToByteArray(password, null);上面的Unicode-16字符使用默认的端编码(可能是WIN-1252或UTF-8 )将字节数组转换为字节数组。由于密码不包含标准7位ASCII以外的任何内容,因此无论编码结果都是相同的。
hashedPassword = md.digest(hashedPassword);hashedPassword现在引用一个完全不同的字节数组,其中包含原始密码的二进制摘要。这是一个二进制字符串,不再代表任何字符编码。它是纯二进制数据。
Utility.convertByteArrayToCharArray(hashedPassword, null);现在,您尝试“解码”二进制字符串,就好像它是用默认字符集编码的一样,这无疑会引发异常。
我怀疑您确实希望显示摘要的十六进制表示形式,或者显示基-64版本。在任何一种情况下,你所做的都是行不通的。
因为你还没有解释你想要实现什么,这是任何人都能做的最好的事情。
https://stackoverflow.com/questions/24169325
复制相似问题