-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from m4dc4p/jgbailey/securerandom-update
Use best-practice SecureRandom, rather than specific algorithm.
- Loading branch information
Showing
32 changed files
with
495 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
.DS_Store | ||
/build | ||
.externalNativeBuild | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/build | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
34 changes: 17 additions & 17 deletions
34
sample/proguard-rules.pro → aes-crypto-sample-app/app/proguard-rules.pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *; | ||
#} |
40 changes: 19 additions & 21 deletions
40
sample/src/main/AndroidManifest.xml → ...mple-app/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
163 changes: 68 additions & 95 deletions
163
...y/crypto/android/sample/MainActivity.java → ...tozny/aes_crypto_sample/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
aes-crypto-sample-app/app/src/main/res/layout/activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added
BIN
+10.2 KB
aes-crypto-sample-app/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions
12
sample/src/main/res/values-w820dp/dimens.xml → ...app/src/main/res/values-w820dp/dimens.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
10 changes: 5 additions & 5 deletions
10
sample/src/main/res/values/dimens.xml → ...le-app/app/src/main/res/values/dimens.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<resources> | ||
<string name="app_name">Tozny Crypto</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.