You should close the output stream immediately

 wb.write(tempData.getOutputStream)

You should not leave the output stream open while then turning around and 
trying to read from the tempData.






On Wednesday 11 May 2022, 11:58:25 GMT+2, Juha Rantanen 
<juha.ranta...@reaktor.com.invalid> wrote: 





Hi,

I'm trying to upgrade poi-ooxml version 3.17. I encountered an error:
"java.io.IOException: javax.crypto.BadPaddingException: Given final block
not properly padded. Such issues can arise if a bad key is used during
decryption." I have tried to upgrade to all possible versions starting from
4.0.0, also versions 5.0.0 and 5.2.2 without success. All versions above
4.0.0 causes same error. The code we use has not changed, it has been
working properly with version 3.17 and below. Below is the code we use. The
failure happens at "val opc = OPCPackage.open(tempData.getInputStream)". I
have commented out all data writing to see that this is not data related.

  def writeExcel(workbookSettings: WorkbookSettings, sheets: Seq[Sheet],
out: OutputStream): Unit = {

    val wb = if (workbookSettings.password.isEmpty) new SXSSFWorkbook else
new SXSSFWorkbookWithCustomZipEntrySource
    try {
      val coreProps = wb.getXSSFWorkbook.getProperties.getCoreProperties
      coreProps.setTitle(workbookSettings.title)
      coreProps.setCreator("Koski")
//      sheets.foreach { sheet =>
//        val sh = createSheet(sheet, wb)
//        sheet match {
//          case ds: SheetWithColumnSettings => writeDataSheet(wb, sh, ds)
//          case ds: DocumentationSheet => writeDocumentationSheet(wb, sh,
ds)
//        }
//      }
      if (workbookSettings.password.isEmpty) {
        wb.write(out)
      } else {
        // based on
https://github.com/apache/poi/blob/f509d1deae86866ed531f10f2eba7db17e098473/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java
        val tempData = new EncryptedTempData
        try {
          wb.write(tempData.getOutputStream)
*          val opc = OPCPackage.open(tempData.getInputStream) //FAILURE
HAPPENS HERE*
          val fs = new POIFSFileSystem
          val enc = Encryptor.getInstance(new
EncryptionInfo(EncryptionMode.agile))
          enc.confirmPassword(workbookSettings.password.get)
          opc.save(enc.getDataStream(fs))
          fs.writeFilesystem(out)
        } finally {
          tempData.dispose()
        }
      }
      out.close()
    } finally {
      // deletes temporary files from disk
      wb.dispose()
    }
  }

To see the whole code you can checkout our public repository and the file
above:
https://github.com/Opetushallitus/koski/blob/snyk-fix-892cc871099e9787cbc96ed1b5c58ba4/src/main/scala/fi/oph/koski/raportit/ExcelWriter.scala

And the branch is here including the code change I made to try to simplify
the error: https://github.com/Opetushallitus/koski/pull/1100

The error can be reproduced with the test found in here:
https://github.com/Opetushallitus/koski/blob/snyk-fix-892cc871099e9787cbc96ed1b5c58ba4/src/test/scala/fi/oph/koski/raportit/ExcelWriterSpec.scala

The full stacktrace of the error is:

An unexpected java.io.IOException was thrown.
ScalaTestFailureLocation: org.scalatest.matchers.MatchersHelper$ at
(ExcelWriterSpec.scala:21)
org.scalatest.exceptions.TestFailedException: An unexpected
java.io.IOException was thrown.
at
org.scalatest.matchers.MatchersHelper$.indicateFailure(MatchersHelper.scala:402)
at
org.scalatest.matchers.dsl.NoExceptionWord.shouldBe(NoExceptionWord.scala:58)
at
fi.oph.koski.raportit.ExcelWriterSpec.$anonfun$new$2(ExcelWriterSpec.scala:22)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at
org.scalatest.freespec.AnyFreeSpecLike$$anon$1.apply(AnyFreeSpecLike.scala:430)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
at org.scalatest.freespec.AnyFreeSpec.withFixture(AnyFreeSpec.scala:1739)
at
org.scalatest.freespec.AnyFreeSpecLike.invokeWithFixture$1(AnyFreeSpecLike.scala:428)
at
org.scalatest.freespec.AnyFreeSpecLike.$anonfun$runTest$1(AnyFreeSpecLike.scala:440)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
at org.scalatest.freespec.AnyFreeSpecLike.runTest(AnyFreeSpecLike.scala:440)
at
org.scalatest.freespec.AnyFreeSpecLike.runTest$(AnyFreeSpecLike.scala:422)
at org.scalatest.freespec.AnyFreeSpec.runTest(AnyFreeSpec.scala:1739)
at
org.scalatest.freespec.AnyFreeSpecLike.$anonfun$runTests$1(AnyFreeSpecLike.scala:499)
at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:413)
at scala.collection.immutable.List.foreach(List.scala:431)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401)
at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:390)
at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:427)
at scala.collection.immutable.List.foreach(List.scala:431)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401)
at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:396)
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:475)
at
org.scalatest.freespec.AnyFreeSpecLike.runTests(AnyFreeSpecLike.scala:499)
at
org.scalatest.freespec.AnyFreeSpecLike.runTests$(AnyFreeSpecLike.scala:498)
at org.scalatest.freespec.AnyFreeSpec.runTests(AnyFreeSpec.scala:1739)
at org.scalatest.Suite.run(Suite.scala:1112)
at org.scalatest.Suite.run$(Suite.scala:1094)
at org.scalatest.freespec.AnyFreeSpec.org
$scalatest$freespec$AnyFreeSpecLike$$super$run(AnyFreeSpec.scala:1739)
at
org.scalatest.freespec.AnyFreeSpecLike.$anonfun$run$1(AnyFreeSpecLike.scala:543)
at org.scalatest.SuperEngine.runImpl(Engine.scala:535)
at org.scalatest.freespec.AnyFreeSpecLike.run(AnyFreeSpecLike.scala:543)
at org.scalatest.freespec.AnyFreeSpecLike.run$(AnyFreeSpecLike.scala:542)
at org.scalatest.freespec.AnyFreeSpec.run(AnyFreeSpec.scala:1739)
at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
at
org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13(Runner.scala:1322)
at
org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13$adapted(Runner.scala:1316)
at scala.collection.immutable.List.foreach(List.scala:431)
at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1316)
at
org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24(Runner.scala:993)
at
org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24$adapted(Runner.scala:971)
at
org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1482)
at
org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:971)
at org.scalatest.tools.Runner$.run(Runner.scala:798)
at org.scalatest.tools.Runner.run(Runner.scala)
at
org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:133)
at
org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:27)
Caused by: java.io.IOException: javax.crypto.BadPaddingException: Given
final block not properly padded. Such issues can arise if a bad key is used
during decryption.
at javax.crypto.CipherInputStream.getMoreData(CipherInputStream.java:128)
at javax.crypto.CipherInputStream.read(CipherInputStream.java:246)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at java.io.PushbackInputStream.read(PushbackInputStream.java:139)
at
org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.readOneByte(ZipArchiveInputStream.java:1213)
at
org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.findEocdRecord(ZipArchiveInputStream.java:1149)
at
org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.skipRemainderOfArchive(ZipArchiveInputStream.java:1127)
at
org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.getNextZipEntry(ZipArchiveInputStream.java:293)
at
org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.getNextEntry(ZipArchiveThresholdInputStream.java:152)
at
org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:94)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:132)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:312)
at fi.oph.koski.raportit.ExcelWriter$.writeExcel(ExcelWriter.scala:45)
at fi.oph.koski.raportit.ExcelWriterSpec.withExcel(ExcelWriterSpec.scala:48)
at
fi.oph.koski.raportit.ExcelWriterSpec.$anonfun$new$3(ExcelWriterSpec.scala:22)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
at
org.scalatest.matchers.should.Matchers.$anonfun$thrownBy$1(Matchers.scala:3029)
at
org.scalatest.matchers.dsl.ResultOfThrownByApplication.execute(ResultOfThrownByApplication.scala:30)
at
org.scalatest.matchers.dsl.NoExceptionWord.shouldBe(NoExceptionWord.scala:52)
... 50 more
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)
at javax.crypto.CipherInputStream.getMoreData(CipherInputStream.java:125)
... 70 more

Any ideas what goes wrong in here?

Br,
Juha

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@poi.apache.org
For additional commands, e-mail: user-h...@poi.apache.org

Reply via email to