尝试解密已加密的文件时,当前出现AEADBadTagException异常。我在stackoverflow上的几乎所有地方都进行了搜索,无法找到解决方案,并且希望这只是我犯的一个小错误,或者与编码等有关,因为GCM无法验证其生成的标记。
我相信问题出在我要加密/解密的文件中。相同的精确代码可在图像上使用,但是,当我尝试加密PDF时,它会失败并出现上述错误。
下面的代码没有使用CipherOutputStream / CipherInputStream,但是我都没有运气尝试过。
我知道加密/解密方法不应该这样写,尤其是对于硬编码的IV,但是现在我只是想使其工作,然后在以后适当地保护这些方法。
我正在使用Android KeyStore来获取我的 secret 密钥。我知道这部分是可行的,因为我在应用程序中还有很多其他使用Keystore的方法。另外,此方法适用于图像。
该错误发生在cipher.doFinal(encryptedBytes)上。当我使用CipherInputStream时,它发生在CipherInputStream(EncryptedFileStream,cipher)上
这是代码以及错误堆栈,非常感谢您的帮助:
加密
val fileBytes = inputStream.readBytes()
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
keyStoreService.checkKeyAndCreate(ALIAS_FILE_KEY)
val key = keyStoreService.getFileKey(ALIAS_FILE_KEY)
val iv = byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
cipher.init(Cipher.ENCRYPT_MODE, key, GCMParameterSpec(128, iv))
val encryptedBytes = cipher.doFinal(fileBytes)
outputStream = FileOutputStream(file)
outputStream.write(encryptedBytes)
outputStream.flush()
inputStream.close()
outputStream.close()
解密
val encryptedFile = File(filePath)
val encryptedBytes = encryptedFile.readBytes()
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
val key = keyStoreService.getFileKey(ALIAS_FILE_KEY)
val iv = byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
cipher.init(Cipher.DECRYPT_MODE, key, GCMParameterSpec(128, iv))
val decryptedBytes = cipher.doFinal(encryptedBytes)
return ByteArrayInputStream(decryptedBytes)
堆栈跟踪
E/AndroidRuntime: FATAL EXCEPTION: main
Process: onboard.app.passageways, PID: 15441
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: javax.crypto.AEADBadTagException
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:517)
at javax.crypto.Cipher.doFinal(Cipher.java:2055)
at passageways.android.onboard.services.EncryptionService.readEncryptedFile(EncryptionService.kt:79)
at passageways.android.onboard.fragments.MeetingBookDialogFragment.onViewCreated(Fragment.kt:38)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.security.KeyStoreException: Signature/MAC verification failed
at android.security.KeyStore.getKeyStoreException(KeyStore.java:839)
at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
at android.security.keystore.AndroidKeyStoreAuthenticatedAESCipherSpi$BufferAllOutputUntilDoFinalStreamer.doFinal(AndroidKeyStoreAuthenticatedAESCipherSpi.java:373)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
at javax.crypto.Cipher.doFinal(Cipher.java:2055)
at passageways.android.onboard.services.EncryptionService.readEncryptedFile(EncryptionService.kt:79)
at passageways.android.onboard.fragments.MeetingBookDialogFragment.onViewCreated(Fragment.kt:38)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
最佳答案
事实证明,readBytes()使用默认的缓冲区大小,并且仅返回该长度的字节缓冲区。因此,实际上并没有以字节为单位返回整个文件,而只是返回缓冲区的长度。
我已切换为使用CipherOutputStream,请确保在将您的内容写入标签后包含flush()!