我们的一个客户将在我们的应用程序中上传一个加密文件,我们将该文件移动到我们的 AIX 服务器并对其进行解密。该文件未正确解密。如果我通过 SFTP 手动将加密文件移动到 AIX 服务器,则解密工作完美,但当文件从 Windows 上传时就不行了。客户端加密和我们解密的密钥相同。但是,并非所有加密文件都发生这种情况,少数文件存在问题,我们在这些文件中没有发现任何不同之处。我们使用 Bouncy Castle 生成密钥以及加密和解密。
例外:
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(Unknown Source)
解密代码:
public void decryptFile(InputStream fis, OutputStream fos)
throws IOException,
ShortBufferException,
IllegalBlockSizeException,
BadPaddingException
{
byte[] buffer = new byte[16];
int noBytes = 0;
byte[] cipherBlock =
new byte[decryptCipher.getOutputSize(buffer.length)];
int cipherBytes;
while((noBytes = fis.read(buffer))!=-1)
{
cipherBytes =
decryptCipher.update(buffer, 0, noBytes, cipherBlock);
fos.write(cipherBlock, 0, cipherBytes);
}
cipherBytes = decryptCipher.doFinal(cipherBlock,0);
fos.write(cipherBlock,0,cipherBytes);
fos.close();
fis.close();
}
我们如何处理这个?