我有一些用 RNCryptor 加密的图像文件,我想解密它们并将它们用作 Framelayout 的背景。我浏览了示例文件,但我的应用程序强制在一段时间后关闭并且没有显示任何图像。我使用 JNCryptor 库
我将加密图像放在原始文件夹中,如下所示:
这是我活动中的代码:
@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): 致命异常: main 08-28 19:52:11.720: E/AndroidRuntime(1063): java.lang.RuntimeException: 无法启动活动 ComponentInfo{net. appersian.android.wod/net.appersian.android.wod.MainActivity}:java.lang.NullPointerException 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): at 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): 在 java.lang.reflect.Method.invokeNative(Native Method) 08-28 19:52:11.720: E/AndroidRuntime(1063): 在 java.lang.reflect.Method.invoke(Method.java:511) 08-28 19:52:11.720: E/AndroidRuntime(1063): 在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): Caused作者: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/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/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() 添加到 try/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): 在 org.cryptonode.jncryptor.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:244) 08-28 20:54:10.496: W/System.err( 1487): 在 org.cryptonode.jncryptor.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:319) 08-28 20:54:10.496: W/System.err(1487): 在 org.cryptonode.jncryptor.AES256JNCryptor.decryptData(AES256JNCryptor .java:276) 08-28 20:54:10.496: W/System.err(1487): 在 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: