Skip to content

Commit

Permalink
Implement Asymmetric encryption and key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan committed Aug 4, 2017
1 parent 9965dcf commit 019d757
Show file tree
Hide file tree
Showing 36 changed files with 1,603 additions and 930 deletions.
13 changes: 6 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ jobs:
- image: circleci/android:api-25-alpha
environment:
JVM_OPTS: -Xmx3200m
KEYSTORE: ${HOME}/${CIRCLE_PROJECT_REPONAME}/keystore.jks
steps:
- checkout
- restore_cache:
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
key: jars-{{ checksum "build.gradle" }}-{{ checksum "easycrypt/build.gradle" }}
- run:
name: Download Keystore
command: bash ./download_keystore.sh
Expand All @@ -20,15 +19,15 @@ jobs:
- save_cache:
paths:
- ~/.gradle
key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
key: jars-{{ checksum "build.gradle" }}-{{ checksum "easycrypt/build.gradle" }}
- run:
name: Run Tests
command: ./gradlew lint test
- store_artifacts:
path: app/build/reports
path: easycrypt/build/reports
destination: reports
- store_test_results:
path: app/build/test-results
path: easycrypt/build/test-results
- run:
name: Gradle build and upload to bintray
command: ./gradlew clean build install bintrayUpload
name: Gradle install and upload to bintray
command: ./gradlew install bintrayUpload
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# Built application files
*.apk
*.ap_

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
Expand Down Expand Up @@ -60,4 +53,4 @@ freeline_project_description.json
/easycrypt/src/main/java/com/pvryan/easycrypt/ECryptAnalyzePassword.kt
.idea/markdown-navigator.xml
.idea/markdown-navigator/
secure.properties
secure.properties
83 changes: 73 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Easily encrypt, decrypt, or hash data in a very secure way.
* Supports MD5, SHA1, and SHA2 hash functions
* SecureRandom fixes on Android below KitKat
* Generate key manually with SecureRandom or random.org
* Asymmetric encryption with RSA
* Auto handle large data by using hybrid asymmetric encryption
* Supported RSA key sizes are 2048 bits and 4096 bits

## Install in Java app
Add in your project's build.gradle
Expand All @@ -37,7 +40,7 @@ apply plugin: 'kotlin-android-extensions'
dependencies {
...
compile "com.pvryan.easycrypt:easycrypt:1.0.6"
compile "com.pvryan.easycrypt:easycrypt:1.1.0"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2"
compile "org.jetbrains.anko:anko-commons:0.10.1"
...
Expand All @@ -49,18 +52,20 @@ Add in your app's build.gradle
```gradle
dependencies {
...
compile "com.pvryan.easycrypt:easycrypt:1.0.6"
compile "com.pvryan.easycrypt:easycrypt:1.1.0"
...
}
```

## Usage
```kotlin
val eCryptSymmetric = ECryptSymmetric()
val eCryptAsymmetric = ECryptAsymmetric()
val eCryptHash = ECryptHash()
val eCryptPass = ECryptPasswords()
```

### Symmetric key encryption
#### Encrypt data
```kotlin
eCryptSymmetric.encrypt (input, password,
Expand Down Expand Up @@ -105,6 +110,62 @@ eCryptSymmetric.decrypt(input, password,
)
```

### Asymmetric key encryption
#### Encrypt data
```kotlin
eCryptAsymmetric.generateKeyPair(object : ECryptRSAKeyPairListener {

override fun onSuccess(keyPair: KeyPair) {
privateKey = keyPair.private as RSAPrivateKey // Save private key
eCryptAsymmetric.encrypt(input, keyPair.public as RSAPublicKey,
object : ECryptResultListener {

// Optional
override fun onProgress(newBytes: Int, bytesProcessed: Long) {

}

override fun <T> onSuccess(result: T) {

}

override fun onFailure(message: String, e: Exception) {

}
},
outputFile // Optional
)
}

override fun onFailure(message: String, e: Exception) {
e.printStackTrace()
}

}, keySize = eCryptAsymmetric.KeySizes._4096)
```

#### Decrypt data
```kotlin
eCryptAsymmetric.decrypt(input, privateKey,
object : ECryptResultListener {

// Optional
override fun onProgress(newBytes: Int, bytesProcessed: Long) {

}

override fun <T> onSuccess(result: T) {

}

override fun onFailure(message: String, e: Exception) {

}
},
outputFile // Optional
)
```

#### Hash data
```kotlin
eCryptHash.calculate(input, hashAlgorithm, // from ECryptHashAlgorithms
Expand All @@ -127,14 +188,16 @@ eCryptHash.calculate(input, hashAlgorithm, // from ECryptHashAlgorithms
)
```

------------------------------------------------------
| Input | Output |
|-----------------|----------------------------------|
| File | outputFile |
| FileInputStream | outputFile |
| ByteArray | String (outputFile, if provided) |
| String | String (outputFile, if provided) |
| CharSequence | String (outputFile, if provided) |
--------------------------------------------------------------
| Input | Output |
|-----------------------|------------------------------------|
| File | outputFile |
| FileInputStream | outputFile |
| ByteArray | String or outputFile (if provided) |
| ByteArrayInputStream | String or outputFile (if provided) |
| String | String or outputFile (if provided) |
| CharSequence | String or outputFile (if provided) |
| Anything else | InvalidParameterException |

#### Generate key with SecureRandom (pseudo-random)
```kotlin
Expand Down
Loading

0 comments on commit 019d757

Please sign in to comment.