AES Crypto testing - DO NOT MERGE

This commit is contained in:
Przemek Grondek 2014-07-28 15:43:34 +02:00
parent 478e85ce9c
commit e2421ae720
2 changed files with 89 additions and 0 deletions
app/src/main/java/info/nerull7/mysqlbrowser

View file

@ -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;
}
}

View file

@ -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);