• Stars
    star
    738
  • Rank 60,989 (Top 2 %)
  • Language
    Java
  • License
    MIT License
  • Created almost 10 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple Android class for encrypting & decrypting strings, aiming to avoid the classic mistakes that most such classes suffer from.

java-aes-crypto

This AES library is very simple and works only on Android. For a cross-platform encryption system, please use TozStore. It's available for Android, iOS, and JavaScript for the browser, as well as back-end systems like Node, Ruby, Python, Java, and Go.

Java-AES-Crypto is Tozny's simple Android class for encrypting & decrypting strings, aiming to avoid serious cryptographic errors that most such classes suffer from. Show me the code

Features

Here are the features of this class. We believe that these properties are consistent with what a lot of people are looking for when encrypting Strings in Android.

  • Works for strings: It should encrypt arbitrary strings or byte arrays. This means it needs to effectively handle multiple blocks (CBC) and partial blocks (padding). It consistently serializes and deserializes ciphertext, IVs, and key material using base64 to make it easy to store.
  • Algorithm & Mode: We chose: AES 128, CBC, and PKCS5 padding. We would have picked GCM for its built-in integrity checking, but that's only available since Android Jelly Bean.
  • IV Handling: We securely generate a random IV before each encryption and provide a simple class to keep the IV and ciphertext together so they're easy to keep track of and store. We set the IV and then request it back from the Cipher class for compatibility across various Android versions.
  • Key generation: Random key generation with the updated generation code recommended for Android. If you want password-based keys, we provide functions to salt and generate them.
  • Integrity: Lots of people think AES has integrity checking built in. The thinking goes, "if it decrypts correctly, it was generated by the person with the private key". Actually, AES CBC allows an attacker to modify the messages. Therefore, we've also added integrity checking in the form of a SHA 256 hash.
  • Older Phones: It's designed for backward compatibility with older phones, including ciphers that are available for most versions of Android as well as entropy fixes for old Android bugs.

How to include in project?

Copy and paste

It's a single very simple java class, AesCbcWithIntegrity.java that works across most or all versions of Android. The class should be easy to paste into an existing codebase.

Android Library project

The library is in Android library project format so you can clone this project and add as a library module/project.

Maven Dependency

We've also published the library AAR file via Jitpack for simple gradle dependency management:

Add the Jitpack repository to your root build.gradle:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Add the dependency to your project's build.gradle:

dependencies {
  compile 'com.github.tozny:java-aes-crypto:1.1.0'
}

Examples

Generate new key

  AesCbcWithIntegrity.SecretKeys keys = AesCbcWithIntegrity.generateKey();

Generate a key from a password or passphrase

  EXAMPLE_PASSWORD = // Get password from user input
  String salt = saltString(generateSalt());
  // You can store the salt, it's not secret. Don't store the key. Derive from password every time
  Log.i(TAG, "Salt: " + salt);
  key = generateKeyFromPassword(EXAMPLE_PASSWORD, salt);

Encrypt

   AesCbcWithIntegrity.CipherTextIvMac cipherTextIvMac = AesCbcWithIntegrity.encrypt("some test", keys);
   //store or send to server
   String ciphertextString = cipherTextIvMac.toString();

Decrypt

  //Use the constructor to re-create the CipherTextIvMac class from the string:
  CipherTextIvMac cipherTextIvMac = new CipherTextIvMac (cipherTextString);
  String plainText = AesCbcWithIntegrity.decryptString(cipherTextIvMac, keys);

Storing Keys

Once you've generated a random key, you naturally might want to store it. This may work for some use cases, but please be aware that if you store the key in the same place that you store the encrypted data, your solution is not cryptographically sound since the attacker can just get both the key and the encrypted text. Instead, you should use either the Keystore infrastructure or consider generating the key from a passphrase and using that to encrypt the user data.

If despite the above you still want to store the key, you can convert the keys to a string using the included functions and store them in preferences or SQLite.

Note that if you hard-code keys or passphrases, or generate them from a static value, you will likely get an error message from the Android security scanner.

License

The included MIT license is compatible with open source or commercial products. Tozny also offers custom support and licensing terms if your organization has different needs. Contact us at [email protected] for more details.

More Repositories

1

rancher-lets-encrypt

Automatically create and manage certificates in Rancher using Let's Encrypt webroot verification via a minimal service
Python
91
star
2

rust-pam

A PAM binding in the Rust programming language, implemented by Tozny.
Rust
9
star
3

e3db-java

E3DB SDK for Android and Java
Java
6
star
4

js-sdk

Tozny Platform JavaScript Software Development Kit
JavaScript
6
star
5

e3db

Command Line Clients and SDKs for E3DB
5
star
6

e3db-php

PHP client library for TozStore
PHP
5
star
7

toznyauth-pam

Authenticate to Linux-based systems using Tozny. Based on the PAM framework. Tozny lets your users log in with their phone. Its one or two-factor authentication system is easy to use, easy to integrate, and cryptographically secure.
Rust
4
star
8

browser-sodium-sdk

JavaScript
3
star
9

sdk-node

Node.js client bindings to the TOZNY API. Tozny lets your users log in with their phone. Its one or two-factor authentication system is easy to use, easy to integrate, and cryptographically secure.
JavaScript
3
star
10

dockerfiles

Tozny Custom Public Docker Images for automated Docker Hub builds
Shell
3
star
11

utils-go

Central repository of Tozny go code that has utility across multiple projects.
Go
2
star
12

e3db-ruby

Ruby client library for E3DB
Ruby
2
star
13

e3db-swift

Client SDK in Swift for Tozny's E3DB.
Swift
2
star
14

terraform-provider-tozny

Tozny Terraform provider for Infrastructure As Code (IAC) automation of Tozny products.
Go
2
star
15

sdk-rust

A Tozny binding in the Rust programming language. Tozny lets your users log in with their phone. Its one or two-factor authentication system is easy to use, easy to integrate, and cryptographically secure.
Rust
2
star
16

e3db-go

Go language client library for e3db
Go
2
star
17

SimpleSAMLphp

Docker container configuration for SimpleSAMLphp built on top of Alpine Linux
Smarty
2
star
18

e3db-js

JavaScript client library for E3DB
JavaScript
2
star
19

e3db-clients-go

Http clients for interacting with e3db services, in go.
Go
2
star
20

docker-swagger

2
star
21

docker-sbt-extras

Build a Docker container to run PaulP's sbt script
1
star
22

js-id-tools

A Tozny SDK extension for server-side identity applications
JavaScript
1
star
23

docker-npm-publish

publish a package to npmjs.org
Shell
1
star
24

sdk-java

A Java library for interacting with the Tozny API. Tozny lets your users log in with their phone. Its one or two-factor authentication system is easy to use, easy to integrate, and cryptographically secure.
Java
1
star
25

sdk-php

PHP client bindings to the TOZNY API. Tozny lets your users log in with their phone. Its one or two-factor authentication system is easy to use, easy to integrate, and cryptographically secure.
PHP
1
star
26

simplesamlphp-toznyauth-external

Use Tozny's Authenticator service as an auth source in SimpleSAMLphp
PHP
1
star
27

toznyauth-wordpress

Quickly add TOZNY authentication to your WordPress! Tozny lets your users log in with their phone. Its one or two-factor authentication system is easy to use, easy to integrate, and cryptographically secure.
PHP
1
star
28

simplesamlphp-ldap-tozny

A SimpleSAMLphp fork of the LDAP plugin to include Tozny authentication of LDAP users. Tozny lets your users log in with their phone. Its one or two-factor authentication system is easy to use, easy to integrate, and cryptographically secure.
PHP
1
star
29

js-account-sdk

Javascript SDK for performing Tozny platform account level operations
TypeScript
1
star