From e2421ae72072d3332c1c682ec82598394a11d304 Mon Sep 17 00:00:00 2001
From: Przemek Grondek <github@nerull7.info>
Date: Mon, 28 Jul 2014 15:43:34 +0200
Subject: [PATCH] AES Crypto testing - DO NOT MERGE

---
 .../info/nerull7/mysqlbrowser/Crypto.java     | 69 +++++++++++++++++++
 .../nerull7/mysqlbrowser/LoginFragment.java   | 20 ++++++
 2 files changed, 89 insertions(+)
 create mode 100644 app/src/main/java/info/nerull7/mysqlbrowser/Crypto.java

diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/Crypto.java b/app/src/main/java/info/nerull7/mysqlbrowser/Crypto.java
new file mode 100644
index 0000000..3e89e8f
--- /dev/null
+++ b/app/src/main/java/info/nerull7/mysqlbrowser/Crypto.java
@@ -0,0 +1,69 @@
+package info.nerull7.mysqlbrowser;
+
+import android.util.Log;
+
+import java.nio.charset.Charset;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.KeyGenerator;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKey;
+
+/**
+ * Created by nerull7 on 28.07.14.
+ */
+public class Crypto {
+    private static final String KEY_FILE = "null_file"; // to trick h4x0r5
+    private static final String ENCRYPTION_ALGORITHM = "AES";
+    private static final int OUTPUT_KEY_LENGTH = 256;
+
+    private static SecretKey secretKey;
+
+    private static SecretKey generateKey() throws NoSuchAlgorithmException {
+        SecureRandom secureRandom = new SecureRandom();
+
+        KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM);
+        keyGenerator.init(OUTPUT_KEY_LENGTH, secureRandom);
+        SecretKey secretKey = keyGenerator.generateKey();
+
+        return secretKey;
+    }
+
+    private static SecretKey getSecretKey() throws NoSuchAlgorithmException {
+        if(secretKey==null)
+            secretKey = generateKey();
+
+        Log.d("SecureKEY", "Hash:" + secretKey.hashCode());
+        return secretKey;
+    }
+
+    public static byte[] encrypt(String input) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
+        byte[] output;
+        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
+        cipher.init(Cipher.ENCRYPT_MODE, getSecretKey());
+        output = cipher.doFinal(input.getBytes(Charset.defaultCharset()));
+
+        return output;
+    }
+
+    public static String decrypt(byte[] input) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
+        String output;
+        byte [] tmp; // TODO: REMOVE
+
+        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
+        cipher.init(Cipher.DECRYPT_MODE, getSecretKey());
+//        output = String.valueOf(cipher.doFinal(input));
+        tmp = cipher.doFinal(input);
+
+//        output = tmp.toString();
+        output = new String(input, Charset.defaultCharset());
+
+        return output;
+    }
+
+}
diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java b/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java
index f4f671e..3ac2322 100644
--- a/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java
+++ b/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java
@@ -9,6 +9,7 @@ import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.PreferenceManager;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -16,6 +17,13 @@ import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ProgressBar;
 
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+
 import info.nerull7.mysqlbrowser.db.AsyncDatabaseConnector;
 
 /**
@@ -56,9 +64,21 @@ public class LoginFragment extends Fragment implements View.OnClickListener, Asy
             urlTextbox.setText(sharedPreferences.getString(SettingsFragment.URL_CREDENTIALS, null));
             loginTextbox.setText(sharedPreferences.getString(SettingsFragment.LOGIN_CREDENTIALS, null));
             passwordTextbox.setText(sharedPreferences.getString(SettingsFragment.PASSWORD_CREDENTIALS, null));
+            test(urlTextbox.getText().toString());
         }
     }
 
+    private void test(String text){ //FIXME Remove Me!!!
+        String tmp;
+        byte [] tmp_byte;
+        try {
+            tmp_byte = Crypto.encrypt(text);
+            Log.d("Crypto", "encrypted: " + tmp_byte);
+            tmp = Crypto.decrypt(tmp_byte);
+            Log.d("Crypto", "decrypted: " + tmp);
+        } catch (Exception e) { e.printStackTrace(); }
+    }
+
     @Override
     public void onClick(View view) {
         progressBar.setVisibility(View.VISIBLE);