Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AES 256位加密(Bouncycastle)Android的OutofMemory异常_Java_Android_Out Of Memory - Fatal编程技术网

Java AES 256位加密(Bouncycastle)Android的OutofMemory异常

Java AES 256位加密(Bouncycastle)Android的OutofMemory异常,java,android,out-of-memory,Java,Android,Out Of Memory,我使用Bouncycastle对文件进行AES 256位加密,然后上传到后端 我的应用程序在安卓4.0设备上运行良好。当我在安卓4.3设备上测试同一个应用程序时,首先它在加密时崩溃,显示OutofMemory异常。所以我添加了try-catch异常 它完成了加密,但在解密后发送到后端的后续文件显示乱码 不确定这是怎么可能的,因为它是使用相同密钥的同一个应用程序 你知道我如何解决这个问题吗?我做错什么了吗?下面是我的加密代码: public void encryptfile(String encp

我使用Bouncycastle对文件进行AES 256位加密,然后上传到后端

我的应用程序在安卓4.0设备上运行良好。当我在安卓4.3设备上测试同一个应用程序时,首先它在加密时崩溃,显示OutofMemory异常。所以我添加了try-catch异常

它完成了加密,但在解密后发送到后端的后续文件显示乱码

不确定这是怎么可能的,因为它是使用相同密钥的同一个应用程序

你知道我如何解决这个问题吗?我做错什么了吗?下面是我的加密代码:

public void encryptfile(String encpathname , String destencfile){

        try {

            byte enc[] = null;

            File file = new File(encpathname);
            byte[] data = new byte[(int) file.length()];

            FileInputStream fis;

                fis = new FileInputStream(file);
                fis.read(data);
                fis.close();

                enc = encrypt(passphrase, data);

                FileOutputStream stream = new FileOutputStream(destencfile);
                stream.write(enc);
                stream.close();

                file.delete();

                System.gc();

                }catch (Exception e) {

                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }

private static byte [] encrypt(String passphrase, byte [] inbytes) throws Exception {
                SecretKey key = generateKey(passphrase);

                Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
               // Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

                cipher.init(Cipher.ENCRYPT_MODE, key, generateIV(cipher), random);
                return cipher.doFinal(inbytes);
            }

            private static SecretKey generateKey(String passphrase) throws Exception {
                PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt.getBytes(), iterations, keyLength);
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
                return keyFactory.generateSecret(keySpec);
            }

            private static IvParameterSpec generateIV(Cipher cipher) throws Exception {
                byte [] ivBytes = new byte[cipher.getBlockSize()];
                random.nextBytes(ivBytes);
                return new IvParameterSpec(ivBytes);
            }

您无法通过捕获异常来修复导致异常的错误。您可能也不应该尝试将整个文件加载到内存中。大多数加密API都允许通过密码对数据段进行流式传输,因此您可以执行cipher.init(),然后对文件的每个段(以1024个块或其他方式读取)调用cipher.update(data),然后在最后一个段上使用cipher.final()将其关闭。否则,您将在内存中发送整个内容,并且取决于数据是什么,许多手机可能没有物理容量来加载数据。这将防止在文件大小实际大于Integer.MAX_值字节的情况下,仅部分加密。