/**
Aes encryption
*/
public class Test
{
private static SecretKeySpec secretKey ; private static byte[] key ; private static String decryptedString; private static String encryptedString; public static void setKey(String myKey){
MessageDigest sha = null; try { key = myKey.getBytes("UTF-8"); System.out.println(key.length); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); // use only first 128 bit System.out.println(key.length); System.out.println(new String(key,"UTF-8")); secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); }
} public static String getDecryptedString() { return decryptedString; } public static void setDecryptedString(String decryptedString) { Test.decryptedString = decryptedString; } public static String getEncryptedString() { return encryptedString; } public static void setEncryptedString(String encryptedString) { Test.encryptedString = encryptedString; } public static String encrypt(String strToEncrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey);