Skip to content

Commit

Permalink
Implement asymmetric signing and verification with RSA
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan committed Sep 15, 2017
1 parent 019d757 commit a8d42fc
Show file tree
Hide file tree
Showing 40 changed files with 1,270 additions and 530 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build/
# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
# Proguard folder onGenerated by Eclipse
proguard/

# Log Files
Expand All @@ -36,7 +36,7 @@ captures/
# Keystore files
*.jks

# External native build folder generated in Android Studio 2.2 and later
# External native build folder onGenerated in Android Studio 2.2 and later
.externalNativeBuild

# Google Services (e.g. APIs or Firebase)
Expand All @@ -50,7 +50,7 @@ freeline_project_description.json
# Unused code
*.unused
.idea/vcs.xml
/easycrypt/src/main/java/com/pvryan/easycrypt/ECryptAnalyzePassword.kt
/easycrypt/src/main/java/com/pvryan/easycrypt/ECAnalyzePassword.kt
.idea/markdown-navigator.xml
.idea/markdown-navigator/
secure.properties
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
not limited to compiled object code, onGenerated documentation,
and conversions to other media types.

"Work" shall mean the work of authorship, whether in Source or
Expand Down Expand Up @@ -111,7 +111,7 @@
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
within a display onGenerated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
Expand Down
85 changes: 67 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![GitHub issues](https://img.shields.io/github/issues/ryan652/easycrypt.svg)](https://github.com/ryan652/EasyCrypt/issues)

# EasyCrypt
Easily encrypt, decrypt, or hash data in a very secure way.
Secure and efficient cryptography library for Android. (Auto fix SecureRandom bugs in API 18 and below.)

## Features
* AES-256 encryption algorithm
Expand All @@ -14,8 +14,7 @@ Easily encrypt, decrypt, or hash data in a very secure way.
* Password stretching with PBKDF2
* Random IV generated on each encryption (16 bytes)
* Supports MD5, SHA1, and SHA2 hash functions
* SecureRandom fixes on Android below KitKat
* Generate key manually with SecureRandom or random.org
* Generate secure keys 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
Expand All @@ -40,8 +39,8 @@ apply plugin: 'kotlin-android-extensions'
dependencies {
...
compile "com.pvryan.easycrypt:easycrypt:1.1.0"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2"
compile "com.pvryan.easycrypt:easycrypt:1.2.0"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.4-3"
compile "org.jetbrains.anko:anko-commons:0.10.1"
...
}
Expand All @@ -52,24 +51,24 @@ Add in your app's build.gradle
```gradle
dependencies {
...
compile "com.pvryan.easycrypt:easycrypt:1.1.0"
compile "com.pvryan.easycrypt:easycrypt:1.2.0"
...
}
```

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

### Symmetric key encryption
#### Encrypt data
```kotlin
eCryptSymmetric.encrypt (input, password,
object : ECryptResultListener {
object : ECResultListener {

// Optional
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
Expand All @@ -91,7 +90,7 @@ eCryptSymmetric.encrypt (input, password,
#### Decrypt data
```kotlin
eCryptSymmetric.decrypt(input, password,
object : ECryptResultListener {
object : ECResultListener {

// Optional
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
Expand All @@ -113,12 +112,12 @@ eCryptSymmetric.decrypt(input, password,
### Asymmetric key encryption
#### Encrypt data
```kotlin
eCryptAsymmetric.generateKeyPair(object : ECryptRSAKeyPairListener {
eCryptAsymmetric.generateKeyPair(object : ECRSAKeyPairListener {

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

// Optional
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
Expand Down Expand Up @@ -147,7 +146,7 @@ eCryptAsymmetric.generateKeyPair(object : ECryptRSAKeyPairListener {
#### Decrypt data
```kotlin
eCryptAsymmetric.decrypt(input, privateKey,
object : ECryptResultListener {
object : ECResultListener {

// Optional
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
Expand All @@ -166,10 +165,59 @@ eCryptAsymmetric.decrypt(input, privateKey,
)
```

#### Sign data
```kotlin
eCryptKeys.genRSAKeyPair(object : ECRSAKeyPairListener {

override fun onGenerated(keyPair: KeyPair) {

publicKey = keyPair.public as RSAPublicKey

eCryptAsymmetric.sign(input,
keyPair.private as RSAPrivateKey,
object : ECResultListener {

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

}

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

}

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

}
},
signatureOutputFile)
}

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

}
})
```

#### Verify data
```kotlin
eCryptAsymmetric.verify(input, publicKey, signatureFile,
object : ECVerifiedListener {
override fun onSuccess(verified: Boolean) {

}

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

}
}
)
```

#### Hash data
```kotlin
eCryptHash.calculate(input, hashAlgorithm, // from ECryptHashAlgorithms
object : ECryptResultListener {
eCryptHash.calculate(input, hashAlgorithm, // from ECHashAlgorithms
object : ECResultListener {

// Optional
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
Expand Down Expand Up @@ -205,11 +253,12 @@ val password = eCryptPass.genSecureRandomPassword(length, charArrayOf(/*symbols
```

#### Generate key with Random.org (true random)
For sample to work enter your API key in FragmentPasswords
```kotlin
eCryptPass.genRandomOrgPassword(
length,
"random-org-api-key", //TODO: Replace with your random.org api key
new ECryptPasswordListener() {
new ECPasswordListener() {

@Override
public void onFailure(@NonNull String message, @NonNull Exception e) {
Expand Down
4 changes: 2 additions & 2 deletions appJava/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ android {
dependencies {
implementation "com.android.support:appcompat-v7:$support_version"
implementation "com.android.support:design:$support_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.1.3-2"
implementation "org.jetbrains.anko:anko-commons:0.10.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation project(path: ':easycrypt')
}

Expand Down
Loading

0 comments on commit a8d42fc

Please sign in to comment.