Ok, this time I'll share about simple apps for encryption using AES and MD5. I try to combine AES with MD5. The string will be encrypted with AES first, and then the cipher text will be encrypted again with MD5. So, this is the capture of the applications.
The first field is for key encryption, and the second field is for the teks to be encrypted.
To create this app, I use 3 classes. The first class is an activity called AesMd5.java, GenerateAes.java, Md5Baru.java.
For the source code to create this simple apps I'll show below.
The first is
AesMd5.java
package org.aesmd5;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.*;
import android.widget.Button;
import android.widget.EditText;
public class AesMd5 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aes_md5);
final Button enkripButton = (Button)findViewById(R.id.enkrip_button);
final EditText inputKunci = (EditText)findViewById(R.id.input_kunci);
final EditText inputKata = (EditText)findViewById(R.id.input_kata);
final EditText teksEnkrip = (EditText)findViewById(R.id.teks_enkrip);
// handling for Encryption
enkripButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ambilKunci = inputKunci.getText().toString();
String ambilKata = inputKata.getText().toString();
String enKata = "";
try {
enKata = GenerateAES.encrypt(ambilKunci, ambilKata);
} catch (Exception e) {
e.printStackTrace();
}
//teksEnkrip.setText(enKata);
teksEnkrip.setText(Md5Baru.MD5(enKata));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_aes_md5, menu);
return true;
}
}
GenerateAes.java
package org.aesmd5;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.SecureRandom;
public class GenerateAES {
//private Cipher cipher;
//private SecretKeySpec sKey;
public GenerateAES() {
}
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}
Md5Baru.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.aesmd5;
import java.security.*;
/**
*
* @author nang
*/
public class Md5Baru {
public static String MD5(String text) {
String md5 = "";
byte test[] = text.getBytes();
int panjang = 16;
byte[] mesDig = new byte[panjang];
try {
MessageDigest mD = MessageDigest.getInstance("MD5");
mD.reset();
mD.update(text.getBytes(), 0, text.length());
mD.digest(mesDig, 0, panjang);
StringBuffer sB = new StringBuffer();
for(int i=0; i<mesDig.length;i++) {
//mesDig = mD.digest(test, 0, test.length);
String hex = Integer.toHexString(0xFF & mesDig[i]);
if(hex.length() == 1) {
sB.append('0');
}
sB.append(hex);
}
md5 = sB.toString();
} catch (Exception e){
}
return md5;
}
}
And the last is activity_aes_md5.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/teks_kunci"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Masukkan kunci"
/>
<EditText
android:id="@+id/input_kunci"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/teks_kata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Masukkan kata"
/>
<EditText
android:id="@+id/input_kata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/enkrip_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enkrip"
/>
<EditText
android:id="@+id/teks_enkrip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Ok, that's all for today, happy coding, and go Open Source.
Tidak ada komentar:
Posting Komentar