|
| 1 | +# Scytale |
| 2 | + |
| 3 | +[](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 | + |
| 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 | +``` |
0 commit comments