One of our clients will upload a ENCRYPTED file in our application and we will move that file to our AIX server and decrypt it. The file is not getting decrypted properly. If I manually move the encrypted file through SFTP to the AIX server, decryption works perfectly but not when then file is uploaded from Windows. The key is same for encryption by the client and decryption by us. This is not happening for all the encrypted files though, problem for few files and we dont find anything different in those files. We use Bouncy Castle for generating the key as well as for encryption and decryption.
Exception:
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)
Decrypt Code:
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();
}
How do we handle this?