Skip to content

Commit 019d757

Browse files
author
Ryan
committed
Implement Asymmetric encryption and key generation
1 parent 9965dcf commit 019d757

36 files changed

+1603
-930
lines changed

.circleci/config.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ jobs:
66
- image: circleci/android:api-25-alpha
77
environment:
88
JVM_OPTS: -Xmx3200m
9-
KEYSTORE: ${HOME}/${CIRCLE_PROJECT_REPONAME}/keystore.jks
109
steps:
1110
- checkout
1211
- restore_cache:
13-
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
12+
key: jars-{{ checksum "build.gradle" }}-{{ checksum "easycrypt/build.gradle" }}
1413
- run:
1514
name: Download Keystore
1615
command: bash ./download_keystore.sh
@@ -20,15 +19,15 @@ jobs:
2019
- save_cache:
2120
paths:
2221
- ~/.gradle
23-
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
22+
key: jars-{{ checksum "build.gradle" }}-{{ checksum "easycrypt/build.gradle" }}
2423
- run:
2524
name: Run Tests
2625
command: ./gradlew lint test
2726
- store_artifacts:
28-
path: app/build/reports
27+
path: easycrypt/build/reports
2928
destination: reports
3029
- store_test_results:
31-
path: app/build/test-results
30+
path: easycrypt/build/test-results
3231
- run:
33-
name: Gradle build and upload to bintray
34-
command: ./gradlew clean build install bintrayUpload
32+
name: Gradle install and upload to bintray
33+
command: ./gradlew install bintrayUpload

.gitignore

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
# Built application files
22
*.apk
3-
*.ap_
4-
5-
# Files for the ART/Dalvik VM
6-
*.dex
7-
8-
# Java class files
9-
*.class
103

114
# Generated files
125
bin/
@@ -60,4 +53,4 @@ freeline_project_description.json
6053
/easycrypt/src/main/java/com/pvryan/easycrypt/ECryptAnalyzePassword.kt
6154
.idea/markdown-navigator.xml
6255
.idea/markdown-navigator/
63-
secure.properties
56+
secure.properties

README.md

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Easily encrypt, decrypt, or hash data in a very secure way.
1616
* Supports MD5, SHA1, and SHA2 hash functions
1717
* SecureRandom fixes on Android below KitKat
1818
* Generate key manually with SecureRandom or random.org
19+
* Asymmetric encryption with RSA
20+
* Auto handle large data by using hybrid asymmetric encryption
21+
* Supported RSA key sizes are 2048 bits and 4096 bits
1922

2023
## Install in Java app
2124
Add in your project's build.gradle
@@ -37,7 +40,7 @@ apply plugin: 'kotlin-android-extensions'
3740
3841
dependencies {
3942
...
40-
compile "com.pvryan.easycrypt:easycrypt:1.0.6"
43+
compile "com.pvryan.easycrypt:easycrypt:1.1.0"
4144
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2"
4245
compile "org.jetbrains.anko:anko-commons:0.10.1"
4346
...
@@ -49,18 +52,20 @@ Add in your app's build.gradle
4952
```gradle
5053
dependencies {
5154
...
52-
compile "com.pvryan.easycrypt:easycrypt:1.0.6"
55+
compile "com.pvryan.easycrypt:easycrypt:1.1.0"
5356
...
5457
}
5558
```
5659

5760
## Usage
5861
```kotlin
5962
val eCryptSymmetric = ECryptSymmetric()
63+
val eCryptAsymmetric = ECryptAsymmetric()
6064
val eCryptHash = ECryptHash()
6165
val eCryptPass = ECryptPasswords()
6266
```
6367

68+
### Symmetric key encryption
6469
#### Encrypt data
6570
```kotlin
6671
eCryptSymmetric.encrypt (input, password,
@@ -105,6 +110,62 @@ eCryptSymmetric.decrypt(input, password,
105110
)
106111
```
107112

113+
### Asymmetric key encryption
114+
#### Encrypt data
115+
```kotlin
116+
eCryptAsymmetric.generateKeyPair(object : ECryptRSAKeyPairListener {
117+
118+
override fun onSuccess(keyPair: KeyPair) {
119+
privateKey = keyPair.private as RSAPrivateKey // Save private key
120+
eCryptAsymmetric.encrypt(input, keyPair.public as RSAPublicKey,
121+
object : ECryptResultListener {
122+
123+
// Optional
124+
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
125+
126+
}
127+
128+
override fun <T> onSuccess(result: T) {
129+
130+
}
131+
132+
override fun onFailure(message: String, e: Exception) {
133+
134+
}
135+
},
136+
outputFile // Optional
137+
)
138+
}
139+
140+
override fun onFailure(message: String, e: Exception) {
141+
e.printStackTrace()
142+
}
143+
144+
}, keySize = eCryptAsymmetric.KeySizes._4096)
145+
```
146+
147+
#### Decrypt data
148+
```kotlin
149+
eCryptAsymmetric.decrypt(input, privateKey,
150+
object : ECryptResultListener {
151+
152+
// Optional
153+
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
154+
155+
}
156+
157+
override fun <T> onSuccess(result: T) {
158+
159+
}
160+
161+
override fun onFailure(message: String, e: Exception) {
162+
163+
}
164+
},
165+
outputFile // Optional
166+
)
167+
```
168+
108169
#### Hash data
109170
```kotlin
110171
eCryptHash.calculate(input, hashAlgorithm, // from ECryptHashAlgorithms
@@ -127,14 +188,16 @@ eCryptHash.calculate(input, hashAlgorithm, // from ECryptHashAlgorithms
127188
)
128189
```
129190

130-
------------------------------------------------------
131-
| Input | Output |
132-
|-----------------|----------------------------------|
133-
| File | outputFile |
134-
| FileInputStream | outputFile |
135-
| ByteArray | String (outputFile, if provided) |
136-
| String | String (outputFile, if provided) |
137-
| CharSequence | String (outputFile, if provided) |
191+
--------------------------------------------------------------
192+
| Input | Output |
193+
|-----------------------|------------------------------------|
194+
| File | outputFile |
195+
| FileInputStream | outputFile |
196+
| ByteArray | String or outputFile (if provided) |
197+
| ByteArrayInputStream | String or outputFile (if provided) |
198+
| String | String or outputFile (if provided) |
199+
| CharSequence | String or outputFile (if provided) |
200+
| Anything else | InvalidParameterException |
138201

139202
#### Generate key with SecureRandom (pseudo-random)
140203
```kotlin

0 commit comments

Comments
 (0)