从保管箱和显示屏解密图像

时间:2012-09-07 09:07:23

标签: java image encryption photo dropbox

我想要解密将在三星Galaxy选项卡上显示的照片,我尝试了ByteArrayOutputStream,当我在我的模拟器中运行它时,当我从Dropbox中随机下载照片时,它会显示内部错误。 为什么这样?或者我从保管箱调用了错误的路径?有人帮我解决这个问题吗?因为我已经尝试了很多方法来解决它但我仍然无法解决问题..

        // 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";

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

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() {

1 个答案:

答案 0 :(得分:1)

尝试使用

ByteArrayInputStream fis = new ByteArrayInputStream(encryptedData);

输出流用于写入,输入流用于读取。您希望Dropbox API从您的字节读取,对吗?

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