问题描述
我在使用 AES/GCM/nopadding 时遇到问题,在特定范围后抛出异常并且在 65536 范围后不加密。我的文本长度为 131072 ,但加密只发生到 65536。>
even tried the following as well,AES/GCM/nopadding
AES/CBC/pkcs7padding.but after a particular range it is throwing invalidexception.
**Note:**
If I use BouncyCastle provider it is working fine(encryption is happening). But when I use Android keystore with keyspec is not working.
甚至还尝试了以下方法, AES/GCM/无填充 AES/CBC/pkcs7padding。但在特定范围之后,它会抛出无效异常。
代码:
public static EncryptCallBack encryptText(final byte[] textToEncrypt)
throws UnrecoverableEntryException,NoSuchAlgorithmException,KeyStoreException,NoSuchProviderException,NoSuchPaddingException,InvalidKeyException,IOException,InvalidAlgorithmParameterException,SignatureException,BadPaddingException,IllegalBlockSizeException {
EncryptCallBack encryptCallBack = new EncryptCallBack();
final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE,getSecretKey());
/*Cipher update for Large data and Incremental Encryption */
byte[] encryptedValue = null;
byte[] input = textToEncrypt;
byte[] cipherText = new byte[cipher.getoutputSize(input.length)];
int ctLength = 0;
try {
ctLength = cipher.update(input,input.length,cipherText,0);
Log.d("Update ciper","Update"+ ctLength);
} catch (ShortBufferException e) {
e.printstacktrace();
}
try {
ctLength += cipher.doFinal(cipherText,ctLength);
} catch (ShortBufferException e) {
e.printstacktrace();
}
encryptedValue = cipherText;
Log.d("Encrypted data Length","Encyrptedlength :"+ encryptedValue.length);
/*Cipher update for Large data and Incremental Encryption */
encryptCallBack.setSpecIV(cipher.getIV());
encryptCallBack.setData(encryptedValue);
return encryptCallBack;
}
/*SecertKey*/
public static SecretKey getSecretKey() {
SecretKey key = null;
try {
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
keyStore.load(null);
} catch (KeyStoreException | CertificateException |
NoSuchAlgorithmException | IOException e) {
}
if (!keyStore.containsAlias("alias")) {
KeyGenerator keygen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,ANDROID_KEY_STORE);
KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder("alias",KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.build();
keygen.init(spec);
key = keygen.generateKey();
} else {
KeyStore.SecretKeyEntry keyEntry = (KeyStore.SecretKeyEntry) keyStore.getEntry("alias",null);
key = keyEntry.getSecretKey();
}
} catch (Exception e) {
}
return key;
}
public static String decryptText(final byte[] encryptedData,byte[] specIV)
throws UnrecoverableEntryException,IllegalBlockSizeException,InvalidAlgorithmParameterException {
final Cipher cipher = Cipher.getInstance("AES/GCM/nopadding");
final GCMParameterSpec spec = new GCMParameterSpec(128,specIV);
cipher.init(Cipher.DECRYPT_MODE,getSecretKey(),spec);
return new String(cipher.doFinal(encryptedData),"UTF-8");
}
如果有人遇到这种情况。请帮忙。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)