Skip to content

Commit 4f20958

Browse files
committed
Scytale initial commit
0 parents  commit 4f20958

26 files changed

+2445
-0
lines changed

.gitignore

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
### Android ###
2+
# Built application files
3+
*.apk
4+
*.ap_
5+
6+
# Files for the Dalvik VM
7+
*.dex
8+
9+
# Java class files
10+
*.class
11+
12+
# Generated files
13+
bin/
14+
gen/
15+
16+
# Gradle files
17+
.gradle/
18+
build/
19+
20+
# Local configuration file (sdk path, etc)
21+
local.properties
22+
23+
# Proguard folder generated by Eclipse
24+
proguard/
25+
26+
# Log Files
27+
*.log
28+
29+
# Android Studio Navigation editor temp files
30+
.navigation/
31+
32+
### Android Patch ###
33+
gen-external-apklibs
34+
35+
36+
### Intellij ###
37+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
38+
39+
*.iml
40+
41+
## Directory-based project format:
42+
.idea/
43+
# if you remove the above rule, at least ignore the following:
44+
45+
# User-specific stuff:
46+
# .idea/workspace.xml
47+
# .idea/tasks.xml
48+
# .idea/dictionaries
49+
50+
# Sensitive or high-churn files:
51+
# .idea/dataSources.ids
52+
# .idea/dataSources.xml
53+
# .idea/sqlDataSources.xml
54+
# .idea/dynamic.xml
55+
# .idea/uiDesigner.xml
56+
57+
# Gradle:
58+
# .idea/gradle.xml
59+
# .idea/libraries
60+
61+
# Mongo Explorer plugin:
62+
# .idea/mongoSettings.xml
63+
64+
## File-based project format:
65+
*.ipr
66+
*.iws
67+
68+
## Plugin-specific files:
69+
70+
# IntelliJ
71+
/out/
72+
73+
# mpeltonen/sbt-idea plugin
74+
.idea_modules/
75+
76+
# JIRA plugin
77+
atlassian-ide-plugin.xml
78+
79+
# Crashlytics plugin (for Android Studio and IntelliJ)
80+
com_crashlytics_export_strings.xml
81+
crashlytics.properties
82+
crashlytics-build.properties
83+
*.asc

README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Scytale
2+
3+
[![Download](https://api.bintray.com/packages/yakivmospan/maven/scytale/images/download.svg)](https://bintray.com/yakivmospan/maven/scytale/_latestVersion)
4+
5+
One tool to manage key generation, key storing and encryption on different APIs of Android.
6+
7+
![](assets/logo.png)
8+
9+
As you may know android provided API to use `keystore` that is stored in system only from API 18. They introduced [AndroidKeyStore](http://developer.android.com/training/articles/keystore.html) provider that is responsible to manage this.
10+
11+
But as always there are underwater stones. Up to API 23 you are only able to create asymmetric keys using `AndroidKeyStore` provider. Also [algorithms](http://developer.android.com/training/articles/keystore.html#SupportedAlgorithms) that you can use are limited. And what about devices below API 18 ?
12+
13+
I've create API that wraps default [JCA](http://docs.oracle.com/javase/7/docs/technotes/guides/security/crypto/CryptoSpec.html) api and `AndroidKeyStore` API and makes it easy to create, manage and use your keys on any andorid API.
14+
15+
## Sample
16+
17+
```java
18+
// Create and save key
19+
Store store = new Store(getApplicationContext());
20+
if (!store.hasKey("test")) {
21+
SecretKey key = store.generateSymmetricKey("test", null);
22+
}
23+
...
24+
25+
// Get key
26+
SecretKey key = store.getSymmetricKey("test", null);
27+
28+
// Encrypt/Dencrypt data
29+
Crypto crypto = new Crypto(Options.TRANSFORMATION_SYMMETRIC);
30+
String text = "Sample text";
31+
32+
String encryptedData = crypto.encrypt(text, key);
33+
Log.i("Scytale", "Encrypted data: " + encryptedData);
34+
35+
String decryptedData = crypto.decrypt(encryptedData, key);
36+
Log.i("Scytale", "Decrypted data: " + decryptedData);
37+
```
38+
39+
## How it works?
40+
41+
Depending on what key you need and what Android you are using, API will create `keystore` file in application inner cache or will use `AndroidKeyStore` to hold keys. Key generation will be also made with different API. The tables below shows what will be used in different cases.
42+
43+
In case you want to generate and save `Asymmetric` key
44+
45+
| API | Application Keystore | AndroidKeyStore |
46+
|:-----:|:--------------------:|:---------------:|
47+
|`< 18` | `+` | |
48+
|`>= 18`| | `+` |
49+
50+
51+
In case you want to generate and save `Symmetric` key
52+
53+
| API | Application Keystore | AndroidKeyStore |
54+
|:-----:|:--------------------:|:---------------:|
55+
|`< 18` | `+` | |
56+
|`>= 23`| | `+` |
57+
58+
After calling one of `generateKey` methods, key will be automatically stored in `keystore`.
59+
60+
To store asymmetric `PrivateKey` we need to provide `X509Certificate`. And of course there is no default API to do that.
61+
62+
On `18+` devices its pretty easy, google did it for us.
63+
64+
For `pre 18` there is one 3d party library that can create self signed `X509Certificate`. It is called [Bouncy Castle](http://www.bouncycastle.org/) and is available on maven as well. But after some research I found that [Google did copied this library](https://goo.gl/Zcaqpj) to their API but made it private. Why ? Don't ask me..
65+
66+
So I decided to make it like this :
67+
68+
- API will try to get Google Bouncy Castle using reflection (I've checked it on few APIs and it seems to work well)
69+
- If Google version is missing, API will try to get 3d party Bouncy Castle library. It will use reflection as well. This gives two advantages:
70+
- You can add this API for 18+ devices with out any additional libraries
71+
- You can run this API on pre 18 devices with out any additional libraries as well. And in case if some device will miss google hidden API you will receive an error and then include Bouncy Castle to project. This is pretty cool if you are getting error on 15 API but your min project API is 16, and there is no errors on it.
72+
73+
In general it creates simple interface to work with `Keystore` using API provided by Java and different versions of Android.
74+
75+
## Extended Usage
76+
77+
Instead of using `generateAsymmetricKey(@NonNull String alias, char[] password)` method you can use ` generateAsymmetricKey(@NonNull KeyProps keyProps)` one, and define key with specific options.
78+
79+
```java
80+
// Create store with specific name and password
81+
Store store = new Store(context, STORE_NAME, STORE_PASSWORD);
82+
83+
final int alias = "alias";
84+
final int password = "password".toCharArray();
85+
final int keysize = 512;
86+
87+
final Calendar start = Calendar.getInstance();
88+
final Calendar end = Calendar.getInstance();
89+
end.add(Calendar.YEAR, 1);
90+
91+
// Create a key store params, some of them are specific per platform
92+
// Check KeyProps doc for more info
93+
KeyProps keyProps = new KeyProps.Builder()
94+
.setAlias(alias)
95+
.setPassword(password)
96+
.setKeySize(keysize)
97+
.setKeyType("RSA")
98+
.setSerialNumber(BigInteger.ONE)
99+
.setSubject(new X500Principal("CN=" + alias + " CA Certificate"))
100+
.setStartDate(start.getTime())
101+
.setEndDate(end.getTime())
102+
.setBlockModes("ECB")
103+
.setEncryptionPaddings("PKCS1Padding")
104+
.setSignatureAlgorithm("SHA256WithRSAEncryption")
105+
.build();
106+
107+
// Generate KeyPair depending on KeyProps
108+
KeyPair keyPair = store.generateAsymmetricKey(keyProps);
109+
110+
// Encrypt/Dencrypt data using buffer with or with out Initialisation Vectors
111+
// This additional level of safety is required on 23 API level for
112+
// some algorithms. Specify encryption/decryption block size to use buffer for
113+
// large data when using block based algorithms(such as RSA)
114+
115+
final int encryptionBlockSize = keysize / 8 - 11; // as specified for RSA/ECB/PKCS1Padding keys
116+
final int decryptionBlockSize = keysize / 8; // as specified for RSA/ECB/PKCS1Padding keys
117+
118+
Crypto crypto = new Crypto("RSA/ECB/PKCS1Padding", encryptionBlockSize, decryptionBlockSize);
119+
120+
String text = "Sample text";
121+
String encryptedData = crypto.encrypt(text, key, false);
122+
String decryptedData = crypto.decrypt(encryptedData, key, false);
123+
```
124+
125+
### Download
126+
127+
Add dependency to your app `gradle.build` file:
128+
129+
```java
130+
compile 'com.yakivmospan:scytale:1.0.0'
131+
```
132+
133+
Minimum supported API version is 8.
134+
135+
### License
136+
137+
```
138+
Copyright 2016 Yakiv Mospan
139+
140+
Licensed under the Apache License, Version 2.0 (the "License");
141+
you may not use this file except in compliance with the License.
142+
You may obtain a copy of the License at
143+
144+
http://www.apache.org/licenses/LICENSE-2.0
145+
146+
Unless required by applicable law or agreed to in writing, software
147+
distributed under the License is distributed on an "AS IS" BASIS,
148+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149+
See the License for the specific language governing permissions and
150+
limitations under the License.
151+
```

assets/logo.png

108 KB
Loading

build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
}
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:2.2.3'
9+
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
10+
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
11+
12+
// NOTE: Do not place your application dependencies here; they belong
13+
// in the individual module build.gradle files
14+
}
15+
}
16+
17+
allprojects {
18+
repositories {
19+
jcenter()
20+
}
21+
}
22+
23+
task clean(type: Delete) {
24+
delete rootProject.buildDir
25+
}

gradle.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# Specifies the JVM arguments used for the daemon process.
11+
# The setting is particularly useful for tweaking memory settings.
12+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14+
15+
# When configured, Gradle will run in incubating parallel mode.
16+
# This option should only be used with decoupled projects. More details, visit
17+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18+
# org.gradle.parallel=true

0 commit comments

Comments
 (0)