Java 为什么我会得到';BadPaddingException';什么时候解密?
以下是我的加密设置:Java 为什么我会得到';BadPaddingException';什么时候解密?,java,encryption,aes,padding,Java,Encryption,Aes,Padding,以下是我的加密设置: public static String encryptionAlgorithm = "AES"; public static short encryptionBitCount = 256; public static int encryptionMessageLength = 176; public static String hashingAlgorithm = "PBEWITHSHAAND128BITAES-CBC-BC"; //PBEWithSHA25
public static String encryptionAlgorithm = "AES";
public static short encryptionBitCount = 256;
public static int encryptionMessageLength = 176;
public static String hashingAlgorithm = "PBEWITHSHAAND128BITAES-CBC-BC";
//PBEWithSHA256And256BitAES-CBC-BC"PBEWithMD5AndDES";//"PBKDF2WithHmacSHA1";
public static short hashingCount = 512;
public static String cipherTransformation = "AES/CBC/PKCS5Padding";
以下是我要解密的代码:
public byte[] readMessage () throws Exception
{
byte[] iv = new byte[16];
byte[] message = new byte[EncryptionSettings.encryptionMessageLength];
try
{
// read IV from stream
if (stream.read(iv) != 16)
throw new Exception("Problem receiving full IV from stream");
}
catch (final IOException e)
{
throw new Exception("Unable to read IV from stream");
}
try
{
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
}
catch (final InvalidKeyException e)
{
throw new Exception("Invalid key");
}
catch (final InvalidAlgorithmParameterException e)
{
throw new Exception("Invalid algorithm parameter");
}
try
{
//read message from stream
if (stream.read(message) != EncryptionSettings.encryptionMessageLength)
throw new Exception("Problem receiving full encrypted message from stream");
}
catch (final IOException e)
{
throw new Exception("Unable to read message from stream");
}
try
{
return cipher.doFinal(message); //decipher message and return it.
}
catch (IllegalBlockSizeException e)
{
throw new Exception("Unable to decrypt message due to illegal block size - "
+ e.getMessage());
}
catch (BadPaddingException e)
{
throw new Exception("Unable to decrypt message due to bad padding - "
+ e.getMessage());
}
}
这是我要加密的代码:
public void writeMessage (final byte[] message) throws Exception
{
try
{
// write iv
byte b[] = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
System.out.println(b.length);
stream.write(b);
}
catch (final InvalidParameterSpecException e)
{
throw new Exception("Unable to write IV to stream due to invalid"+
" parameter specification");
}
catch (final IOException e)
{
throw new Exception("Unable to write IV to stream");
}
try
{
// write cipher text
byte b[] = cipher.doFinal(message);
System.out.println(b.length);
stream.write(b);
}
catch (final IllegalBlockSizeException e)
{
throw new Exception("Unable to write cipher text to stream due to "+
"illegal block size");
}
catch (final BadPaddingException e)
{
throw new Exception("Unable to write cipher text to stream due to " +
"bad padding");
}
catch (final IOException e)
{
throw new Exception("Unable to write cipher text to stream");
}
}
错误:由于填充错误而无法解密消息-null。
我在解密时遇到一个BadPaddingException,为什么?消息正好是168个字符,填充后为176个字符(可被16整除)来自我的初始评论: 一个典型的场景是钥匙与另一端使用的钥匙不同。这是最可能的原因,但您可能还需要检查处理流的方式,因为您确实缺少.close()和.flush()语句。您还假设始终可以将所有数据读入缓冲区,但情况可能并非如此
钥匙确实计算错误。从我最初的评论来看: 一个典型的场景是钥匙与另一端使用的钥匙不同。这是最可能的原因,但您可能还需要检查处理流的方式,因为您确实缺少.close()和.flush()语句。您还假设始终可以将所有数据读入缓冲区,但情况可能并非如此
密钥的计算确实不正确。加密/解密中的BadPaddingException错误 我遇到了这样的错误,但这对我有帮助
希望它也能对您有所帮助。加密/解密中的BadPaddingException错误 我遇到了这样的错误,但这对我有帮助
希望它也能帮助您。此外,您的异常处理并不是真正的“处理”。。。您只是将堆栈跟踪的一部分扔掉,以交换一个不太具描述性的异常类型和一个稍微更具描述性的消息。我应该指出,我只选择了关键方法,所有内容都正确打开/关闭。密钥不能不同,因为它内置于服务器和客户端(目前-显然这会改变)。我们的信息太少,无法编译,因为我们没有代码,例如流的代码。我认为一切都很好,但与此同时,您的代码将无法运行。所以这是不正确的。不管怎么说,问题是你挑出来的——钥匙不一样。当我把md5hash(password+salt)作为加密密码读入时,它也读入了它后面的注释中。。。您只是将堆栈跟踪的一部分扔掉,以交换一个不太具描述性的异常类型和一个稍微更具描述性的消息。我应该指出,我只选择了关键方法,所有内容都正确打开/关闭。密钥不能不同,因为它内置于服务器和客户端(目前-显然这会改变)。我们的信息太少,无法编译,因为我们没有代码,例如流的代码。我认为一切都很好,但与此同时,您的代码将无法运行。所以这是不正确的。不管怎么说,问题是你挑出来的——钥匙不一样。当我在读md5hash(password+salt)作为加密密码时,它也在读后面的注释。