失败的图像加密后,从画廊的图像移动到SD卡 [英] failing to move image from gallery to sdcard after encrypting the image

查看:137
本文介绍了失败的图像加密后,从画廊的图像移动到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像从画廊加密并存储在SD卡。

当我试图将图像从画廊到SD卡我得到一个强制关闭的错误和logcat中没有显示异常移动。

我正举杯说:图像成功加密,但它不是图像从画廊到SD卡移动。

我究竟做错了什么?

 私人无效encript(字节[]数据字节[]清){        字节[] = keyBytes新的字节[] {0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07,0×08,×09,
        的0x0A,0x0B中,的0x0C,符进行,为0x0E,为0x0F,为0x10,为0x11,0×12,0x13,0x14的,为0x15,0x16,0x17已};
        SecretKeySpec键=新SecretKeySpec(keyBytes,AES);
        密码加密= NULL;        尝试{        密= Cipher.getInstance(AES / ECB / PKCS7Padding,BC);        }赶上(抛出:NoSuchAlgorithmException E){        e.printStackTrace();
        }赶上(NoSuchProviderException E){        e.printStackTrace();        }赶上(NoSuchPaddingException E){        e.printStackTrace();
        }
        尝试{
        cipher.init(Cipher.ENCRYPT_MODE,键);        }赶上(InvalidKeyException将E){        // TODO自动生成catch块
        e.printStackTrace();
        }        字节[] =密文新字节[cipher.getOutputSize(data.length)];       // INT ctLength = cipher.u​​pdate(数据,0,data.length,密文,0);        字节[]输入= NULL;        尝试{
        输入= cipher.doFinal(密文);
        }赶上(IllegalBlockSizeException E){
        // TODO自动生成catch块
        e.printStackTrace();
        }赶上(BadPaddingException E){
        // TODO自动生成catch块
        e.printStackTrace();
        }
        尝试{
        如果(input.length大于0){
        Toast.makeText(getApplicationContext(),图像成功encripted,3000).show();
        }
        ByteArrayInputStream的双=新ByteArrayInputStream的(输入);        ObjectInputStream的OIS =新的ObjectInputStream(之二);     文件encriptedfile =(文件)ois.readObject(); //这是我想移动到App文件夹中            bis.close();
        ois.close();
        }赶上(StreamCorruptedException E){
        // TODO自动生成catch块
        e.printStackTrace();
       }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
       }赶上(ClassNotFoundException的E){
        // TODO自动生成catch块
        e.printStackTrace();
       }
    返回;
    }
     // code来复制图像到app文件夹......
    文件outputFolder =新的文件(Environment.getExternalStorageDirectory()getAbsolutePath()+文件分割符+.MAP);
         尝试{
          FileUtils.copyFile(encriptedfile,outputFolder);
          }
          赶上(IOException异常五){
          Log.e(photomover,e.toString());
          }
}


  11月8日至17日:42:00.905:WARN / System.err的(8453):java.io.StreamCorruptedException
11月8日至17日:42:00.905:WARN / System.err的(8453):在java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2392)
11月8日至17日:42:00.905:WARN / System.err的(8453):在java.io.ObjectInputStream中的<&初始化GT;(ObjectInputStream.java:445)
11月8日至17日:42:00.905:WARN / System.err的(8453):在com.cw.GalleryShareimage1Activity.encript(GalleryShareimage1Activity.java:177)
11月8日至17日:42:00.905:WARN / System.err的(8453):在com.cw.GalleryShareimage1Activity.onCreate(GalleryShareimage1Activity.java:76)
11月8日至17日:42:00.915:WARN / System.err的(8453):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11月8日至17日:42:00.915:WARN / System.err的(8453):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
11月8日至17日:42:00.915:WARN / System.err的(8453):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
11月8日至17日:42:00.915:WARN / System.err的(8453):在android.app.ActivityThread.access $ 2300(ActivityThread.java:126)
11月8日至17日:42:00.915:WARN / System.err的(8453):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2038)
11月8日至17日:42:00.915:WARN / System.err的(8453):在android.os.Handler.dispatchMessage(Handler.java:99)
11月8日至17日:42:00.915:WARN / System.err的(8453):在android.os.Looper.loop(Looper.java:123)
11月8日至17日:42:00.915:WARN / System.err的(8453):在android.app.ActivityThread.main(ActivityThread.java:4633)
11月8日至17日:42:00.915:WARN / System.err的(8453):在java.lang.reflect.Method.invokeNative(本机方法)
11月8日至17日:42:00.915:WARN / System.err的(8453):在java.lang.reflect.Method.invoke(Method.java:521)
11月8日至17日:42:00.915:WARN / System.err的(8453):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
11月8日至17日:42:00.915:WARN / System.err的(8453):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11月8日至17日:42:00.915:WARN / System.err的(8453):在dalvik.system.NativeStart.main(本机方法)
11月8日至17日:42:00.925:WARN / dalvikvm(8453):主题ID = 1:螺纹未捕获的异常(组= 0x400207d8)退出
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):致命异常:主要
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):了java.lang.RuntimeException:无法启动活动ComponentInfo {} com.cw/com.cw.GalleryShareimage1Activity:显示java.lang.NullPointerException:源不能为空
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.app.ActivityThread.access $ 2300(ActivityThread.java:126)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2038)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.os.Handler.dispatchMessage(Handler.java:99)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.os.Looper.loop(Looper.java:123)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.app.ActivityThread.main(ActivityThread.java:4633)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在java.lang.reflect.Method.invokeNative(本机方法)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在java.lang.reflect.Method.invoke(Method.java:521)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在dalvik.system.NativeStart.main(本机方法)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):显示java.lang.NullPointerException:产生的原因来源不能为空
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1068)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1038)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在com.cw.GalleryShareimage1Activity.onCreate(GalleryShareimage1Activity.java:96)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
11月8日至17日:42:00.935:ERROR / AndroidRuntime(8453):11 ...更多


解决方案

您code几个缺陷:

首先,当你对数据进行加密您没有处理的数据被正确地加密。你应该尝试一下这种方式:

 字节[] =密文cipher.doFinal(数据);

二您使用的是的ObjectInputStream ,它是的readObject()方法。一个ObjectInputStream的是仅对于已经由Java序列创建的数据是有用的。您不能处理图像这种方式。它铸造于文件能源部也没有任何意义。

当你正在执行在RAM中的一切(这将BTW不是因为Android上的内存限制的大型文件),你只需要加密在内存中的数据保存到文件中。没有必要招:

 文件outputFolder =新的文件(Environment.getExternalStorageDirectory()getAbsolutePath(),地图);
文件OUTPUTFILE =新的文件(outputFolderoutputfilename.bin);
FileOutputStream中出=新的FileOutputStream(OUTPUTFILE);
out.write(密文);
out.close;

I am trying to encrypt an image from the gallery and store it on the sdcard.

When i am trying to move the image from the gallery to the sdcard i am getting a "force close" error and logcat does not show an exception.

I am getting a toast saying "Image successfully encrypted" but it is not moving the image from the gallery to the sdcard.

What am i doing wrong?

        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

解决方案

Your code as several flaws:

First, when you encrypt the data you are not processing the data to be encrypted correctly. You should try it this way:

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

Second you are using an ObjectInputStream and it's readObject() method. An ObjectInputStream is only useful for data that has been created by Java serialization. You can not process images this way. Casting it to File also doe not make any sense.

As you are performing everything in RAM (which will BTW not work for large files because of the RAM limitation on android) you just have to save the encrypted in-memory data to the file. No move necessary:

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;

这篇关于失败的图像加密后,从画廊的图像移动到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆