我试图在Android KeyStore中使用AES加密-密钥生成和加密工作正常,但加密不能。我使用以下代码对密钥进行了更新:
KeyGenerator keyGenerator = KeyGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyGenerator.init(
new KeyGenParameterSpec.Builder(keyAlias, keyUsage)
.setKeySize(256)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.setIsStrongBoxBacked(false)
.build());
keyTeeAES = keyGenerator.generateKey();
和下面的加密和解密代码
Cipher cipher;
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, keyTeeAES);
cipherCreatedAES = cipher.doFinal(data);
IvParameterSpec ivParameterSpec = new IvParameterSpec(cipher.getIV());
cipher.init(Cipher.DECRYPT_MODE, keyTeeAES, ivParameterSpec);
cipher.doFinal(cipherCreatedAES);
我在执行解密时出现了以下错误,很明显,我在
IvParameterSpec ivParameterSpec =新IvParameterSpec(cipher.getIV());
W/System.err: java.lang.NullPointerException: Attempt to get length of null array
at javax.crypto.spec.IvParameterSpec.<init>(IvParameterSpec.java:53)
at com.example.securecomputingtest.CryptographyTee.useKeysAES(CryptographyTee.java:347)
at com.example.securecomputingtest.MainActivity$2.onClick(MainActivity.java:150)
at android.view.View.performClick(View.java:7259)
at android.view.View.performClickInternal(View.java:7236)
at android.view.View.access$3600(View.java:801)
at android.view.View$PerformClick.run(View.java:27892)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
我也试过跟随Why does my AES encryption throws an InvalidKeyException? --但也没有工作
发布于 2020-06-22 19:03:31
我现在通过禁用KeyGenParameterSpec中的随机加密来解决这个问题
.setRandomizedEncryptionRequired(假)
设置一个固定的IV,它现在起作用了,尽管这个解决方案可能不是最优的。
https://stackoverflow.com/questions/62521053
复制相似问题