Skip to content

Commit

Permalink
Merge pull request #32 from m4dc4p/jgbailey/securerandom-update
Browse files Browse the repository at this point in the history
Use best-practice SecureRandom, rather than specific algorithm.
  • Loading branch information
m4dc4p authored Dec 7, 2016
2 parents a228758 + e451be2 commit aed40cf
Show file tree
Hide file tree
Showing 32 changed files with 495 additions and 327 deletions.
7 changes: 7 additions & 0 deletions aes-crypto-sample-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.iml
.gradle
/local.properties
.DS_Store
/build
.externalNativeBuild
.idea
2 changes: 1 addition & 1 deletion sample/.gitignore → aes-crypto-sample-app/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/build
/build
33 changes: 33 additions & 0 deletions aes-crypto-sample-app/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.tozny.aeswithintegritysample"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
main.java.srcDirs += '../../aes-crypto/src/main/java'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/scottab/dev/adt-bundle-mac-x86_64/sdk-macosx-v2/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\Justin\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tozny.crypto.android.sample" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tozny.aeswithintegritysample">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.tozny.aes_crypto_sample.MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,95 +1,68 @@
package com.tozny.crypto.android.sample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

import com.tozny.crypto.android.AesCbcWithIntegrity;

import static com.tozny.crypto.android.AesCbcWithIntegrity.decryptString;
import static com.tozny.crypto.android.AesCbcWithIntegrity.encrypt;
import static com.tozny.crypto.android.AesCbcWithIntegrity.generateKey;
import static com.tozny.crypto.android.AesCbcWithIntegrity.generateKeyFromPassword;
import static com.tozny.crypto.android.AesCbcWithIntegrity.generateSalt;
import static com.tozny.crypto.android.AesCbcWithIntegrity.keyString;
import static com.tozny.crypto.android.AesCbcWithIntegrity.keys;
import static com.tozny.crypto.android.AesCbcWithIntegrity.saltString;

/**
* Sample shows password based key gen
*/
public class MainActivity extends Activity {
public static final String TAG = "Tozny";

private static boolean PASSWORD_BASED_KEY = true;
private static String EXAMPLE_PASSWORD = "LeighHunt";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {
AesCbcWithIntegrity.SecretKeys key;
if (PASSWORD_BASED_KEY) {//example for password based keys
String salt = saltString(generateSalt());
//If you generated the key from a password, you can store the salt and not the key.
Log.i(TAG, "Salt: " + salt);
key = generateKeyFromPassword(EXAMPLE_PASSWORD, salt);
} else {
key = generateKey();
//Note: If you are generating a random key, you'll probably be storing it somewhere
}

// The encryption / storage & display:

String keyStr = keyString(key);
key = null; //Pretend to throw that away so we can demonstrate converting it from str

String textToEncrypt = "We, the Fairies, blithe and antic,\n" +
"Of dimensions not gigantic,\n" +
"Though the moonshine mostly keep us,\n" +
"Oft in orchards frisk and peep us. ";
Log.i(TAG, "Before encryption: " + textToEncrypt);

// Read from storage & decrypt
key = keys(keyStr); // alternately, regenerate the key from password/salt.
AesCbcWithIntegrity.CipherTextIvMac civ = encrypt(textToEncrypt, key);
Log.i(TAG, "Encrypted: " + civ.toString());

String decryptedText = decryptString(civ, key);
Log.i(TAG, "Decrypted: " + decryptedText);
//Note: "String.equals" is not a constant-time check, which can sometimes be problematic.
Log.i(TAG, "Do they equal: " + textToEncrypt.equals(decryptedText));
} catch (GeneralSecurityException e) {
Log.e(TAG, "GeneralSecurityException", e);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "UnsupportedEncodingException", e);
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.tozny.aes_crypto_sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

import com.tozny.aeswithintegritysample.R;
import com.tozny.crypto.android.AesCbcWithIntegrity;
import static com.tozny.crypto.android.AesCbcWithIntegrity.*;

public class MainActivity extends AppCompatActivity {

public static final String TAG = "Tozny";

private static boolean PASSWORD_BASED_KEY = true;
private static String EXAMPLE_PASSWORD = "always use passphrases for passwords wherever possible!";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {
AesCbcWithIntegrity.SecretKeys key;
if (PASSWORD_BASED_KEY) {//example for password based keys
String salt = saltString(generateSalt());
//If you generated the key from a password, you can store the salt and not the key.
Log.i(TAG, "Salt: " + salt);
key = generateKeyFromPassword(EXAMPLE_PASSWORD, salt);
} else {
key = generateKey();
//Note: If you are generating a random key, you'll probably be storing it somewhere
}

// The encryption / storage & display:

String keyStr = keyString(key);
key = null; //Pretend to throw that away so we can demonstrate converting it from str

String textToEncrypt = "Testing shows the presence, not the absence of bugs.\n\n Edsger W. Dijkstra";
Log.i(TAG, "Before encryption: " + textToEncrypt);

// Read from storage & decrypt
key = keys(keyStr); // alternately, regenerate the key from password/salt.
AesCbcWithIntegrity.CipherTextIvMac civ = encrypt(textToEncrypt, key);
Log.i(TAG, "Encrypted: " + civ.toString());

String decryptedText = decryptString(civ, key);
Log.i(TAG, "Decrypted: " + decryptedText);
//Note: "String.equals" is not a constant-time check, which can sometimes be problematic.
Log.i(TAG, "Do they equal: " + textToEncrypt.equals(decryptedText));

TextView t = (TextView) findViewById(R.id.textView);
t.setText(decryptedText);
} catch (GeneralSecurityException e) {
Log.e(TAG, "GeneralSecurityException", e);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "UnsupportedEncodingException", e);
}

}
}
21 changes: 21 additions & 0 deletions aes-crypto-sample-app/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tozny.aeswithintegritysample.com.tozny.aes_crypto_sample.MainActivity">

<TextView
android:text="TextView"
android:textIsSelectable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:fontFamily="sans-serif" />
</RelativeLayout>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
6 changes: 6 additions & 0 deletions aes-crypto-sample-app/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
3 changes: 3 additions & 0 deletions aes-crypto-sample-app/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Tozny Crypto</string>
</resources>
11 changes: 11 additions & 0 deletions aes-crypto-sample-app/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>
23 changes: 23 additions & 0 deletions aes-crypto-sample-app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
17 changes: 17 additions & 0 deletions aes-crypto-sample-app/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
Binary file not shown.
Loading

0 comments on commit aed40cf

Please sign in to comment.