· 7 years ago · Nov 12, 2018, 10:02 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.view.View;
12import android.widget.Toast;
13
14import java.io.UnsupportedEncodingException;
15import java.security.NoSuchAlgorithmException;
16import java.security.SecureRandom;
17import java.security.spec.AlgorithmParameterSpec;
18
19import javax.crypto.Cipher;
20import javax.crypto.spec.IvParameterSpec;
21import javax.crypto.spec.SecretKeySpec;
22
23public class dataBaseHelper extends SQLiteOpenHelper {
24
25 private static final String DATABASE_NAME = "bsm.sqlite";
26 private static final String TABLE_NAME = "bsm_table";
27 private static final String KEY_ROWID = "_id";
28 String Key = "ABCDEFGHIJKLMNOPQRSTUVEXYZABCDEF";
29 String IV = "1234567890ABCDEF";
30 private String hash1;
31 MainActivity main;
32
33// public String hashing() {
34
35// SecureRandom rand = new SecureRandom();
36// byte bytes[] = new byte[512];
37// rand.nextBytes(bytes);
38// hash = bytes.toString();
39
40// }
41
42 public dataBaseHelper(Context context) {
43 super(context,DATABASE_NAME,null,1);
44
45 }
46
47 @Override
48 public void onCreate(SQLiteDatabase db) {
49 main = new MainActivity();
50 db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,PASSWORD TEXT,HASH TEXT,CONTENT TEXT)");
51 }
52
53 @Override
54 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
55 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
56 onCreate(db);
57
58 }
59 // inserting password
60 public boolean insertBsm (String password) throws NoSuchAlgorithmException {
61 SQLiteDatabase db = this.getWritableDatabase();
62 ContentValues contentValues = new ContentValues();
63 System.out.println("inserted"+MainActivity.md5(hash1,password));
64 contentValues.put("PASSWORD",MainActivity.md5(hash1,password));
65 contentValues.put("HASH",hash1);
66 long result = db.insert(TABLE_NAME,null,contentValues);
67 if(result == -1 ) {
68 return false;
69 } else {
70 return true;
71 }
72
73 }
74
75 // checking password
76 public boolean checkPassword(String password) throws NoSuchAlgorithmException {
77 password = MainActivity.md5(hash1,password);
78 SQLiteDatabase db = this.getWritableDatabase();
79 Cursor cursor = db.rawQuery("Select * From " +TABLE_NAME+ " where PASSWORD "+ " = ?" ,new String[]{password});
80 if (cursor.getCount()>0) {
81 return false;
82 } else {
83 return true;
84 }
85
86 }
87 // Changing password
88 public boolean change(String password,String newPassword) throws NoSuchAlgorithmException {
89 password = MainActivity.md5(hash1,password);
90 String newPass = MainActivity.md5(hash1, newPassword);
91 SQLiteDatabase db = this.getWritableDatabase();
92 Cursor cursor = db.rawQuery("Select * From " +TABLE_NAME+ " where PASSWORD "+ " = ?" ,new String[]{password});
93 if(cursor.getCount()>0) {
94 ContentValues values = new ContentValues();
95 values.put("PASSWORD",newPass);
96 values.put("HASH",hash1);
97 db.update(TABLE_NAME,values,"PASSWORD=?",new String[]{password});
98 return false;
99 } else {
100 System.out.println("Haslo nie wystąpiło");
101 return true;
102 }
103
104 }
105 // inserting Note
106 public boolean insertContent (String content) {
107 SQLiteDatabase db = this.getWritableDatabase();
108 ContentValues contentValues = new ContentValues();
109 String EncryptText = encrypt(content,Key,IV);
110 contentValues.put("CONTENT",EncryptText);
111 long result = db.update(TABLE_NAME,contentValues,"ID=1",null);
112 if(result == -1) {
113 return false;
114 } else {
115 return true;
116 }
117
118 }
119 // encrypting note
120 public static String encrypt(String strToEncrypt, String strKey, String strIV)
121 {
122 try {
123 byte[] ivByte = strIV.getBytes("UTF-8");
124 AlgorithmParameterSpec mAlgorithmParameterSpec = new IvParameterSpec(ivByte);
125
126 byte[] keyByte = strKey.getBytes("UTF-8");
127 SecretKeySpec secretKey = new SecretKeySpec(keyByte, "AES");
128
129 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
130 cipher.init(Cipher.ENCRYPT_MODE, secretKey, mAlgorithmParameterSpec);
131
132 byte[] byteToEncrypt = strToEncrypt.getBytes("UTF-8");
133 byte[] encryptByte = cipher.doFinal(byteToEncrypt);
134
135 String encryptString = Base64.encodeToString(encryptByte, Base64.DEFAULT);
136 return encryptString;
137
138 } catch (NoSuchAlgorithmException e) {
139 e.printStackTrace();
140 } catch (UnsupportedEncodingException e) {
141 e.printStackTrace();
142 } catch (Exception e) {
143 e.printStackTrace();
144 }
145 return null;
146 }
147
148 public String showMsg()
149 {
150 String Key = "ABCDEFGHIJKLMNOPQRSTUVEXYZABCDEF";
151 String IV = "1234567890ABCDEF";
152 String selectQuery = "SELECT CONTENT FROM " + TABLE_NAME + " WHERE ID " + " = '" + 1 +"'";
153 SQLiteDatabase db = this.getWritableDatabase();
154 Cursor cursor = db.rawQuery(selectQuery, null);
155 System.out.println(cursor.getCount());
156 cursor.moveToFirst();
157 String dcrypt = decrypt(cursor.getString(0),Key,IV);
158 return dcrypt;
159// Toast.makeText(notepad.this, dcrypt, Toast.LENGTH_LONG).show();
160
161 }
162 private static String decrypt(String strToDecrypt, String strKey, String strIV)
163 {
164 try {
165 byte[] ivByte = strIV.getBytes("UTF-8");
166 AlgorithmParameterSpec mAlgorithmParameterSpec = new IvParameterSpec(ivByte);
167
168 byte[] keyByte = strKey.getBytes("UTF-8");
169 SecretKeySpec mSecretKeySpec = new SecretKeySpec(keyByte, "AES");
170
171 Cipher mCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
172 mCipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec, mAlgorithmParameterSpec);
173
174 byte[] byteToDecrypt = Base64.decode(strToDecrypt, Base64.DEFAULT);
175 byte[] decryptByte = mCipher.doFinal(byteToDecrypt);
176
177 String decryptString = new String(decryptByte,"UTF-8");
178 return decryptString;
179
180 } catch (NoSuchAlgorithmException e) {
181 e.printStackTrace();
182 } catch (UnsupportedEncodingException e) {
183 e.printStackTrace();
184 } catch (Exception e) {
185 e.printStackTrace();
186 }
187 return null;
188 }
189
190}