Skip to content

Easy-to-use text encryption support library with "Android KeyStore System".

License

Notifications You must be signed in to change notification settings

KazaKago/Cryptore

Repository files navigation

./artwork/logo.png

Cryptore

Maven Central javadoc Test license

This library performs encryption and decryption byte array using Android KeyStore System.

Requirement

  • RSA encryption
    • Android 4.3 (API 18) or later
  • AES encryption
    • Android 6.0 (API 23) or later

This is due to Android OS hardware restrictions. More details.

Install

Add the following gradle dependency exchanging x.x.x for the latest release.

implementation 'com.kazakago.cryptore:cryptore:x.x.x'

Usage

The following is a sample to encrypt and decrypt text using RSA encryption.

Initialize

Cryptore getCryptore(Context context, String alias) throws Exception {
    Cryptore.Builder builder = new Cryptore.Builder(alias, CipherAlgorithm.RSA);
    builder.setContext(context); //Need Only RSA on below API Lv22.
//    builder.setBlockMode(BlockMode.ECB); //If Needed.
//    builder.setEncryptionPadding(EncryptionPadding.RSA_PKCS1); //If Needed.
    return builder.build();
}

Encrypt

String encrypt(String plainStr) throws Exception {         
    byte[] plainByte = plainStr.getBytes();         
    EncryptResult result = getCryptore().encrypt(plainByte);
    return Base64.encodeToString(result.getBytes(), Base64.DEFAULT);
}

Decrypt

String decrypt(String encryptedStr) throws Exception {
    byte[] encryptedByte = Base64.decode(encryptedStr, Base64.DEFAULT);
    DecryptResult result = getCryptore().decrypt(encryptedByte, null);
    return new String(result.getBytes());
}

Refer to the sample module (Java & Kotlin) for details.

For other encryption options supported by Android, please see here.

Default encryption mode

  • RSA encryption
    • BlockMode : ECB
    • Padding : PKCS1Padding
  • AES encryption
    • BlockMode : CBC
    • Padding : PKCS7Padding

License

MIT License

Copyright (c) 2017 KazaKago

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.