Skip to content

Commit 42b29f8

Browse files
committed
文件加密解密的输出文件名可以进行预解析
1 parent 1bded5a commit 42b29f8

22 files changed

+46
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ CryptoApp安卓版
33

44
经过考虑,我决定开源CryptoApp-Android,虽然是学习Android开发时顺手开发的练手项目,但还是希望对大家有用。
55

6+
## 2021-01-08
7+
8+
文件加密解密功能中,输出文件名可以进行预先加密或解密了。
9+
610
## 2020-12-08
711

812
修改了主页

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111
minSdkVersion 21
1212
targetSdkVersion 29
1313
versionCode 1
14-
versionName "1.2.2"
14+
versionName "1.2.3"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}
99.7 KB
Loading

app/src/main/java/com/example/cryptoapp/FileActivity.kt

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package com.example.cryptoapp
33
import android.app.Activity
44
import android.content.Intent
55
import android.net.Uri
6+
import android.os.Build
67
import androidx.appcompat.app.AppCompatActivity
78
import android.os.Bundle
89
import android.os.Handler
910
import android.os.Message
11+
import android.util.Log
1012
import android.view.Menu
1113
import android.view.MenuItem
1214
import android.view.View
15+
import androidx.annotation.RequiresApi
1316
import androidx.core.view.GravityCompat
1417
import com.google.android.material.snackbar.Snackbar
1518
import kotlinx.android.synthetic.main.activity_file.*
@@ -19,6 +22,7 @@ import kotlinx.android.synthetic.main.activity_file.passwordEditText
1922
import kotlinx.android.synthetic.main.activity_main.drawerLayout
2023
import kotlinx.android.synthetic.main.activity_main.navView
2124
import kotlinx.android.synthetic.main.activity_main.toolbar
25+
import kotlinx.android.synthetic.main.activity_text.*
2226
import java.io.*
2327
import java.lang.ref.WeakReference
2428
import kotlin.concurrent.thread
@@ -90,6 +94,7 @@ class FileActivity : AppCompatActivity(), View.OnClickListener {
9094
decryptButton.setOnClickListener(this)
9195
}
9296

97+
@RequiresApi(Build.VERSION_CODES.O)
9398
override fun onClick(v: View?) {
9499
when (v?.id) {
95100
R.id.readFileButton -> {
@@ -141,11 +146,21 @@ class FileActivity : AppCompatActivity(), View.OnClickListener {
141146

142147
}
143148

149+
@RequiresApi(Build.VERSION_CODES.O)
144150
private fun setToFileUri() {
151+
val password = passwordEditText.text.toString()
152+
val readName = readPathEditText.text.toString()
153+
// 通过输入文件名是否包含特殊后缀来判断输出文件名默认值是加密还是解密后的字符串
154+
val outputName = if (readName.contains(".cf", ignoreCase = true)) {
155+
fileNameHandle(readName.substring(0, readName.lastIndexOf('.')), password, "DECRYPT")
156+
} else {
157+
fileNameHandle(readName, password, "ENCRYPT") + ".cf"
158+
}
159+
145160
val intent =Intent(Intent.ACTION_CREATE_DOCUMENT)
146161
intent.addCategory(Intent.CATEGORY_OPENABLE)
147162
intent.type = "*/*"
148-
intent.putExtra(Intent.EXTRA_TITLE,"OutPutName")
163+
intent.putExtra(Intent.EXTRA_TITLE, outputName)
149164
startActivityForResult(intent, REQUEST_CODE_FOR_CREATE_FILE)
150165
}
151166

@@ -172,6 +187,29 @@ class FileActivity : AppCompatActivity(), View.OnClickListener {
172187
}
173188
}
174189

190+
@RequiresApi(Build.VERSION_CODES.O)
191+
fun fileNameHandle(inputName: String, password:String, option: String): String {
192+
var outputName = ""
193+
if (option == "ENCRYPT") {
194+
outputName = try {
195+
StringCrypto(password).encrypt(inputName)
196+
} catch (e: Exception) {
197+
Log.d("Encrypt Error", "Encrypt input name exception!")
198+
"EncryptNameError"
199+
}
200+
} else if (option == "DECRYPT"){
201+
outputName = try {
202+
StringCrypto(password).decrypt(inputName)
203+
} catch (e: Exception) {
204+
Log.d("Decrypt Error", "Decrypt input name exception!")
205+
"DecryptNameError"
206+
}
207+
} else {
208+
outputName = ""
209+
}
210+
return outputName
211+
}
212+
175213
private fun fileHandle(uri: Uri, option: String): Boolean {
176214
val inputFileResolver = contentResolver.openFileDescriptor(uri, "r")
177215
val outputFileResolver = contentResolver.openFileDescriptor(toFileUri!!, "w")
21.9 KB
Loading
122 KB
Loading

app/src/main/res/layout/nav_header.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
android:id="@+id/iconImge"
1010
android:layout_width="70dp"
1111
android:layout_height="70dp"
12-
android:src="@drawable/nav_icon"
12+
android:src="@drawable/logo"
1313
android:layout_centerInParent="true" />
1414

1515
<TextView
@@ -26,7 +26,7 @@
2626
android:layout_width="wrap_content"
2727
android:layout_height="wrap_content"
2828
android:layout_above="@id/appInfoText"
29-
android:text="CryptoApp V1.2.2"
29+
android:text="CryptoApp V1.2.3"
3030
android:textColor="#FFF"
3131
android:textSize="14sp" />
3232

1.22 KB
Loading
5.25 KB
Loading
1.55 KB
Loading

0 commit comments

Comments
 (0)