2

I'm new in cryptography and my scala code is not able to decrypt a file sized over 1.6Gb, i'm getting a BadPaddingException and it decrypt successfully with files sized less then that.
I know that smime decryption using openssl cli has a limit size of 1.5 Gb because of the LIMIT_BEFORE_EXPANSION hard coded value. But i don't think that bouncycastle has the same limit. maybe i'm doing something wrong. Any help is appreciated, thanks!

Knowing that i used openssl cli to :

  • generate private key (private_key.pem) and public key certificate (certificate.pem) :

openssl req -x509 -nodes -days 100000 -newkey rsa:8912 -keyout private_key.pem -out certificate.pem

  • encrypt files with smime command:

openssl smime -encrypt -aes-256-cbc -in big_file.json -out big_file.json.enc -outform DER certificate.pem

  • decrypt file using scala function :
 def decrypt(privateKey: PrivateKey, encryptedData: InputStream, decryptedDestination: File): Unit = {
         val encryptedDataBuffer = new BufferedInputStream(encryptedData, (16 * 1024))
         val parser = new CMSEnvelopedDataParser(encryptedDataBuffer)
         val recInfo = getSingleRecipient(parser)
         val recipient = new JceKeyTransEnvelopedRecipient(privateKey)
         val decryptedStream = recInfo.getContentStream(recipient).getContentStream
         try {
           Files.copy(decryptedStream, decryptedDestination.toPath)
         } finally {decryptedStream.close()}
       }
  • the exception throwed :
Error finalising cipher
org.bouncycastle.crypto.io.InvalidCipherTextIOException: Error finalising cipher
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
    at com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:975)
    at com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1056)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
    at javax.crypto.Cipher.doFinal(Cipher.java:2051)
    ... 68 more
khalilosgh
  • 21
  • 2

0 Answers0