Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Java Encryption Using Key
- //Kalo mau paspornya lebih panjang
- /*
- SecretKey = getSecretKey();
- byte[] md5Key = null;
- try {
- MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(SecretKey.getBytes("UTF-8"),0,SecretKey.length());
- md5Key = md.digest();
- // Log.d("md5Key",md.toString());
- } catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- */
- import java.io.UnsupportedEncodingException;
- import java.security.NoSuchAlgorithmException;
- import javax.crypto.Cipher;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- public class TestActivity extends Activity {
- private String iv = "fedcba9876543210";// Dummy iv (CHANGE IT!)
- private IvParameterSpec ivspec;
- private SecretKeySpec keyspec;
- private Cipher cipher;
- // private String SecretKey = "0123456789abcdef";// Dummy secretKey (CHANGE
- // // IT!)
- private String SecretKey = "0123456789abcdef";// Dummy secretKey (CHANGE
- // IT!)
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ivspec = new IvParameterSpec(iv.getBytes());
- keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
- try {
- cipher = Cipher.getInstance("AES/CBC/NoPadding");
- } catch (NoSuchAlgorithmException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchPaddingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Log.d("cipher.toString()", cipher.toString());
- Log.d("keyspec.toString()", keyspec.toString());
- Log.d("ivspec.toString()", ivspec.toString());
- Log.d("SecretKey", SecretKey);
- Log.d("iv", iv);
- String eiv = "";
- try {
- eiv = new String(encrypt(iv), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Log.d("eiv", eiv);
- try {
- eiv = new String(decrypt(eiv), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Log.d("eiv", eiv);
- /* Encrypt */
- String encrypted = "";
- try {
- encrypted = bytesToHex( encrypt("Mine Fujiko") );
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Log.d("encrypted", encrypted);
- /* Decrypt */
- String decrypted = "";
- try {
- decrypted = new String( decrypt( encrypted ) );
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Log.d("decrypted", decrypted);
- }
- public byte[] encrypt(String text) throws Exception {
- if (text == null || text.length() == 0)
- throw new Exception("Empty string");
- byte[] encrypted = null;
- try {
- cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
- encrypted = cipher.doFinal(padString(text).getBytes());
- } catch (Exception e) {
- throw new Exception("[encrypt] " + e.getMessage());
- }
- return encrypted;
- }
- public byte[] decrypt(String code) throws Exception {
- if (code == null || code.length() == 0)
- throw new Exception("Empty string");
- byte[] decrypted = null;
- try {
- cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
- decrypted = cipher.doFinal(hexToBytes(code));
- } catch (Exception e) {
- throw new Exception("[decrypt] " + e.getMessage());
- }
- return decrypted;
- }
- public static String bytesToHex(byte[] data) {
- if (data == null) {
- return null;
- }
- int len = data.length;
- String str = "";
- for (int i = 0; i < len; i++) {
- if ((data[i] & 0xFF) < 16)
- str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
- else
- str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
- }
- return str;
- }
- public static byte[] hexToBytes(String str) {
- if (str == null) {
- return null;
- } else if (str.length() < 2) {
- return null;
- } else {
- int len = str.length() / 2;
- byte[] buffer = new byte[len];
- for (int i = 0; i < len; i++) {
- buffer[i] = (byte) Integer.parseInt(
- str.substring(i * 2, i * 2 + 2), 16);
- }
- return buffer;
- }
- }
- private static String padString(String source) {
- char paddingChar = ' ';
- int size = 16;
- int x = source.length() % size;
- int padLength = size - x;
- for (int i = 0; i < padLength; i++) {
- source += paddingChar;
- }
- return source;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement