android encryption/decryption with AES Algorithm

March 14, 2017 , 0 Comments

Use this Class For AES ALGORITHAM :
import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
* Created by db on 3/14/2017.
*/

public class AESAlgoritham {
private static final String ALGO = "AES/ECB/PKCS7Padding";
private static final String SECRETKEY = "SIDDHARTH";

// private static final String ALGO = "AES/CBC/PKCS7Padding";

public static String encryptData(String cleartext)
throws Exception {
byte[] result = encrypt(cleartext.getBytes());
return toHex(result);
}

public static String decryptData(String encrypted)
throws Exception {
byte[] enc = toByte(encrypted);
byte[] result = decrypt(enc);
return new String(result);
}

private static byte[] encrypt(byte[] clear) throws Exception {
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.ENCRYPT_MODE, getSecreteKey(SECRETKEY));
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}

private static byte[] decrypt(byte[] encrypted)
throws Exception {
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.DECRYPT_MODE, getSecreteKey(SECRETKEY));
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

public static SecretKey getSecreteKey(String secretKey) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
SecretKey key = new SecretKeySpec(keyBytes, "AES");
return key;
}

public static String toHex(String txt) {
return toHex(txt.getBytes());
}

public static String fromHex(String hex) {
return new String(toByte(hex));
}

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 > 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}

}

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Popular Posts