├── 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 | ![choose](https://github.com/charanolati/Android-encryption-sample/blob/master/SceenShots/Screenshot_1570531925.jpg) 10 | 11 | **Decrypt data** 12 | 13 | ![choose](https://github.com/charanolati/Android-encryption-sample/blob/master/SceenShots/Screenshot_1570531917.jpg) 14 | ![choose](https://github.com/charanolati/Android-encryption-sample/blob/master/SceenShots/Screenshot_1570531938.jpg) 15 | ![choose](https://github.com/charanolati/Android-encryption-sample/blob/master/SceenShots/Screenshot_1570531954.jpg) 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 | 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 |