Java 在使用AES加密从数据库读取数据并在listview中列出时,线程以未捕获异常(组=0x40a3b1f8)退出?
我成功地使用AES加密字符串并保存到数据库中。现在,当应用程序启动时,它会读取listview上的数据库和列表。由于字符串是加密的,所以在显示在listview中之前必须对其进行解密。我编写了代码,看起来还可以,但每次启动时我的应用程序都会崩溃 AESHelper.javaJava 在使用AES加密从数据库读取数据并在listview中列出时,线程以未捕获异常(组=0x40a3b1f8)退出?,java,android,listview,encryption,Java,Android,Listview,Encryption,我成功地使用AES加密字符串并保存到数据库中。现在,当应用程序启动时,它会读取listview上的数据库和列表。由于字符串是加密的,所以在显示在listview中之前必须对其进行解密。我编写了代码,看起来还可以,但每次启动时我的应用程序都会崩溃 AESHelper.java package protect.my.password; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto
package protect.my.password;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.app.Activity;
public class AESHelper extends Activity{
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}
package protect.my.password;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
/**
* Demo application to show how to use the
* built-in SQLite database with a cursor to populate
* a ListView.
*/
public class ListView_read extends Activity {
int[] imageIDs = {R.drawable.lock};
int nextImageIndex = 0;
String seedValue = "This Is MySecure";
String diwas=DBAdapter.KEY_WEBSITE;
String Website_decrypt;
DBAdapter myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_read);
openDB();
populateListViewFromDB();
registerListClickCallback();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDb = new DBAdapter(this);
myDb.open();
}
private void closeDB() {
myDb.close();
}
private void populateListViewFromDB() {
Cursor cursor = myDb.getAllRows();
// Allow activity to manage lifetime of the cursor.
// DEPRECATED! Runs on the UI thread, OK for small/short queries.
startManagingCursor(cursor);
diwas();
// Setup mapping from cursor to view fields:
String[] fromFieldNames = new String[] {Website_decrypt, DBAdapter.KEY_STUDENTNUM};
int[] toViewIDs = new int[] {R.id.item_name, R.id.item_icon};
// Create adapter to may columns of the DB onto elementt in the UI.
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this, // Context
R.layout.item_layout, // Row layout template
cursor, // cursor (set of DB records to map)
fromFieldNames, // DB Column names
toViewIDs, // View IDs to put information in
1
);
// Set the adapter for the list view
ListView myList = (ListView) findViewById(R.id.listViewFromDB);
myList.setAdapter(myCursorAdapter);
}
private void diwas() {
// TODO Auto-generated method stub
try {
Website_decrypt = AESHelper.decrypt(seedValue, diwas);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
知道我的应用程序为什么会崩溃吗
Logcat
08-05 08:49:11.425: D/dalvikvm(2363): GC_FOR_ALLOC freed 76K, 5% free 3135K/3276K, paused 31ms, total 34ms
08-05 08:49:11.475: W/System.err(2363): java.lang.NumberFormatException: Invalid int: "we"
08-05 08:49:11.485: W/System.err(2363): at java.lang.Integer.invalidInt(Integer.java:137)
08-05 08:49:11.485: W/System.err(2363): at java.lang.Integer.parse(Integer.java:374)
08-05 08:49:11.485: W/System.err(2363): at java.lang.Integer.parseInt(Integer.java:365)
08-05 08:49:11.495: W/System.err(2363): at java.lang.Integer.valueOf(Integer.java:509)
08-05 08:49:11.495: W/System.err(2363): at protect.my.password.AESHelper.toByte(AESHelper.java:72)
08-05 08:49:11.495: W/System.err(2363): at protect.my.password.AESHelper.decrypt(AESHelper.java:29)
08-05 08:49:11.495: W/System.err(2363): at protect.my.password.ListView_read.diwas(ListView_read.java:87)
08-05 08:49:11.495: W/System.err(2363): at protect.my.password.ListView_read.populateListViewFromDB(ListView_read.java:59)
08-05 08:49:11.505: W/System.err(2363): at protect.my.password.ListView_read.onCreate(ListView_read.java:35)
08-05 08:49:11.505: W/System.err(2363): at android.app.Activity.performCreate(Activity.java:5231)
08-05 08:49:11.505: W/System.err(2363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-05 08:49:11.505: W/System.err(2363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-05 08:49:11.505: W/System.err(2363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-05 08:49:11.505: W/System.err(2363): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-05 08:49:11.505: W/System.err(2363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-05 08:49:11.505: W/System.err(2363): at android.os.Handler.dispatchMessage(Handler.java:102)
08-05 08:49:11.505: W/System.err(2363): at android.os.Looper.loop(Looper.java:136)
08-05 08:49:11.505: W/System.err(2363): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-05 08:49:11.505: W/System.err(2363): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 08:49:11.505: W/System.err(2363): at java.lang.reflect.Method.invoke(Method.java:515)
08-05 08:49:11.505: W/System.err(2363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-05 08:49:11.505: W/System.err(2363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-05 08:49:11.505: W/System.err(2363): at dalvik.system.NativeStart.main(Native Method)
08-05 08:49:11.515: D/AndroidRuntime(2363): Shutting down VM
08-05 08:49:11.515: W/dalvikvm(2363): threadid=1: thread exiting with uncaught exception (group=0xb4aa9ba8)
08-05 08:49:11.535: E/AndroidRuntime(2363): FATAL EXCEPTION: main
08-05 08:49:11.535: E/AndroidRuntime(2363): Process: protect.my.password, PID: 2363
08-05 08:49:11.535: E/AndroidRuntime(2363): java.lang.RuntimeException: Unable to start activity ComponentInfo{protect.my.password/protect.my.password.ListView_read}: java.lang.NullPointerException
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.os.Handler.dispatchMessage(Handler.java:102)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.os.Looper.loop(Looper.java:136)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-05 08:49:11.535: E/AndroidRuntime(2363): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 08:49:11.535: E/AndroidRuntime(2363): at java.lang.reflect.Method.invoke(Method.java:515)
08-05 08:49:11.535: E/AndroidRuntime(2363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-05 08:49:11.535: E/AndroidRuntime(2363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-05 08:49:11.535: E/AndroidRuntime(2363): at dalvik.system.NativeStart.main(Native Method)
08-05 08:49:11.535: E/AndroidRuntime(2363): Caused by: java.lang.NullPointerException
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.database.sqlite.SQLiteCursor.getColumnIndex(SQLiteCursor.java:178)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:301)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:107)
08-05 08:49:11.535: E/AndroidRuntime(2363): at protect.my.password.ListView_read.populateListViewFromDB(ListView_read.java:69)
08-05 08:49:11.535: E/AndroidRuntime(2363): at protect.my.password.ListView_read.onCreate(ListView_read.java:35)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.app.Activity.performCreate(Activity.java:5231)
08-05 08:49:11.535: E/AndroidRuntime(2363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-05 08:49:11.425:D/dalvikvm(2363):全部释放76K,5%释放3135K/3276K,暂停31ms,总计34ms
08-05 08:49:11.475:W/System.err(2363):java.lang.NumberFormatException:无效int:“我们”
08-05 08:49:11.485:W/System.err(2363):位于java.lang.Integer.invalidit(Integer.java:137)
08-05 08:49:11.485:W/System.err(2363):at java.lang.Integer.parse(Integer.java:374)
08-05 08:49:11.485:W/System.err(2363):位于java.lang.Integer.parseInt(Integer.java:365)
08-05 08:49:11.495:W/System.err(2363):at java.lang.Integer.valueOf(Integer.java:509)
08-05 08:49:11.495:W/System.err(2363):at-protect.my.password.AESHelper.toByte(AESHelper.java:72)
08-05 08:49:11.495:W/System.err(2363):at-protect.my.password.AESHelper.decrypt(AESHelper.java:29)
08-05 08:49:11.495:W/System.err(2363):at-protect.my.password.ListView\u-read.diwas(ListView\u-read.java:87)
08-05 08:49:11.495:W/System.err(2363):at protect.my.password.ListView\u read.populateListViewFromDB(ListView\u read.java:59)
08-05 08:49:11.505:W/System.err(2363):at protect.my.password.ListView\u read.onCreate(ListView\u read.java:35)
08-05 08:49:11.505:W/System.err(2363):位于android.app.Activity.performCreate(Activity.java:5231)
08-05 08:49:11.505:W/System.err(2363):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-05 08:49:11.505:W/System.err(2363):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-05 08:49:11.505:W/System.err(2363):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-05 08:49:11.505:W/System.err(2363):在android.app.ActivityThread.access$800(ActivityThread.java:135)
08-05 08:49:11.505:W/System.err(2363):位于android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-05 08:49:11.505:W/System.err(2363):位于android.os.Handler.dispatchMessage(Handler.java:102)
08-05 08:49:11.505:W/System.err(2363):在android.os.Looper.loop(Looper.java:136)上
08-05 08:49:11.505:W/System.err(2363):位于android.app.ActivityThread.main(ActivityThread.java:5017)
08-05 08:49:11.505:W/System.err(2363):位于java.lang.reflect.Method.invokenactive(本机方法)
08-05 08:49:11.505:W/System.err(2363):位于java.lang.reflect.Method.invoke(Method.java:515)
08-05 08:49:11.505:W/System.err(2363):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-05 08:49:11.505:W/System.err(2363):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-05 08:49:11.505:W/System.err(2363):在dalvik.System.NativeStart.main(本机方法)
08-05 08:49:11.515:D/AndroidRuntime(2363):关闭虚拟机
08-05 08:49:11.515:W/dalvikvm(2363):threadid=1:线程以未捕获异常退出(组=0xb4aa9ba8)
08-05 08:49:11.535:E/AndroidRuntime(2363):致命异常:主
08-05 08:49:11.535:E/AndroidRuntime(2363):进程:protect.my.password,PID:2363
08-05 08:49:11.535:E/AndroidRuntime(2363):java.lang.RuntimeException:无法启动活动组件信息{protect.my.password/protect.my.password.ListView_read}:java.lang.NullPointerException
08-05 08:49:11.535:E/AndroidRuntime(2363):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-05 08:49:11.535:E/AndroidRuntime(2363):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-05 08:49:11.535:E/AndroidRuntime(2363):在android.app.ActivityThread.access$800(ActivityThread.java:135)
08-05 08:49:11.535:E/AndroidRuntime(2363):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-05 08:49:11.535:E/AndroidRuntime(2363):在android.os.Handler.dispatchMessage(Handler.java:102)上
08-05 08:49:11.535:E/AndroidRuntime(2363):在android.os.Looper.loop(Looper.java:136)
08-05 08:49:11.535:E/AndroidRuntime(2363):位于android.app.ActivityThread.main(ActivityThread.java:5017)
08-05 08:49:11.535:E/AndroidRuntime(2363):位于java.lang.reflect.Method.Invokenactive(本机方法)
08-05 08:49:11.535:E/AndroidRuntime(2363):位于java.lang.reflect.Method.invoke(Method.java:515)
08-05 08:49:11.535:E/AndroidRuntime(2363):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-05 08:49:11.535:E/AndroidRuntime(2363):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-05 08:49:11.535:E/AndroidRuntime(2363):在dalvik.system.NativeStart.main(本机方法)
08-05 08:49:11.535:E/AndroidRuntime(2363):由以下原因引起:java.lang.NullPointerException
08-05 08:49:11.535:E/AndroidRuntime(2363):位于android.database.sqlite.SQLiteCursor.getColumnIndex(SQLiteCursor.java:178)
08-05 08:49:11.535:E/AndroidRuntime(2363):位于android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:301)
08-05 08:49:11.535:E/AndroidRuntime(2363):在android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
08-05 08:49:11.535:E/AndroidRuntime(2363):在android.widget.SimpleCursorAdapter。(SimpleCursorAdapter.java:107)
08-05 08:49:11.535:E/AndroidRuntime(2363):at protect.my.password.ListView\u read.populateListViewFromDB(ListView\u read.java:69)
08-05 08:49:11.535:E/AndroidRuntime(2363):at protect.my.password.ListView\u read.onCreate(ListView\u read.java:35)
08-05 08:49:11.535:E/AndroidRuntime(2363):在android.app.Activity.performCreate(Activity.java:5231)
08