· 7 years ago · Nov 12, 2018, 09:26 PM
1package com.example.szef.zadaniebsm;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.SQLException;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.util.Base64;
10import android.util.Log;
11import android.widget.Toast;
12
13import java.io.UnsupportedEncodingException;
14import java.security.NoSuchAlgorithmException;
15import java.security.SecureRandom;
16import java.security.spec.AlgorithmParameterSpec;
17
18import javax.crypto.Cipher;
19import javax.crypto.spec.IvParameterSpec;
20import javax.crypto.spec.SecretKeySpec;
21
22public class dataBaseHelper extends SQLiteOpenHelper {
23
24 private static final String DATABASE_NAME = "bsm.sqlite";
25 private static final String TABLE_NAME = "bsm_table";
26 private static final String KEY_ROWID = "_id";
27 String Key = "ABCDEFGHIJKLMNOPQRSTUVEXYZABCDEF";
28 String IV = "1234567890ABCDEF";
29 private String hash1;
30 MainActivity main;
31
32// public String hashing() {
33
34// SecureRandom rand = new SecureRandom();
35// byte bytes[] = new byte[512];
36// rand.nextBytes(bytes);
37// hash = bytes.toString();
38
39// }
40
41 public dataBaseHelper(Context context) {
42 super(context,DATABASE_NAME,null,1);
43
44 }
45
46 @Override
47 public void onCreate(SQLiteDatabase db) {
48 main = new MainActivity();
49 db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,PASSWORD TEXT,HASH TEXT,CONTENT TEXT)");
50 }
51
52 @Override
53 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
54 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
55 onCreate(db);
56
57 }
58 // inserting password
59 public boolean insertBsm (String password) throws NoSuchAlgorithmException {
60 SQLiteDatabase db = this.getWritableDatabase();
61 ContentValues contentValues = new ContentValues();
62 System.out.println("inserted"+MainActivity.md5(hash1,password));
63 contentValues.put("PASSWORD",MainActivity.md5(hash1,password));
64 contentValues.put("HASH",hash1);
65 long result = db.insert(TABLE_NAME,null,contentValues);
66 if(result == -1 ) {
67 return false;
68 } else {
69 return true;
70 }
71
72 }
73
74 // checking password
75 public boolean checkPassword(String password) throws NoSuchAlgorithmException {
76 password = MainActivity.md5(hash1,password);
77 SQLiteDatabase db = this.getWritableDatabase();
78 Cursor cursor = db.rawQuery("Select * From " +TABLE_NAME+ " where PASSWORD "+ " = ?" ,new String[]{password});
79 if (cursor.getCount()>0) {
80 return false;
81 } else {
82 return true;
83 }
84
85 }
86 // Changing password
87 public boolean change(String password,String newPassword) throws NoSuchAlgorithmException {
88 password = MainActivity.md5(hash1,password);
89 String newPass = MainActivity.md5(hash1, newPassword);
90 SQLiteDatabase db = this.getWritableDatabase();
91 Cursor cursor = db.rawQuery("Select * From " +TABLE_NAME+ " where PASSWORD "+ " = ?" ,new String[]{password});
92 if(cursor.getCount()>0) {
93 ContentValues values = new ContentValues();
94 values.put("PASSWORD",newPass);
95 values.put("HASH",hash1);
96 db.update(TABLE_NAME,values,"PASSWORD=?",new String[]{password});
97 return false;
98 } else {
99 System.out.println("Haslo nie wystąpiło");
100 return true;
101 }
102
103 }
104 // inserting Note
105 public boolean insertContent (String content) {
106 SQLiteDatabase db = this.getWritableDatabase();
107 ContentValues contentValues = new ContentValues();
108 String EncryptText = encrypt(content,Key,IV);
109 contentValues.put("CONTENT",EncryptText);
110 long result = db.update(TABLE_NAME,contentValues,"ID=1",null);
111 if(result == -1) {
112 return false;
113 } else {
114 return true;
115 }
116
117 }
118 // encrypting note
119 public static String encrypt(String strToEncrypt, String strKey, String strIV)
120 {
121 try {
122 byte[] ivByte = strIV.getBytes("UTF-8");
123 AlgorithmParameterSpec mAlgorithmParameterSpec = new IvParameterSpec(ivByte);
124
125 byte[] keyByte = strKey.getBytes("UTF-8");
126 SecretKeySpec secretKey = new SecretKeySpec(keyByte, "AES");
127
128 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
129 cipher.init(Cipher.ENCRYPT_MODE, secretKey, mAlgorithmParameterSpec);
130
131 byte[] byteToEncrypt = strToEncrypt.getBytes("UTF-8");
132 byte[] encryptByte = cipher.doFinal(byteToEncrypt);
133
134 String encryptString = Base64.encodeToString(encryptByte, Base64.DEFAULT);
135 return encryptString;
136
137 } catch (NoSuchAlgorithmException e) {
138 e.printStackTrace();
139 } catch (UnsupportedEncodingException e) {
140 e.printStackTrace();
141 } catch (Exception e) {
142 e.printStackTrace();
143 }
144 return null;
145 }
146
147}