我有一些使用RNCryptor加密的图像文件,我想解密它们并将它们用作Framelayout的背景。 我浏览了示例文件,但是我的应用程序在一段时间后关闭并且没有显示任何图像。 我用 JNCryptor library
我将加密图像放在原始文件夹中,如下所示:
这是我活动中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout img = (FrameLayout) findViewById(R.id.scrollView);
final String password = "myPasswordString";
JNCryptor cryptor = new AES256JNCryptor();
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.troodon_ph);
byte[] b = null;
byte[] data = null;
try {
b = new byte[in_s.available()];
in_s.read(b);
} catch (IOException e) {
Log.i("decrypt error", e.toString());
}
try {
data = cryptor.decryptData(b, password.toCharArray());
} catch (InvalidHMACException e) {
Log.i("decrypt error", e.toString());
} catch (CryptorException e) {
Log.i("decrypt error", e.toString());
}
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap);
img.setBackgroundDrawable(bDrawable);
}
此文件在iOS上运行良好,代码如下:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:getName ofType:@"wod"];
NSData *encryptedData = [NSData dataWithContentsOfFile:imagePath];
NSData *decryptedData = [RNDecryptor decryptData:encryptedData
withPassword:PASSWORD
error:&error];
UIImage* bgImage = [UIImage imageWithData:decryptedData];
UIImageView * movingImageView = [[UIImageView alloc]initWithImage:bgImage];
这是logcat输出:
08-28 19:52:11.720:E / AndroidRuntime(1063):致命异常:主要 08-28 19:52:11.720:E / AndroidRuntime(1063):java.lang.RuntimeException:无法启动活动ComponentInfo {net.appersian.android.wod / net.appersian.android.wod.MainActivity}:java.lang 。空指针异常 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.app.ActivityThread.access $ 600(ActivityThread.java:130) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.os.Handler.dispatchMessage(Handler.java:99) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.os.Looper.loop(Looper.java:137) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.app.ActivityThread.main(ActivityThread.java:4745) 08-28 19:52:11.720:E / AndroidRuntime(1063):at java.lang.reflect.Method.invokeNative(Native Method) 08-28 19:52:11.720:E / AndroidRuntime(1063):at java.lang.reflect.Method.invoke(Method.java:511) 08-28 19:52:11.720:E / AndroidRuntime(1063):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 08-28 19:52:11.720:E / AndroidRuntime(1063):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-28 19:52:11.720:E / AndroidRuntime(1063):at dalvik.system.NativeStart.main(Native Method) 08-28 19:52:11.720:E / AndroidRuntime(1063):引起:java.lang.NullPointerException 08-28 19:52:11.720:E / AndroidRuntime(1063):at net.appersian.android.wod.MainActivity.onCreate(MainActivity.java:54) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.app.Activity.performCreate(Activity.java:5008) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-28 19:52:11.720:E / AndroidRuntime(1063):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 08-28 19:52:11.720:E / AndroidRuntime(1063):... 11更多
我在解密文件方面做错了什么?
更新: 我添加了e.printStackTrace()来尝试/ catch,这是我的新logcat:
08-28 20:54:10.496:W / System.err(1487):org.cryptonode.jncryptor.InvalidHMACException:HMAC值不正确。 08-28 20:54:10.496:W / System.err(1487):at org.cryptonode.jncryptor.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:244) 08-28 20:54:10.496:W / System.err(1487):at org.cryptonode.jncryptor.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:319) 08-28 20:54:10.496:W / System.err(1487):at org.cryptonode.jncryptor.AES256JNCryptor.decryptData(AES256JNCryptor.java:276) 08-28 20:54:10.496:W / System.err(1487):at net.appersian.android.wod.MainActivity.onCreate(MainActivity.java:50) 08-28 20:54:10.496:W / System.err(1487):在android.app.Activity.performCreate(Activity.java:5008) 08-28 20:54:10.496:W / System.err(1487):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-28 20:54:10.496:W / System.err(1487):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 08-28 20:54:10.496:W / System.err(1487):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-28 20:54:10.496:W / System.err(1487):在android.app.ActivityThread.access $ 600(ActivityThread.java:130) 08-28 20:54:10.496:W / System.err(1487):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195) 08-28 20:54:10.496:W / System.err(1487):在android.os.Handler.dispatchMessage(Handler.java:99) 08-28 20:54:10.496:W / System.err(1487):在android.os.Looper.loop(Looper.java:137) 08-28 20:54:10.496:W / System.err(1487):在android.app.ActivityThread.main(ActivityThread.java:4745) 08-28 20:54:10.496:W / System.err(1487):at java.lang.reflect.Method.invokeNative(Native Method) 08-28 20:54:10.496:W / System.err(1487):at java.lang.reflect.Method.invoke(Method.java:511) 08-28 20:54:10.496:W / System.err(1487):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 08-28 20:54:10.496:W / System.err(1487):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-28 20:54:10.496:W / System.err(1487):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
第一
http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available%28%29
http://docs.oracle.com/javase/8/docs/technotes/guides/io/troubleshooting.html
Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream
我不确定那些有任何问题,但试试这个:
InputStream is = getResources().openRawResource(R.raw.troodon_ph);
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
try {
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = baf.toByteArray();
我希望它有效,但让我知道!!
并且如果它不起作用,请确保您的文件已正确加密。
答案 1 :(得分:1)
我在这个问题上做了很多工作,我在Github问开发人员,最后,我发现问题是密码字符串,而不是因为密码错误,是因为密码字符串中的unicode字符,在某种程度上,如果我们想要在所有设备和平台上获得良好的编码/解码效果,最好选择ascii字符作为密码。