问题
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() {
回答1:
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
.
来源:https://stackoverflow.com/questions/12315043/decrypt-image-from-the-dropbox-and-display