Guest User

Untitled

a guest
Jan 28th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.86 KB | None | 0 0
  1. --------- beginning of crash
  2. E/AndroidRuntime: FATAL EXCEPTION: main
  3. Process: com.passwordsavver.wahlfachprojekt, PID: 3336
  4. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.passwordsavver.wahlfachprojekt/com.passwordsavver.wahlfachprojekt.MainScreen}: android.database.sqlite.SQLiteException: no such column: favourites (code 1): , while compiling: SELECT DISTINCT _id, websitename, username, password, notes, favourites FROM mainTable
  5. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
  6. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
  7. at android.app.ActivityThread.access$800(ActivityThread.java:151)
  8. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
  9. at android.os.Handler.dispatchMessage(Handler.java:102)
  10. at android.os.Looper.loop(Looper.java:135)
  11. at android.app.ActivityThread.main(ActivityThread.java:5254)
  12. at java.lang.reflect.Method.invoke(Native Method)
  13. at java.lang.reflect.Method.invoke(Method.java:372)
  14. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  15. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  16. Caused by: android.database.sqlite.SQLiteException: no such column: favourites (code 1): , while compiling: SELECT DISTINCT _id, websitename, username, password, notes, favourites FROM mainTable
  17. at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  18. at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
  19. at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
  20. at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
  21. at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
  22. at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
  23. at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
  24. at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
  25. at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
  26. at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
  27. at com.passwordsavver.wahlfachprojekt.DBAdapter.getAllRows(DBAdapter.java:142)
  28. at com.passwordsavver.wahlfachprojekt.MainScreen.populateButtons(MainScreen.java:52)
  29. at com.passwordsavver.wahlfachprojekt.MainScreen.onCreate(MainScreen.java:33)
  30. at android.app.Activity.performCreate(Activity.java:5990)
  31. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
  32. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
  33. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
  34. at android.app.ActivityThread.access$800(ActivityThread.java:151)
  35. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
  36. at android.os.Handler.dispatchMessage(Handler.java:102)
  37. at android.os.Looper.loop(Looper.java:135)
  38. at android.app.ActivityThread.main(ActivityThread.java:5254)
  39. at java.lang.reflect.Method.invoke(Native Method)
  40. at java.lang.reflect.Method.invoke(Method.java:372)
  41. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  42. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  43. Application terminated.
  44.  
  45. package com.passwordsavver.wahlfachprojekt;
  46.  
  47. import android.content.ContentValues;
  48. import android.content.Context;
  49. import android.database.Cursor;
  50. import android.database.sqlite.SQLiteDatabase;
  51. import android.database.sqlite.SQLiteOpenHelper;
  52. import android.util.Log;
  53.  
  54.  
  55. public class DBAdapter {
  56.  
  57. private static final String TAG = "DBAdapter";
  58.  
  59. // DB Fields
  60. public static final String KEY_ROWID = "_id";
  61. public static final int COL_ROWID = 0;
  62.  
  63. public static final String KEY_WEBSITENAME = "websitename";
  64. public static final String KEY_USERNAME = "username";
  65. public static final String KEY_PASSWORD = "password";
  66. public static final String KEY_NOTES = "notes";
  67. public static final String KEY_FAV = "favourites";
  68.  
  69. public static final int COL_WEBSITENAME = 1;
  70. public static final int COL_USERNAME = 2;
  71. public static final int COL_PASSWORD = 3;
  72. public static final int COL_NOTES = 4;
  73. public static final int COL_FAV = 5;
  74.  
  75. public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_WEBSITENAME, KEY_USERNAME, KEY_PASSWORD, KEY_NOTES, KEY_FAV};
  76.  
  77. public static final String DATABASE_NAME = "MyDb";
  78. public static final String DATABASE_TABLE = "mainTable";
  79.  
  80. public static final int DATABASE_VERSION = 5;
  81.  
  82. private static final String DATABASE_CREATE_SQL =
  83. "create table " + DATABASE_TABLE
  84. + " (" + KEY_ROWID + " integer primary key autoincrement, "
  85. + KEY_WEBSITENAME + " string not null, "
  86. + KEY_USERNAME + " string not null, "
  87. + KEY_PASSWORD + " string not null, "
  88. + KEY_NOTES + " string, "
  89. + KEY_FAV + "string"
  90. + ");";
  91.  
  92. // Context of application who uses us.
  93. private final Context context;
  94.  
  95. private DatabaseHelper myDBHelper;
  96. private SQLiteDatabase db;
  97.  
  98. public DBAdapter(Context ctx) {
  99. this.context = ctx;
  100. myDBHelper = new DatabaseHelper(context);
  101. }
  102.  
  103. // Open the database connection.
  104. public DBAdapter open() {
  105. db = myDBHelper.getWritableDatabase();
  106. return this;
  107. }
  108.  
  109. // Close the database connection.
  110. public void close() {
  111. myDBHelper.close();
  112. }
  113.  
  114. // Add a new set of values to the database.
  115. public long insertRow(String websitename, String username, String password, String notes, String fav) {
  116.  
  117. // Create row's data:
  118. ContentValues initialValues = new ContentValues();
  119. initialValues.put(KEY_WEBSITENAME, websitename);
  120. initialValues.put(KEY_USERNAME, username);
  121. initialValues.put(KEY_PASSWORD, password);
  122. initialValues.put(KEY_NOTES, notes);
  123. initialValues.put(KEY_FAV, fav);
  124.  
  125. // Insert it into the database.
  126. return db.insert(DATABASE_TABLE, null, initialValues);
  127. }
  128.  
  129. // Delete a row from the database, by rowId (primary key)
  130. public boolean deleteRow(long rowId) {
  131. String where = KEY_ROWID + "=" + rowId;
  132. return db.delete(DATABASE_TABLE, where, null) != 0;
  133. }
  134.  
  135. public void deleteAll() {
  136. Cursor c = getAllRows();
  137. long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
  138. if (c.moveToFirst()) {
  139. do {
  140. deleteRow(c.getLong((int) rowId));
  141. } while (c.moveToNext());
  142. }
  143. c.close();
  144. }
  145.  
  146. // Return all data in the database.
  147. public Cursor getAllRows() {
  148. String where = null;
  149. Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
  150. where, null, null, null, null, null);
  151. if (c != null) {
  152. c.moveToFirst();
  153. }
  154. return c;
  155. }
  156.  
  157. // Get a specific row (by rowId)
  158. public Cursor getRow(long rowId) {
  159. String where = KEY_ROWID + "=" + rowId;
  160. Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
  161. where, null, null, null, null, null);
  162. if (c != null) {
  163. c.moveToFirst();
  164. }
  165. return c;
  166. }
  167.  
  168. // Change an existing row to be equal to new data.
  169. public boolean updateRow(long rowId, String websitename, String username, String password, String notes, String fav) {
  170. String where = KEY_ROWID + "=" + rowId;
  171.  
  172. // Create row's data:
  173. ContentValues newValues = new ContentValues();
  174. newValues.put(KEY_WEBSITENAME, websitename);
  175. newValues.put(KEY_USERNAME, username);
  176. newValues.put(KEY_PASSWORD, password);
  177. newValues.put(KEY_NOTES, notes);
  178. newValues.put(KEY_FAV, fav);
  179.  
  180. // Insert it into the database.
  181. return db.update(DATABASE_TABLE, newValues, where, null) != 0;
  182. }
  183.  
  184. private static class DatabaseHelper extends SQLiteOpenHelper
  185. {
  186. DatabaseHelper(Context context) {
  187. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  188. }
  189.  
  190. @Override
  191. public void onCreate(SQLiteDatabase _db) {
  192. _db.execSQL(DATABASE_CREATE_SQL);
  193. }
  194.  
  195. @Override
  196. public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
  197. Log.w(TAG, "Upgrading application's database from version " + oldVersion
  198. + " to " + newVersion + ", which will destroy all old data!");
  199.  
  200. // Destroy old database:
  201. _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
  202.  
  203. // Recreate new database:
  204. onCreate(_db);
  205. }
  206. }
  207. }
  208.  
  209. package com.passwordsavver.wahlfachprojekt;
  210.  
  211. import android.content.Intent;
  212. import android.support.v7.app.AppCompatActivity;
  213. import android.os.Bundle;
  214. import android.util.Base64;
  215. import android.view.Menu;
  216. import android.view.MenuItem;
  217. import android.view.View;
  218. import android.widget.Button;
  219. import android.widget.TableLayout;
  220. import android.widget.TableRow;
  221. import android.database.Cursor;
  222. import android.widget.TextView;
  223. import java.security.MessageDigest;
  224.  
  225. import javax.crypto.Cipher;
  226. import javax.crypto.spec.SecretKeySpec;
  227.  
  228. public class MainScreen extends AppCompatActivity {
  229. DBAdapter myDb;
  230. String passwordAES;
  231.  
  232. @Override
  233. protected void onCreate(Bundle savedInstanceState) {
  234. super.onCreate(savedInstanceState);
  235. setContentView(R.layout.activity_main_screen);
  236.  
  237. Intent intent = getIntent();
  238. passwordAES = intent.getStringExtra("Password");
  239.  
  240. openDB();
  241. populateButtons();
  242. }
  243.  
  244. @Override
  245. protected void onDestroy() {
  246. super.onDestroy();
  247. closeDB();
  248. }
  249.  
  250. private void openDB() {
  251. myDb = new DBAdapter(this);
  252. myDb.open();
  253. }
  254.  
  255. private void closeDB() {
  256. myDb.close();
  257. }
  258.  
  259. private void populateButtons() {
  260. Cursor cursor = myDb.getAllRows();
  261. int itemNumber = 0;
  262. int i = 0;
  263.  
  264. if (cursor.moveToFirst()) {
  265. do {
  266. itemNumber++;
  267. } while (cursor.moveToNext());
  268. }
  269.  
  270. int ids[] = new int[itemNumber];
  271. String aesWebsitenames[] = new String[itemNumber];
  272. String websitenames[] = new String[itemNumber];
  273. String aesFav[] = new String[itemNumber];
  274. String fav[] = new String[itemNumber];
  275.  
  276.  
  277. if (cursor.moveToFirst()) {
  278. do {
  279. ids[i] = cursor.getInt(DBAdapter.COL_ROWID);
  280. aesWebsitenames[i] = cursor.getString(DBAdapter.COL_WEBSITENAME);
  281. aesFav[i] = cursor.getString(DBAdapter.COL_FAV);
  282.  
  283. try
  284. {
  285. websitenames[i] = decrypt(passwordAES, aesWebsitenames[i]);
  286. fav[i] = decrypt(passwordAES, aesFav[i]);
  287. }
  288. catch(Exception e)
  289. {
  290.  
  291. }
  292.  
  293. i++;
  294. } while (cursor.moveToNext());
  295. }
  296.  
  297. i = 0;
  298.  
  299. int numCols = 1;
  300.  
  301. int numRows = itemNumber;
  302.  
  303.  
  304. TableLayout table = (TableLayout)findViewById(R.id.tableForButtons);
  305.  
  306. // Favourites
  307. int countFavourites = 0;
  308. for(int row = 0; row < numRows; row++)
  309. {
  310. TableRow tableRow = new TableRow(this);
  311. table.addView(tableRow);
  312.  
  313. if(fav[i] == "1") {
  314. Button button = new Button(this);
  315.  
  316. button.setText(websitenames[i]);
  317.  
  318.  
  319. // Make text not clip on small buttons
  320. button.setPadding(0, 0, 0, 0);
  321. tableRow.addView(button);
  322.  
  323. final int FINAL_ID = ids[i];
  324.  
  325.  
  326. button.setOnClickListener(new View.OnClickListener() {
  327. @Override
  328. public void onClick(View v) {
  329. Intent intent = new Intent(v.getContext(), ViewScreen.class);
  330. intent.putExtra("id", FINAL_ID);
  331. intent.putExtra("Password", passwordAES);
  332. startActivity(intent);
  333. }
  334. });
  335. }
  336. countFavourites++;
  337. i++;
  338. }
  339.  
  340. if(countFavourites == 0)
  341. {
  342. TextView txtNoFavourites = (TextView) findViewById(R.id.txtMainScreenNoFavourites);
  343. txtNoFavourites.setVisibility(View.VISIBLE);
  344. }
  345.  
  346. i = 0;
  347.  
  348. // Not Favourites
  349. for(int row = 0; row < numRows; row++)
  350. {
  351. TableRow tableRow = new TableRow(this);
  352. table.addView(tableRow);
  353. for(int col = 0; col < numCols; col++)
  354. {
  355. if(fav[i] == "0") {
  356. Button button = new Button(this);
  357.  
  358. button.setText(websitenames[i]);
  359.  
  360. // Make text not clip on small buttons
  361. button.setPadding(0, 0, 0, 0);
  362. tableRow.addView(button);
  363.  
  364. final int FINAL_ID = ids[i];
  365.  
  366. button.setOnClickListener(new View.OnClickListener() {
  367. @Override
  368. public void onClick(View v) {
  369. Intent intent = new Intent(v.getContext(), ViewScreen.class);
  370. intent.putExtra("id", FINAL_ID);
  371. intent.putExtra("Password", passwordAES);
  372. startActivity(intent);
  373. }
  374. });
  375. }
  376. }
  377. i++;
  378. }
  379.  
  380. }
  381.  
  382. @Override
  383. public boolean onCreateOptionsMenu(Menu menu) {
  384. getMenuInflater().inflate(R.menu.menu, menu);
  385. return true;
  386. }
  387.  
  388. @Override
  389. public boolean onOptionsItemSelected(MenuItem item) {
  390. int id = item.getItemId();
  391. Intent intent;
  392.  
  393. switch (id)
  394. {
  395. case R.id.action_offlinemode:
  396. intent = new Intent(this, OptionScreen.class);
  397. intent.putExtra("Password", passwordAES);
  398. startActivity(intent);
  399. break;
  400.  
  401. case R.id.action_allPasswords:
  402. intent = new Intent(this, MainScreen.class);
  403. intent.putExtra("Password", passwordAES);
  404. startActivity(intent);
  405. break;
  406.  
  407. case R.id.action_favorites:
  408.  
  409. break;
  410.  
  411. case R.id.action_addPassword:
  412. intent = new Intent(this, AddScreen2.class);
  413. intent.putExtra("Password", passwordAES);
  414. startActivity(intent);
  415. break;
  416.  
  417. case R.id.action_deleteAllPasswords:
  418. myDb.deleteAll();
  419. break;
  420.  
  421. case R.id.action_option:
  422. intent = new Intent(this, OptionScreen.class);
  423. intent.putExtra("Password", passwordAES);
  424. startActivity(intent);
  425. break;
  426.  
  427. case R.id.action_logout:
  428.  
  429. break;
  430.  
  431. default:
  432. return super.onOptionsItemSelected(item);
  433. }
  434.  
  435. return true;
  436. }
  437.  
  438. private String decrypt (String password, String data) throws Exception
  439. {
  440. SecretKeySpec key = generateKey(password);
  441. Cipher c = Cipher.getInstance("AES");
  442. c.init(Cipher.DECRYPT_MODE, key);
  443. byte[] decodedValue = Base64.decode(data, Base64.DEFAULT);
  444. byte[] decValue = c.doFinal(decodedValue);
  445. String decryptedValue = new String(decValue);
  446. return decryptedValue;
  447. }
  448.  
  449. private SecretKeySpec generateKey(String password) throws Exception
  450. {
  451. final MessageDigest digest = MessageDigest.getInstance("SHA-256");
  452. byte[] bytes = password.getBytes("UTF-8");
  453. digest.update(bytes, 0, bytes.length);
  454. byte[] key = digest.digest();
  455. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  456. return secretKeySpec;
  457. }
  458. }
Add Comment
Please, Sign In to add comment