Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Star1128 committed Jul 10, 2022
1 parent 4e4ad50 commit 6a06863
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 40 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@

- 主页Item长按显示密码

2.4

- 主页面Item添加删除按钮

3.0

- 引入加密算法



## 优化计划
Expand All @@ -32,7 +40,5 @@

根据不同重要性等级使用不同密码加密策略

主页面Item侧滑删除

搜索功能

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.ethan.passwordbox"
minSdkVersion 24
targetSdkVersion 30
versionCode 4
versionName "2.2"
versionCode 5
versionName "3.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
Expand Down
Binary file added app/release/app-release.apk
Binary file not shown.
20 changes: 20 additions & 0 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "com.ethan.passwordbox",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 5,
"versionName": "3.0",
"outputFile": "app-release.apk"
}
],
"elementType": "File"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "1c75e39910f7d9f886f8d3231605f2df",
"identityHash": "96772fc6966666afd1587e827aaceda7",
"entities": [
{
"tableName": "Item",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `appName` TEXT, `importanceId` INTEGER NOT NULL, `userName` TEXT, `password` TEXT)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `appName` TEXT, `importanceId` INTEGER NOT NULL, `userName` TEXT, `password` TEXT, `version` TEXT)",
"fields": [
{
"fieldPath": "id",
Expand Down Expand Up @@ -37,6 +37,12 @@
"columnName": "password",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "version",
"columnName": "version",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
Expand All @@ -52,7 +58,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '1c75e39910f7d9f886f8d3231605f2df')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '96772fc6966666afd1587e827aaceda7')"
]
}
}
12 changes: 11 additions & 1 deletion app/src/main/java/com/ethan/passwordbox/POJO/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public Item[] newArray(int size) {
private final int importanceId;
private final String userName;
private String password;
private String version;

public Item(String appName, String userName, int importanceId, String password) {
public Item(String appName, String userName, int importanceId, String password,String version) {
this.appName = appName;
this.userName = userName;
this.importanceId = importanceId;
this.password = password;
this.version=version;
}

protected Item(Parcel in) {
Expand Down Expand Up @@ -70,6 +72,14 @@ public int getImportanceId() {
return importanceId;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

@Override
public int describeContents() {
return 0;
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/ethan/passwordbox/config/Cons.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ public static class Importance {
public static final int MORE = 2;
public static final int NORMAL = 3;
}

public static class Time{
// 长按Item 密码展示时间
public static final int PSW_SHOW_TIME=1000;
}

public static class Encrypt{
public static final String PRIVATE_KEY="2000w11w28x17c05";
public static final String IV_PARAMETER="138e0321t6h3a7n7";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.*;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.ethan.passwordbox.POJO.Item;

Expand All @@ -11,19 +14,14 @@
*
* @author Ethan 2022/6/19
*/
@Database(version = 1, entities = {Item.class})
@Database(version = 2, entities = {Item.class})
public abstract class AppRoomDatabase extends RoomDatabase {
// static Migration mMigration_1_2 = new Migration(1, 2) {
// @Override
// public void migrate(@NonNull SupportSQLiteDatabase database) {
// database.beginTransaction();
// database.execSQL("CREATE TABLE ItemV1 (id INTEGER PRIMARY KEY AUTOINCREMENT not null, appName TEXT, importanceId INTEGER not null default \"null\", userName TEXT, password TEXT)");
// database.execSQL("INSERT INTO ItemV1(id,appName,userName,password) SELECT id,appName,userName,password FROM Item;");
// database.execSQL("DROP TABLE Item");
// database.execSQL("ALTER TABLE ItemV1 RENAME TO Item");
// database.endTransaction();
// }
// };
static Migration mMigration_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("alter table Item add column version text default '2.0'");
}
};
// 懒汉式单例
private static AppRoomDatabase mMyRoomDatabase = null;

Expand All @@ -32,6 +30,7 @@ public static AppRoomDatabase getMyRoomDatabase(Context context) {
mMyRoomDatabase = Room.databaseBuilder(context.getApplicationContext(),
AppRoomDatabase.class,
"PasswordBox")
.addMigrations(mMigration_1_2)
.build();
}
return mMyRoomDatabase;
Expand Down
94 changes: 94 additions & 0 deletions app/src/main/java/com/ethan/passwordbox/encrypt/AES.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.ethan.passwordbox.encrypt;

import android.util.Base64;
import android.util.Log;

import com.ethan.passwordbox.config.Cons;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
* NOTE:
*
* @author Ethan 2022/7/10
*/
public class AES {
private static final String TAG = "AESCBCUtils";

// CBC(Cipher Block Chaining, 加密块链)模式,PKCS5Padding补码方式
// AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
private static final String AES = "AES";

/**
* AES 加密
*
* @param strClearText 待加密内容
* @return 返回Base64转码后的加密数据
*/
public static String encrypt_AES(String strClearText) {

try {
byte[] raw = Cons.Encrypt.PRIVATE_KEY.getBytes();
// 创建AES密钥
SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
// 创建密码器
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
// 创建偏移量
IvParameterSpec iv = new IvParameterSpec(Cons.Encrypt.IV_PARAMETER.getBytes());
// 初始化加密器
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
// 执行加密操作
byte[] cipherText = cipher.doFinal(strClearText.getBytes());
Log.d(TAG, "encrypt result(not BASE64): " + Arrays.toString(cipherText));
// encode it by BASE64 again
String strBase64Content = Base64.encodeToString(cipherText, Base64.DEFAULT);
Log.d(TAG, "encrypt result(BASE64): " + strBase64Content);
// strBase64Content = strBase64Content.replaceAll(System.getProperty("line.separator"), "");

return strBase64Content;
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

/**
* AES 解密
*
* @param strCipherText 待解密内容
* @return 返回Base64转码后的加密数据
*/
public static String decrypt(String strCipherText) throws Exception {

try {
byte[] raw = Cons.Encrypt.PRIVATE_KEY.getBytes(StandardCharsets.US_ASCII);
// 创建AES秘钥
SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
// 创建密码器
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
// 创建偏移量
IvParameterSpec iv = new IvParameterSpec(Cons.Encrypt.IV_PARAMETER.getBytes());
// 初始化解密器
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
// 执行解密操作 decode by BASE64 first
byte[] cipherText = Base64.decode(strCipherText, Base64.DEFAULT);
Log.d(TAG, "BASE64 decode result(): " + Arrays.toString(cipherText));
byte[] clearText = cipher.doFinal(cipherText);
String strClearText = new String(clearText);
Log.d(TAG, "decrypt result: " + strClearText);

return strClearText;
} catch (Exception e) {
e.printStackTrace();
}

return null;
}
}
6 changes: 4 additions & 2 deletions app/src/main/java/com/ethan/passwordbox/ui/AddActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.ethan.passwordbox.config.MainApplication;
import com.ethan.passwordbox.POJO.Item;
import com.ethan.passwordbox.R;
import com.ethan.passwordbox.config.MainApplication;
import com.ethan.passwordbox.data.local.AppDao;
import com.ethan.passwordbox.data.local.AppRoomDatabase;
import com.ethan.passwordbox.databinding.ActivityAddBinding;
import com.ethan.passwordbox.encrypt.AES;

public class AddActivity extends AppCompatActivity {
ActivityAddBinding mBinding;
Expand Down Expand Up @@ -65,7 +66,8 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
read();

if (!TextUtils.isEmpty(appName) && !TextUtils.isEmpty(userName) && !TextUtils.isEmpty(password) && radioButtonId != -1) {
Item newItem = new Item(appName, userName, radio2ImportanceId(radioButtonId), password);
String psw_encrypt = AES.encrypt_AES(password);
Item newItem = new Item(appName, userName, radio2ImportanceId(radioButtonId), psw_encrypt, getString(R.string.version_code));
new Thread(() -> {
AppDao appDao = AppRoomDatabase.getMyRoomDatabase(MainApplication.mContext).appDao();
newItem.setId(appDao.insertItem(newItem));
Expand Down
Loading

0 comments on commit 6a06863

Please sign in to comment.