EncryptionDecryptionNewLogic
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.axiom.om.util.Base64;
private static String key = STATIC_AES_128BIT_KEY;
private static String initvector = STATIC_16_BYTE_INITVECTOR;
/**
* Encrypt the value
*
* @param str the value to encrypt
* @return the encrypted value
*/
public static String encrypt(String str) {
try {
IvParameterSpec iv = new IvParameterSpec(initvector.getBytes("UTF-8"));
Key aesKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, aesKey, iv);
byte[] encrypted = cipher.doFinal(str.getBytes());
return Base64.encode(encrypted);
} catch (java.security.InvalidAlgorithmParameterException | java.io.UnsupportedEncodingException
| javax.crypto.BadPaddingException | javax.crypto.IllegalBlockSizeException | javax.crypto.NoSuchPaddingException
| java.security.InvalidKeyException | java.security.NoSuchAlgorithmException e) {
throw new SecurityException(e);
}
}
/**
* Decrypts the value
*
* @param str the value to decrypt
* @return the decrypted value
*/
public static String decrypt(String str) {
try {
IvParameterSpec iv = new IvParameterSpec(initvector.getBytes("UTF-8"));
Key aesKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, aesKey, iv);
byte[] bytes = Base64.decode(str);
String decrypted = new String(cipher.doFinal(bytes));
return decrypted;
} catch (java.security.InvalidAlgorithmParameterException | java.io.UnsupportedEncodingException
| javax.crypto.BadPaddingException | javax.crypto.IllegalBlockSizeException | javax.crypto.NoSuchPaddingException
| java.security.InvalidKeyException | java.security.NoSuchAlgorithmException e) {
throw new SecurityException(e);
}
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.axiom.om.util.Base64;
private static String key = STATIC_AES_128BIT_KEY;
private static String initvector = STATIC_16_BYTE_INITVECTOR;
/**
* Encrypt the value
*
* @param str the value to encrypt
* @return the encrypted value
*/
public static String encrypt(String str) {
try {
IvParameterSpec iv = new IvParameterSpec(initvector.getBytes("UTF-8"));
Key aesKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, aesKey, iv);
byte[] encrypted = cipher.doFinal(str.getBytes());
return Base64.encode(encrypted);
} catch (java.security.InvalidAlgorithmParameterException | java.io.UnsupportedEncodingException
| javax.crypto.BadPaddingException | javax.crypto.IllegalBlockSizeException | javax.crypto.NoSuchPaddingException
| java.security.InvalidKeyException | java.security.NoSuchAlgorithmException e) {
throw new SecurityException(e);
}
}
/**
* Decrypts the value
*
* @param str the value to decrypt
* @return the decrypted value
*/
public static String decrypt(String str) {
try {
IvParameterSpec iv = new IvParameterSpec(initvector.getBytes("UTF-8"));
Key aesKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, aesKey, iv);
byte[] bytes = Base64.decode(str);
String decrypted = new String(cipher.doFinal(bytes));
return decrypted;
} catch (java.security.InvalidAlgorithmParameterException | java.io.UnsupportedEncodingException
| javax.crypto.BadPaddingException | javax.crypto.IllegalBlockSizeException | javax.crypto.NoSuchPaddingException
| java.security.InvalidKeyException | java.security.NoSuchAlgorithmException e) {
throw new SecurityException(e);
}
}
Comments
Post a Comment