微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android Keystore 存在大数据提供问题,我们使用 AES/GCM/NoPadding在特定范围后抛出异常

如何解决Android Keystore 存在大数据提供问题,我们使用 AES/GCM/NoPadding在特定范围后抛出异常

我在使用 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 举报,一经查实,本站将立刻删除。