我正在尝试解码我从解析中获取的图像,而我将其发送到另一个函数进行解密,但我似乎无法正确解密文件,因为在解码行上我得到的字符串是因此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();
}
}
}
我似乎没有找到文件的问题,因为我不确定问题是解密方法还是我从解析得到的文件是不正确的。
答案 0 :(得分:1)
错误是:&#34;字符串为null因此无需解码&#34;这表示没有输入,这就是要看的东西。 IOW,调试,断点,调试器中的单一浸渍,打印变量。