├── settings.gradle
├── app
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ ├── layout
│ │ │ │ └── activity_encrypt.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── co
│ │ │ │ └── tob
│ │ │ │ └── encryption
│ │ │ │ ├── constants
│ │ │ │ └── Constants.kt
│ │ │ │ ├── utils
│ │ │ │ ├── Extensions.kt
│ │ │ │ └── Utils.kt
│ │ │ │ ├── AES_Salt_Encryption.kt
│ │ │ │ ├── RSA_KeystoreWrapper.kt
│ │ │ │ ├── AES_KeystoreWrapper.kt
│ │ │ │ └── EncryptActivity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── co
│ │ │ └── tob
│ │ │ └── encryption
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── co
│ │ └── tob
│ │ └── encryption
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── SceenShots
├── Screenshot_1570531917.jpg
├── Screenshot_1570531925.jpg
├── Screenshot_1570531938.jpg
└── Screenshot_1570531954.jpg
├── README.md
├── Encryption.iml
├── gradle.properties
├── gradlew.bat
└── gradlew
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name='Encryption'
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Encryption
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/SceenShots/Screenshot_1570531917.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/SceenShots/Screenshot_1570531917.jpg
--------------------------------------------------------------------------------
/SceenShots/Screenshot_1570531925.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/SceenShots/Screenshot_1570531925.jpg
--------------------------------------------------------------------------------
/SceenShots/Screenshot_1570531938.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/SceenShots/Screenshot_1570531938.jpg
--------------------------------------------------------------------------------
/SceenShots/Screenshot_1570531954.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/SceenShots/Screenshot_1570531954.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/charanolati/Android-encryption-sample/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #7c4dff
4 | #3f1dcb
5 | #b47cff
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Oct 08 15:53:38 IST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/co/tob/encryption/constants/Constants.kt:
--------------------------------------------------------------------------------
1 | package co.tob.charan.constants
2 |
3 | enum class EncType {
4 | AESENCRYPT,
5 | AESALTENCRYPT,
6 | RSAENCRYPT
7 | }
8 |
9 | object EncryptConstants{
10 | const val SALT_VALUE = "salt_value"
11 | const val IV_VALUE = "iv_value"
12 | const val ENC_VALUE = "encrypt_value"
13 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/co/tob/encryption/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package co.tob.encryption
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/co/tob/encryption/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package co.tob.encryption
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("co.tob.encryption", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # [Android-encryption-sample](https://medium.com/@charanolati/android-biometric-authentication-introduction-55103391ef0e)
2 |
3 | This repository is explained on [Medium](https://medium.com/@charanolati/android-biometric-authentication-introduction-55103391ef0e)
4 |
5 | **Material Design** is used for the UI Elements
6 |
7 | **Choose an Option to encrypt & Input Data**
8 |
9 | 
10 |
11 | **Decrypt data**
12 |
13 | 
14 | 
15 | 
16 |
--------------------------------------------------------------------------------
/app/src/main/java/co/tob/encryption/utils/Extensions.kt:
--------------------------------------------------------------------------------
1 | package co.tob.charan.utils
2 |
3 | import android.app.admin.DevicePolicyManager
4 | import android.content.Context
5 | import android.content.Intent
6 | import android.provider.Settings
7 | import android.view.View
8 | import android.view.inputmethod.InputMethodManager
9 | import android.widget.Toast
10 | import androidx.appcompat.app.AppCompatActivity
11 |
12 | fun String.toByteArray() = this.toByteArray(Charsets.UTF_8)
13 |
14 | fun ByteArray.fromBytetoString() = String(this,Charsets.UTF_8)
15 |
16 | fun Context.openLockScreenSettings() {
17 | val intent = Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD)
18 | startActivity(intent)
19 | }
20 |
21 | fun View.hideKeyboard() {
22 | val service: InputMethodManager? = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
23 | service?.hideSoftInputFromWindow(windowToken, 0)
24 | }
--------------------------------------------------------------------------------
/Encryption.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/java/co/tob/encryption/utils/Utils.kt:
--------------------------------------------------------------------------------
1 | package co.tob.charan.utils
2 |
3 | import android.app.KeyguardManager
4 | import android.content.Context
5 | import android.os.Build
6 | import androidx.appcompat.app.AlertDialog
7 | import co.tob.encryption.BuildConfig
8 | import kotlin.system.exitProcess
9 |
10 | fun hasMarshmallow() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
11 |
12 |
13 | //To check Device has set Screen lock or not
14 | fun isDeviceSecure(keyguardManager : KeyguardManager): Boolean = if (hasMarshmallow()) keyguardManager.isDeviceSecure else keyguardManager.isKeyguardSecure
15 |
16 | //To SHOW the Dialog to to Open settings to set a SCREEN LOCK
17 | fun showDeviceSecurityAlert(context: Context): AlertDialog {
18 | return AlertDialog.Builder(context)
19 | .setMessage("To Use the App\nSet a Password to Unlock the Screen")
20 | .setPositiveButton("Settings") { _, _ -> context.openLockScreenSettings() }
21 | .setNegativeButton("Exit") { _, _ -> run {
22 | exitProcess(0)
23 | }
24 | }
25 | .setCancelable(BuildConfig.DEBUG)
26 | .show()
27 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | apply plugin: 'kotlin-android'
4 |
5 | apply plugin: 'kotlin-android-extensions'
6 |
7 | android {
8 | compileSdkVersion 29
9 | buildToolsVersion "29.0.2"
10 | defaultConfig {
11 | applicationId "co.tob.encryption"
12 | minSdkVersion 19
13 | targetSdkVersion 29
14 | versionCode 1
15 | versionName "1.0"
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | }
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | }
25 |
26 | dependencies {
27 | implementation fileTree(dir: 'libs', include: ['*.jar'])
28 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
29 | implementation 'androidx.appcompat:appcompat:1.1.0'
30 | implementation 'androidx.core:core-ktx:1.1.0'
31 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
32 | testImplementation 'junit:junit:4.12'
33 | androidTestImplementation 'androidx.test:runner:1.2.0'
34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
35 |
36 | //Material Design
37 | api 'com.google.android.material:material:1.1.0-beta01'
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/co/tob/encryption/AES_Salt_Encryption.kt:
--------------------------------------------------------------------------------
1 | package co.tob.charan.encrytion
2 |
3 | import android.security.keystore.KeyProperties
4 | import androidx.annotation.RequiresApi
5 | import co.tob.charan.constants.EncryptConstants.ENC_VALUE
6 | import co.tob.charan.constants.EncryptConstants.IV_VALUE
7 | import co.tob.charan.constants.EncryptConstants.SALT_VALUE
8 | import co.tob.charan.utils.fromBytetoString
9 | import java.security.Key
10 | import java.security.SecureRandom
11 | import java.util.*
12 | import javax.crypto.Cipher
13 | import javax.crypto.SecretKeyFactory
14 | import javax.crypto.spec.IvParameterSpec
15 | import javax.crypto.spec.PBEKeySpec
16 | import javax.crypto.spec.SecretKeySpec
17 |
18 | @RequiresApi(23)
19 | class AesSaltEncryption {
20 |
21 | companion object{
22 | const val ALGORTIHM_TYPE = "PBKDF2WithHmacSHA1"
23 | const val CPR_TRANSFORMATION = "AES/CBC/PKCS7Padding"//API 23+ //https://miro.medium.com/max/2068/1*MNcknQeCrJMhTWx9JlpnKg.png
24 | const val ENCRYPT_PASSWORD = "charan12345"
25 | }
26 |
27 | fun encrypt(data: ByteArray): HashMap {
28 |
29 | val salt = ByteArray(256)
30 | SecureRandom().nextBytes(salt)
31 |
32 | val iv = ByteArray(16)
33 | SecureRandom().nextBytes(iv)
34 |
35 | val cipher = Cipher.getInstance(CPR_TRANSFORMATION)
36 | cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(salt), IvParameterSpec(iv))
37 |
38 | return hashMapOf(Pair(SALT_VALUE,salt),Pair(IV_VALUE,iv),Pair(ENC_VALUE,cipher.doFinal(data)))
39 |
40 | }
41 |
42 | fun decrypt(map: HashMap): String {
43 |
44 | val salt = map[SALT_VALUE]
45 | val iv = map[IV_VALUE]
46 | val encrypted = map[ENC_VALUE]
47 |
48 | val cipher = Cipher.getInstance(CPR_TRANSFORMATION)
49 | cipher.init(Cipher.DECRYPT_MODE, getSecretKey(salt!!), IvParameterSpec(iv))
50 |
51 | return cipher.doFinal(encrypted).fromBytetoString()
52 | }
53 |
54 |
55 | private fun getSecretKey(salt : ByteArray) : Key {
56 | val pbKeySpec = PBEKeySpec(ENCRYPT_PASSWORD.toCharArray(), salt, 1324, 256)
57 | val keyBytes = SecretKeyFactory.getInstance(ALGORTIHM_TYPE).generateSecret(pbKeySpec).encoded
58 | return SecretKeySpec(keyBytes, KeyProperties.KEY_ALGORITHM_AES)
59 | }
60 |
61 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/co/tob/encryption/RSA_KeystoreWrapper.kt:
--------------------------------------------------------------------------------
1 | package co.tob.charan.encrytion
2 |
3 | import android.annotation.TargetApi
4 | import android.security.keystore.KeyGenParameterSpec
5 | import android.security.keystore.KeyProperties
6 | import android.util.Base64
7 | import co.tob.charan.utils.hasMarshmallow
8 | import co.tob.charan.utils.toByteArray
9 | import java.security.*
10 | import javax.crypto.Cipher
11 |
12 | class RsaKeystoreWrapper {
13 |
14 | companion object{
15 | const val AES_NOPAD_TRANS = "RSA/ECB/PKCS1Padding"
16 | const val ANDROID_KEYSTORE = "AndroidKeyStore"
17 | const val KEY_ALIAS = "Keyalaisras"
18 | }
19 |
20 | private fun createKeyStore(): KeyStore {
21 | val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE)
22 | keyStore.load(null)
23 | return keyStore
24 | }
25 |
26 | fun createAsymmetricKeyPair(): KeyPair {
27 | val generator: KeyPairGenerator
28 |
29 | if (hasMarshmallow()) {
30 | generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, ANDROID_KEYSTORE)
31 | getKeyGenParameterSpec(generator)
32 | } else {
33 | generator = KeyPairGenerator.getInstance("RSA")
34 | generator.initialize(2048)
35 | }
36 |
37 | return generator.generateKeyPair()
38 | }
39 |
40 | @TargetApi(23)
41 | private fun getKeyGenParameterSpec(generator: KeyPairGenerator) {
42 |
43 | val builder = KeyGenParameterSpec.Builder(KEY_ALIAS,
44 | KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
45 | .setBlockModes(KeyProperties.BLOCK_MODE_ECB)
46 | //.setUserAuthenticationRequired(true)
47 | .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
48 |
49 | generator.initialize(builder.build())
50 | }
51 |
52 | fun getAsymmetricKeyPair(): KeyPair? {
53 | val keyStore: KeyStore = createKeyStore()
54 |
55 | val privateKey = keyStore.getKey(KEY_ALIAS, null) as PrivateKey?
56 | val publicKey = keyStore.getCertificate(KEY_ALIAS)?.publicKey
57 |
58 | return if (privateKey != null && publicKey != null) {
59 | KeyPair(publicKey, privateKey)
60 | } else {
61 | null
62 | }
63 | }
64 |
65 | fun removeKeyStoreKey() = createKeyStore().deleteEntry(KEY_ALIAS)
66 |
67 | fun encrypt(data: String, key: Key?): String {
68 | val cipher: Cipher = Cipher.getInstance(AES_NOPAD_TRANS)
69 | cipher.init(Cipher.ENCRYPT_MODE, key)
70 | val bytes = cipher.doFinal(data.toByteArray())
71 | return Base64.encodeToString(bytes, Base64.DEFAULT)
72 | }
73 |
74 | fun decrypt(data: String, key: Key?): String {
75 | val cipher: Cipher = Cipher.getInstance(AES_NOPAD_TRANS)
76 | cipher.init(Cipher.DECRYPT_MODE, key)
77 | val encryptedData = Base64.decode(data, Base64.DEFAULT)
78 | val decodedData = cipher.doFinal(encryptedData)
79 | return String(decodedData)
80 | }
81 |
82 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_encrypt.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
23 |
24 |
31 |
32 |
40 |
41 |
49 |
50 |
51 |
52 |
68 |
69 |
83 |
84 |
94 |
95 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/java/co/tob/encryption/AES_KeystoreWrapper.kt:
--------------------------------------------------------------------------------
1 | package co.tob.charan.encrytion
2 |
3 | import android.annotation.SuppressLint
4 | import android.security.keystore.KeyGenParameterSpec
5 | import android.security.keystore.KeyProperties
6 | import android.util.Base64
7 | import androidx.annotation.RequiresApi
8 | import co.tob.charan.constants.EncryptConstants.ENC_VALUE
9 | import co.tob.charan.constants.EncryptConstants.IV_VALUE
10 | import co.tob.charan.utils.fromBytetoString
11 | import co.tob.charan.utils.toByteArray
12 | import java.security.InvalidAlgorithmParameterException
13 | import java.security.KeyStore
14 | import java.security.NoSuchAlgorithmException
15 | import java.security.NoSuchProviderException
16 | import java.util.*
17 | import javax.crypto.Cipher
18 | import javax.crypto.KeyGenerator
19 | import javax.crypto.SecretKey
20 | import javax.crypto.spec.GCMParameterSpec
21 |
22 | //AES Encryption will be available after API 23+ (ANDROID M)
23 | class AesKeystoreWrapper {
24 |
25 | companion object{
26 | const val AES_NOPAD_TRANS = "AES/GCM/NoPadding" //Format - ”Algorithm/Mode/Padding”
27 | const val ANDROID_KEYSTORE = "AndroidKeyStore"
28 | const val KEY_ALIAS = "Keyalaisasf"
29 | }
30 |
31 | private fun createKeyStore(): KeyStore {
32 | val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE)
33 | keyStore.load(null)
34 | return keyStore
35 | }
36 |
37 | //@Throws(NoSuchAlgorithmException::class, NoSuchProviderException::class, InvalidAlgorithmParameterException::class)
38 | @RequiresApi(23)
39 | fun createSymmetricKey() : SecretKey {
40 | try{
41 | val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEYSTORE)
42 |
43 | val keyGenParameterSpec = KeyGenParameterSpec.Builder(
44 | KEY_ALIAS,
45 | KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
46 | .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
47 | .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
48 | //.setUserAuthenticationRequired(true) // requires lock screen, invalidated if lock screen is disabled
49 | //.setUserAuthenticationValidityDurationSeconds(120) // only available x seconds from password authentication. -1 requires finger print - every time
50 | // .setKeySize(256) // Set key size
51 | //To Set Certificate Values instead of maual initialization of certificate
52 | // .setCertificateNotBefore(startDate) // By default, this date is Jan 1 1970.
53 | // .setCertificateNotAfter(endDate) // By default, this date is Jan 1 2048.
54 | // .setCertificateSerialNumber(number) // By default, the serial number is 1.
55 | // .setCertificateSubject(x500Principal) // By default, the subject is CN=fake.
56 |
57 | .setRandomizedEncryptionRequired(true) // 4 different ciphertext for same plaintext on each call
58 | .build()
59 | keyGenerator.init(keyGenParameterSpec)
60 | return keyGenerator.generateKey()
61 | } catch (e: NoSuchAlgorithmException) {
62 | throw RuntimeException("Failed to create a symmetric key", e)
63 | } catch (e: NoSuchProviderException) {
64 | throw RuntimeException("Failed to create a symmetric key", e)
65 | } catch (e: InvalidAlgorithmParameterException) {
66 | throw RuntimeException("Failed to create a symmetric key", e)
67 | }
68 | }
69 |
70 | fun decryptData(hashMap: HashMap): String {
71 |
72 | val encryptedBytes = Base64.decode(hashMap[ENC_VALUE],Base64.NO_WRAP)
73 | val ivBytes = Base64.decode(hashMap[IV_VALUE],Base64.NO_WRAP)
74 |
75 | val cipher = Cipher.getInstance(AES_NOPAD_TRANS)
76 | cipher.init(Cipher.DECRYPT_MODE, getSymmetricKey(), GCMParameterSpec(128, ivBytes))
77 |
78 | return cipher.doFinal(encryptedBytes).fromBytetoString()
79 | }
80 |
81 | fun encryptData(data: ByteArray): HashMap {
82 |
83 | val cipher = Cipher.getInstance(AES_NOPAD_TRANS)
84 | cipher.init(Cipher.ENCRYPT_MODE, getSymmetricKey())
85 |
86 | val eiv = (Base64.encodeToString(cipher.iv,Base64.NO_WRAP)).toByteArray()
87 | val edata = (Base64.encodeToString(cipher.doFinal(data),Base64.NO_WRAP)).toByteArray()
88 |
89 | return hashMapOf(Pair(IV_VALUE,eiv),Pair(ENC_VALUE,edata))
90 | }
91 |
92 | fun decryptNoBase(ivBytes : ByteArray,encryptedBytes : ByteArray): String {
93 |
94 | val cipher = Cipher.getInstance(AES_NOPAD_TRANS)
95 | cipher.init(Cipher.DECRYPT_MODE, getSymmetricKey(), GCMParameterSpec(128, ivBytes))
96 |
97 | return cipher.doFinal(encryptedBytes).fromBytetoString()
98 | }
99 |
100 | @SuppressLint("NewApi")
101 | fun getSymmetricKey(): SecretKey {
102 | /*val keysore = keyStore.getEntry(KEY_ALIAS, null) as KeyStore.SecretKeyEntry
103 | return keysore.secretKey*/
104 |
105 | val keyStore = createKeyStore()
106 |
107 | if(!isKeyExists(keyStore)){
108 | createSymmetricKey()
109 | }
110 |
111 | return keyStore.getKey(KEY_ALIAS,null) as SecretKey
112 | }
113 |
114 | fun removeKeyStoreKey() {
115 | val keyStore = createKeyStore()
116 |
117 | if(isKeyExists(keyStore)) {
118 | keyStore.deleteEntry(KEY_ALIAS)
119 | }
120 | }
121 |
122 | fun isKeyExists(keyStore : KeyStore): Boolean {
123 | val aliases = keyStore.aliases()
124 | while (aliases.hasMoreElements()) {
125 | return (KEY_ALIAS == aliases.nextElement())
126 | }
127 | return false
128 | }
129 |
130 | fun getCipher(): Cipher {
131 | val key = getSymmetricKey()
132 | val cipher = Cipher.getInstance(AES_NOPAD_TRANS)
133 | cipher.init(Cipher.ENCRYPT_MODE, key)
134 |
135 | return cipher
136 | }
137 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/co/tob/encryption/EncryptActivity.kt:
--------------------------------------------------------------------------------
1 | package co.tob.encryption
2 |
3 | import android.annotation.SuppressLint
4 | import android.app.KeyguardManager
5 | import android.content.Context
6 | import android.os.Build
7 | import android.os.Bundle
8 | import android.view.View
9 | import android.widget.Toast
10 | import androidx.appcompat.app.AppCompatActivity
11 | import co.tob.charan.constants.EncType
12 | import co.tob.charan.constants.EncryptConstants.ENC_VALUE
13 | import co.tob.charan.constants.EncryptConstants.IV_VALUE
14 | import co.tob.charan.constants.EncryptConstants.SALT_VALUE
15 | import co.tob.charan.encrytion.AesKeystoreWrapper
16 | import co.tob.charan.encrytion.AesSaltEncryption
17 | import co.tob.charan.encrytion.RsaKeystoreWrapper
18 | import co.tob.charan.utils.*
19 | import kotlinx.android.synthetic.main.activity_encrypt.*
20 | import java.security.KeyPair
21 |
22 | @Suppress("UNCHECKED_CAST")
23 | class EncryptActivity : AppCompatActivity() {
24 |
25 | private var isencrypt = true
26 | private lateinit var dataHashMap : HashMap
27 |
28 | private var encType = EncType.AESENCRYPT
29 |
30 | private lateinit var rsaKeystoreWrapper : RsaKeystoreWrapper
31 | private lateinit var rsaKeyPair : KeyPair
32 | private var encrypteData : String = ""
33 |
34 | private lateinit var aesKeystoreWrapper: AesKeystoreWrapper
35 |
36 | private lateinit var aesSaltEncryption: AesSaltEncryption
37 |
38 | @SuppressLint("SetTextI18n")
39 | override fun onCreate(savedInstanceState: Bundle?) {
40 | super.onCreate(savedInstanceState)
41 | setContentView(R.layout.activity_encrypt)
42 |
43 | //To Check Lock Screen Enabled or not
44 | val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
45 | if(!isDeviceSecure(keyguardManager)) showDeviceSecurityAlert(this)
46 |
47 | if(Build.VERSION.SDK_INT >= 23) {
48 | aesToggleBtn.isChecked = true
49 |
50 | aesKeystoreWrapper = AesKeystoreWrapper()
51 | aesSaltEncryption = AesSaltEncryption()
52 |
53 | }else{
54 | aesSaltToggleBtn.isEnabled = false
55 | aesToggleBtn.isEnabled = false
56 | aesSaltToggleBtn.visibility = View.GONE
57 | aesToggleBtn.visibility = View.GONE
58 |
59 | rsaToggleBtn.isChecked = true
60 | encType = EncType.RSAENCRYPT
61 | }
62 |
63 | rsaKeystoreWrapper = RsaKeystoreWrapper()
64 |
65 | encryptToggleGrp.addOnButtonCheckedListener { _, checkedId, isChecked ->
66 | run {
67 | if(isChecked) {
68 |
69 | encType =
70 | when (checkedId) {
71 | aesToggleBtn.id -> EncType.AESENCRYPT
72 | aesSaltToggleBtn.id -> EncType.AESALTENCRYPT
73 | else -> EncType.RSAENCRYPT
74 | }
75 |
76 | isencrypt = true
77 | dataHashMap = HashMap()
78 | encrypteData = ""
79 | encryptButton.text = "Encrypt"
80 | inputEt.text = null
81 | dataTV.text = null
82 | }
83 | }
84 | }
85 | }
86 |
87 | @SuppressLint("SetTextI18n")
88 | fun onEncyptClicked(v : View){
89 |
90 | v.hideKeyboard()
91 |
92 | if(isencrypt){
93 |
94 | val data: String = inputEt.text.toString()
95 | if(data.isEmpty()){
96 | Toast.makeText(this,"Enter Data",Toast.LENGTH_SHORT).show()
97 | return
98 | }
99 |
100 | val response = encryptData(data)
101 |
102 | encryptButton.text = "Decrypt"
103 | isencrypt = false
104 | if(response is HashMap<*, *>) {
105 | dataHashMap = response as HashMap
106 |
107 | val ss: StringBuilder = StringBuilder()
108 | ss.append("$encType EN-crypted Data \n\nIV - ${dataHashMap[IV_VALUE]!!.fromBytetoString()}" +
109 | "\nENC - ${dataHashMap[ENC_VALUE]!!.fromBytetoString()}")
110 | if(encType == EncType.AESALTENCRYPT){
111 | ss.append("\nSLAT - ${dataHashMap[SALT_VALUE]!!.fromBytetoString()}")
112 | }
113 |
114 | dataTV.text = ss.toString()
115 | } else if(response is String) {
116 | encrypteData = response
117 | dataTV.text = "$encType EN-crypted Data \n\nENC - $response"
118 | }
119 |
120 | }else{
121 |
122 | val data = if(encType == EncType.AESENCRYPT || encType == EncType.AESALTENCRYPT ) { decryptData(dataHashMap)!! }else{ decryptData(encrypteData)}
123 |
124 | encryptButton.text = "Encrypt"
125 | isencrypt = true
126 |
127 | inputEt.text = null
128 | dataTV.text = "$encType DE-crypted Data \n\nDEC - $data"
129 |
130 | }
131 | }
132 |
133 | @SuppressLint("NewApi")
134 | private fun encryptData(data: String) : Any? {
135 |
136 | return when (encType) {
137 | EncType.AESENCRYPT -> {
138 | aesKeystoreWrapper.encryptData(data.toByteArray())
139 | }
140 | EncType.AESALTENCRYPT -> aesSaltEncryption.encrypt(data.toByteArray())
141 | else -> {
142 |
143 | if(hasMarshmallow()) {
144 | rsaKeystoreWrapper.createAsymmetricKeyPair()
145 | rsaKeyPair = rsaKeystoreWrapper.getAsymmetricKeyPair()!!
146 | }else{
147 | rsaKeyPair = rsaKeystoreWrapper.createAsymmetricKeyPair()
148 | }
149 | rsaKeystoreWrapper.encrypt(data,rsaKeyPair.public)
150 | }
151 | }
152 | }
153 |
154 | @SuppressLint("NewApi")
155 | private fun decryptData(data: Any) : String? {
156 | return when (encType) {
157 | EncType.AESENCRYPT -> aesKeystoreWrapper.decryptData(data as HashMap)
158 | EncType.AESALTENCRYPT -> aesSaltEncryption.decrypt(data as HashMap)
159 | else -> rsaKeystoreWrapper.decrypt(data as String, rsaKeyPair.private)
160 | }
161 | }
162 |
163 | @Suppress("SENSELESS_COMPARISON")
164 | override fun onDestroy() {
165 | if(::rsaKeyPair.isInitialized)rsaKeystoreWrapper.removeKeyStoreKey()
166 | super.onDestroy()
167 | }
168 |
169 | }
--------------------------------------------------------------------------------