0

我正在尝试加密图库中的图像并将其存储在 sdcard 上。

当我试图将图像从图库移动到 sdcard 时,出现“强制关闭”错误,并且 logcat 没有显示异常。

我得到一个祝酒词“图像已成功加密”,但它没有将图像从画廊移动到 sdcard。

我究竟做错了什么?

        private void encript(byte[] data, byte[] clear) {

        byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
        0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = null;

        try {

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

        }    catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
        }    catch (NoSuchProviderException e) {

        e.printStackTrace();

        } catch (NoSuchPaddingException e) {

        e.printStackTrace();
        }
        try {
        cipher.init(Cipher.ENCRYPT_MODE, key);

        } catch (InvalidKeyException e) {

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

        byte[] cipherText = new byte[cipher.getOutputSize(data.length)];

       // int ctLength = cipher.update(data, 0, data.length, cipherText, 0);

        byte[] input = null;

        try { 
        input = cipher.doFinal(cipherText);
        } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (BadPaddingException e) {
        // TODO Auto-generated catch block


        e.printStackTrace();
        }
        try{
        if(input.length>0){
        Toast.makeText(getApplicationContext(), "Image successfully encripted", 3000).show();
        }
        ByteArrayInputStream bis = new ByteArrayInputStream(input);

        ObjectInputStream ois = new ObjectInputStream(bis);

     File encriptedfile = (File) ois.readObject();//This is the file which i want to Move to App folder

            bis.close();
        ois.close();
        } catch (StreamCorruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
    return;
    }
     //Code for copy  image into app folder......
    File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + ".map");
         try {
          FileUtils.copyFile (encriptedfile, outputFolder);
          } 
          catch (IOException e) {
          Log.e("photomover", e.toString());
          }
}

08-17 11:42:00.905: WARN/System.err(8453): java.io.StreamCorruptedException
08-17 11:42:00.905: WARN/System.err(8453):     at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2392)
08-17 11:42:00.905: WARN/System.err(8453):     at java.io.ObjectInputStream.<init>(ObjectInputStream.java:445)
08-17 11:42:00.905: WARN/System.err(8453):     at com.cw.GalleryShareimage1Activity.encript(GalleryShareimage1Activity.java:177)
08-17 11:42:00.905: WARN/System.err(8453):     at com.cw.GalleryShareimage1Activity.onCreate(GalleryShareimage1Activity.java:76)
08-17 11:42:00.915: WARN/System.err(8453):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 11:42:00.915: WARN/System.err(8453):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
08-17 11:42:00.915: WARN/System.err(8453):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
08-17 11:42:00.915: WARN/System.err(8453):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
08-17 11:42:00.915: WARN/System.err(8453):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
08-17 11:42:00.915: WARN/System.err(8453):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 11:42:00.915: WARN/System.err(8453):     at android.os.Looper.loop(Looper.java:123)
08-17 11:42:00.915: WARN/System.err(8453):     at android.app.ActivityThread.main(ActivityThread.java:4633)
08-17 11:42:00.915: WARN/System.err(8453):     at java.lang.reflect.Method.invokeNative(Native Method)
08-17 11:42:00.915: WARN/System.err(8453):     at java.lang.reflect.Method.invoke(Method.java:521)
08-17 11:42:00.915: WARN/System.err(8453):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-17 11:42:00.915: WARN/System.err(8453):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-17 11:42:00.915: WARN/System.err(8453):     at dalvik.system.NativeStart.main(Native Method)
08-17 11:42:00.925: WARN/dalvikvm(8453): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453): FATAL EXCEPTION: main
08-17 11:42:00.935: ERROR/AndroidRuntime(8453): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cw/com.cw.GalleryShareimage1Activity}: java.lang.NullPointerException: Source must not be null
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.os.Looper.loop(Looper.java:123)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.app.ActivityThread.main(ActivityThread.java:4633)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at java.lang.reflect.Method.invokeNative(Native Method)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at java.lang.reflect.Method.invoke(Method.java:521)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at dalvik.system.NativeStart.main(Native Method)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453): Caused by: java.lang.NullPointerException: Source must not be null
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1068)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1038)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at com.cw.GalleryShareimage1Activity.onCreate(GalleryShareimage1Activity.java:96)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):     ... 11 more
4

1 回答 1

0

您的代码有几个缺陷:

首先,当您加密数据时,您并未正确处理要加密的数据。你应该这样尝试:

byte[] cipherText = cipher.doFinal(data);

其次,您正在使用一个ObjectInputStream和它的readObject()方法。ObjectInputStream 仅对由 Java 序列化创建的数据有用。您不能以这种方式处理图像。铸造它File也没有任何意义。

当您在 RAM 中执行所有操作时(顺便说一句,由于 android 的 RAM 限制,这对大文件不起作用)您只需将加密的内存数据保存到文件中。无需移动:

File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),".map");
File outputFile = new File(outputFolder, "outputfilename.bin");
FileOutputStream out = new FileOutputStream(outputFile);
out.write(cipherText);
out.close;
于 2012-08-17T08:11:08.170 回答