import java.io.ByteArrayOutputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* AES-128
*/
public class EncryptionUtils {
/**
* initialisation vector
*/
private final static byte[] IV = "thai8bai9voo1haV".getBytes();
/**
* algorithm/mode/padding
*/
private final static String TRANSFORMATION = "AES/CBC/PKCS5Padding";
/**
*
* @param text encrypted text
* @param password need have 128-bytes length (16 characters string)
* @return
* @throws Exception
*/
public static String encrypt(String text, String password) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(IV);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedBytes = cipher.doFinal(text.getBytes());
return toHexString(encryptedBytes);
}
/**
*
* @param text hex-encoded text do decrypt
* @param password need have 128-bytes length (16 characters string)
* @return
* @throws Exception
*/
public static String decrypt(String text, String password) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(IV);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] decryptedBytes = cipher.doFinal(toBytes(text));
return new String(decryptedBytes);
}
private static byte[] toBytes(String hex) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(hex.length() / 2);
for (int i = 0; i < hex.length(); i += 2) {
String output = hex.substring(i, i + 2);
int decimal = Integer.parseInt(output, 16);
baos.write(decimal);
}
return baos.toByteArray();
}
private static String toHexString(byte[] encrypted) {
StringBuilder hex = new StringBuilder();
for (byte b : encrypted) {
hex.append(String.format("%02x", b));
}
return hex.toString();
}
} |
import java.io.ByteArrayOutputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* AES-128
*/
public class EncryptionUtils {
/**
* initialisation vector
*/
private final static byte[] IV = "thai8bai9voo1haV".getBytes();
/**
* algorithm/mode/padding
*/
private final static String TRANSFORMATION = "AES/CBC/PKCS5Padding";
/**
*
* @param text encrypted text
* @param password need have 128-bytes length (16 characters string)
* @return
* @throws Exception
*/
public static String encrypt(String text, String password) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(IV);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedBytes = cipher.doFinal(text.getBytes());
return toHexString(encryptedBytes);
}
/**
*
* @param text hex-encoded text do decrypt
* @param password need have 128-bytes length (16 characters string)
* @return
* @throws Exception
*/
public static String decrypt(String text, String password) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(IV);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] decryptedBytes = cipher.doFinal(toBytes(text));
return new String(decryptedBytes);
}
private static byte[] toBytes(String hex) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(hex.length() / 2);
for (int i = 0; i < hex.length(); i += 2) {
String output = hex.substring(i, i + 2);
int decimal = Integer.parseInt(output, 16);
baos.write(decimal);
}
return baos.toByteArray();
}
private static String toHexString(byte[] encrypted) {
StringBuilder hex = new StringBuilder();
for (byte b : encrypted) {
hex.append(String.format("%02x", b));
}
return hex.toString();
}
}