gpt4 book ai didi

android - 在安卓上使用 RNCryptor

转载 作者:行者123 更新时间:2023-11-29 00:17:33 27 4
gpt4 key购买 nike

我有一些用 RNCryptor 加密的图像文件,我想解密它们并将它们用作 Framelayout 的背景。我浏览了样本文件,但我的应用程序在一段时间后关闭并且没有显示任何图像。我用 JNCryptor library

我把加密后的图片放在raw文件夹中,如下所示:

here my project folder

这是我在 Activity 中的代码:

@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): FATAL EXCEPTION: main 08-28 19:52:11.720: E/AndroidRuntime(1063): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.appersian.android.wod/net.appersian.android.wod.MainActivity}: java.lang.NullPointerException 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.access$600(ActivityThread.java:130) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 08-28 19:52:11.720: E/AndroidRuntime(1063): at 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): at 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): Caused by: 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): at android.app.Activity.performCreate(Activity.java:5008) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 08-28 19:52:11.720: E/AndroidRuntime(1063): ... 11 more

我在解密文件时做错了什么?

更新:我将 e.printStackTrace() 添加到 try/catch,这是我的新 logcat:

08-28 20:54:10.496: W/System.err(1487): org.cryptonode.jncryptor.InvalidHMACException: Incorrect HMAC value. 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): at android.app.Activity.performCreate(Activity.java:5008) 08-28 20:54:10.496: W/System.err(1487): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.access$600(ActivityThread.java:130) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 08-28 20:54:10.496: W/System.err(1487): at android.os.Handler.dispatchMessage(Handler.java:99) 08-28 20:54:10.496: W/System.err(1487): at android.os.Looper.loop(Looper.java:137) 08-28 20:54:10.496: W/System.err(1487): at 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)

最佳答案

首先:

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

请注意,虽然 InputStream 的某些实现会返回流中的总字节数,但很多不会。使用此方法的返回值来分配旨在保存此流中所有数据的缓冲区是永远不正确的

我不确定这些有什么问题,但让我们试试这个:

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

我希望它有用,但请告诉我!!

最后,如果它不起作用,请确保您的文件已正确加密。

关于android - 在安卓上使用 RNCryptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25556955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com