java - 无法从解析中解码图像

标签 java android encryption parse-platform

我正在尝试解码从解析中获得的图像,同时将其发送到另一个函数进行解密,但是我似乎无法正确解密文件,因为在解码行上我得到的字符串是null 因此没有什么可解码的,我如何检索解析文件并解密然后将其发送到解码。

我的解析函数

case R.id.decryptButton:


    Intent i = getIntent();
    image = i.getStringExtra("image");
    progressDialog = ProgressDialog.show(SingleFileView.this, "",
    "Downloading Image...", true);

    // Locate the class table named "ImageUpload" in Parse.com
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                                                           "NewFiles");

    // Locate the objectId from the class
         query.getInBackground(objectId,
         new GetCallback<ParseObject>() {

         public void done(ParseObject object,
            ParseException e) {
            // TODO Auto-generated method stub

            // Locate the column named "ImageName" and set
            // the string

           final ParseFile fileObject = (ParseFile) object
               .get("ImageFile");
               fileObject
               .getDataInBackground(new GetDataCallback() {
                  public void done(byte[] data,
                     ParseException e) {
                        if (e == null) {
                          Log.d("test",
                            "We've got data in data.");
                            // Decode the Byte[] into
                            // Bitmap
                            FileInputStream fis = null;
                            saveFile(data, "temp.jpeg");

                            try {
                             File xx = new File(Environment.getExternalStorageDirectory()+"/temp.jpeg");
                             fis = new FileInputStream(String.valueOf(data));
                             System.out.print(fileObject.getUrl());
                             System.out.print(fis);

                             } catch (FileNotFoundException e1) {
                             e1.printStackTrace();
                             }
                             byte[] dmg = decrypt(key, fis);
                             Bitmap bmp = BitmapFactory
                            .decodeByteArray(
                            dmg, 0,
                            dmg.length);

                          // Get the ImageView from
                          // main.xml
                          ImageView image = (ImageView) findViewById(R.id.image);

                          // Set the Bitmap into the
                          // ImageView
                          image.setImageBitmap(bmp);

                         // Close progress dialog
                         progressDialog.dismiss();

                         } else {
                         Log.d("test",
                         "There was a problem downloading the data.");
                         }
                      }
                   });
         }
});

我的解密和保存文件功能

private byte[] decrypt(byte[] skey, InputStream fis){

    SecretKeySpec skeySpec = new SecretKeySpec(skey, "AES");
    Cipher cipher;
    byte[] decryptedData=null;
    CipherInputStream cis=null;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv));
        // Create CipherInputStream to read and decrypt the image data
        cis = new CipherInputStream(fis, cipher);
        // Write encrypted image data to ByteArrayOutputStream
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] data = new byte[2048];
        while ((cis.read(data)) != -1) {
            buffer.write(data);
        }
        buffer.flush();
        decryptedData=buffer.toByteArray();

    }catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try {
            fis.close();
            cis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return decryptedData;
}
public void saveFile(byte[] data, String outFileName){
    FileOutputStream fos=null;
    try {
        fos=new FileOutputStream(Environment.getExternalStorageDirectory()+File.separator+outFileName);
        fos.write(data);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我似乎没有找到该文件的问题,因为我不确定问题是否出在解密方法上,或者我从解析中获取的文件是否正确完成。

最佳答案

错误是:“字符串为空,因此无法解码”,这表示没有输入,这就是要查看的内容。 IOW、调试、断点、调试器中的单次浸泡、打印变量。

关于java - 无法从解析中解码图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34426681/

相关文章:

performance - 此次主机扫描中最快的 SSL 加密算法是什么?

java - 如何将一个数组列表深度复制到另一个数组列表中

android - 从 AsyncTask 更改 TextView

android - CoordinatorLayout、AppBarLayout 和 ToolBar - 工具栏不会滚出屏幕

javascript - Java中的AES加密无法在Javascript中解密

java - 解密错误: Pad block corrupted

java - 用于 ant 的文件路径模式应该是什么,以选择未知子文件夹名称的所有内容文件

java - 在 Java 中使用自定义字体创建按钮会导致按钮形状奇怪

java - Java 查找字符串中出现次数最多的字符串

android - 在 Android 中运行 Vuforia 示例时出错