从保管箱中解密图像并显示 [英] Decrypt image from the dropbox and display

查看:127
本文介绍了从保管箱中解密图像并显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要解密这张照片,这张照片会显示在我的三星Galaxy Tab上,我尝试过ByteArrayOutputStream,当我在我的模拟器中运行它时,我会从Dropbox中随机下载一张照片。
为什么是这样的?或者我从投影箱中拨错了路径?有人帮我这个问题吗?因为我已经尝试了许多方法来解决它,但是我仍然无法解决问题。

  //现在选择一个随机的一个
int index =(int)(Math.random()* thumbs.size());
输入ent = thumbs.get(index);
String path = ent.path;
mFileLen = ent.bytes;

String cachePath = mContext.getCacheDir()。getAbsolutePath()+/+ IMAGE_FILE_NAME;
try {

KeyGenerator keygen = KeyGenerator.getInstance(DES);
SecretKey key = keygen.generateKey(); //生成密钥
byte [] encryptedData;
byte [] decryptptedData;
密码密码= Cipher.getInstance(DES / ECB / PKCS5Padding);
cipher.init(Cipher.DECRYPT_MODE,key);
// File f = new File(encryptfilepath);
FileInputStream in = new FileInputStream(cachePath);
encryptedData = new byte [(int)cachePath.length()];
in.read(encryptedData);
decryptptedData = cipher.doFinal(encryptedData);
ByteArrayOutputStream fis = new ByteArrayOutputStream(decryptptedData);
// ByteArrayInputStream fis = new ByteArrayInputStream(decryptptedData);
mFos = new FileOutputStream(new File(fis));

// mFos = new FileOutputStream(cachePath);
} catch(FileNotFoundException e){
mErrorMsg =无法创建本地文件来存储图像;

这是我加密图像的一部分。

  KeyGenerator keygen = KeyGenerator.getInstance(DES); 
SecretKey key = keygen.generateKey(); //生成密钥
//先加密文件
byte [] plainData;
byte [] encryptedData;
密码密码= Cipher.getInstance(DES / ECB / PKCS5Padding);
cipher.init(Cipher.ENCRYPT_MODE,key);
FileInputStream in = new FileInputStream(mFile); //从文件获取输入字节
plainData = new byte [(int)mFile.length()];
in.read(plainData); //将数据字节读入字节数组
encryptedData = cipher.doFinal(plainData); //加密数据
ByteArrayInputStream fis = new ByteArrayInputStream(encryptedData);
//将加密文件保存到dropbox

//通过创建一个请求,我们得到一个putFile操作的句柄
//所以如果我们想要的话我们可以取消它到
// FileInputStream fis = new FileInputStream(mFile);
String path = mPath + mFile.getName();
mRequest = mApi.putFileOverwriteRequest(path,fis,mFile.length(),
new ProgressListener(){


解决方案

尝试使用

  ByteArrayInputStream fis = new ByteArrayInputStream encryptData); 

输出流用于写入,输入流为您想要从您的字节读取的dropbox API吧?



如果您坚持将加密数据写入本地硬盘首先,那么您可能需要使用 File.createTempFile


I want to decrypt back the photo which will display it on my samsung galaxy tab and i tried the ByteArrayOutputStream and when i run it in my emulator it says internal error when i randomly download a photo from the dropbox. Why is this like this? Or did i call the wrong path from the dropbox? Is anyone kind to help me with this problem? As i've tried many ways to solve it but then i am still unable to solve the problem..

        // Now pick a random one
        int index = (int)(Math.random() * thumbs.size());
        Entry ent = thumbs.get(index);
        String path = ent.path;
        mFileLen = ent.bytes;

        String cachePath = mContext.getCacheDir().getAbsolutePath() + "/" + IMAGE_FILE_NAME;
        try {

            KeyGenerator keygen = KeyGenerator.getInstance("DES");
            SecretKey key = keygen.generateKey(); //generate key
            byte[] encryptedData;
            byte[] decryptedData;
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            //File f = new File(encryptfilepath);
            FileInputStream in = new FileInputStream(cachePath);
            encryptedData = new byte[(int)cachePath.length()];
            in.read(encryptedData);
            decryptedData = cipher.doFinal(encryptedData);
            ByteArrayOutputStream fis = new ByteArrayOutputStream(decryptedData);
            //ByteArrayInputStream fis = new ByteArrayInputStream(decryptedData);
             mFos = new FileOutputStream(new File(fis));

          //  mFos = new FileOutputStream(cachePath);
        } catch (FileNotFoundException e) {
            mErrorMsg = "Couldn't create a local file to store the image";

This is a part of my encrypted image.

KeyGenerator keygen = KeyGenerator.getInstance("DES");
        SecretKey key = keygen.generateKey(); //generate key
        //encrypt file here first
        byte[] plainData;
        byte[] encryptedData;
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        FileInputStream in = new FileInputStream(mFile); //obtains input bytes from a file
        plainData = new byte[(int)mFile.length()]; 
        in.read(plainData); //Read bytes of data into an array of bytes
        encryptedData = cipher.doFinal(plainData); //encrypt data               
        ByteArrayInputStream fis = new ByteArrayInputStream(encryptedData);
        //save encrypted file to dropbox

        // By creating a request, we get a handle to the putFile operation,
        // so we can cancel it later if we want to
      // FileInputStream fis = new FileInputStream(mFile);
        String path = mPath + mFile.getName();
        mRequest = mApi.putFileOverwriteRequest(path, fis, mFile.length(),
                new ProgressListener() {

解决方案

Try using

ByteArrayInputStream fis = new ByteArrayInputStream(encryptedData);

An output stream is for writing, and input stream is for reading. You want the dropbox API to read from your bytes, right?

If you insist on writing the encrypted data to the local harddrive first, then you might want to use File.createTempFile.

这篇关于从保管箱中解密图像并显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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