AES encryption algorithm, user-sensitive information encryption, and aes Encryption Algorithm
/**
* AES is a reversible encryption algorithm that encrypts users' sensitive information and performs Base64 encoding and conversion after the original data is encrypted by AES;
*/
Public class AESOperator {
/*
* The Encrypted Key can consist of 26 letters and numbers. Here the AES-128-CBC encryption mode is used, and the key needs to be 16 characters. 0021XdSh0021XdSh
*/
Private String sKey = "10d2Xd4hf0s1XvSw ";
Private String ivParameter = "10d2Xd4hf0s1XvSw ";
Private static AESOperator instance = null;
Private AESOperator (){
}
Public static AESOperator getInstance (){
If (instance = null)
Instance = new AESOperator ();
Return instance;
}
Public static String Encrypt (String encData, String secretKey, String vector) throws Exception {
If (secretKey = null ){
Return null;
}
If (secretKey. length ()! = 16 ){
Return null;
}
Cipher cipher = Cipher. getInstance ("AES/CBC/PKCS5Padding ");
Byte [] raw = secretKey. getBytes ();
SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES ");
IvParameterSpec iv = new IvParameterSpec (vector. getBytes (); // In CBC mode, a vector iv is required to increase the strength of the encryption algorithm.
Cipher. init (Cipher. ENCRYPT_MODE, skeySpec, iv );
Byte [] encrypted = cipher. doFinal (encData. getBytes ("UTF-8 "));
Return new BASE64Encoder (). encode (encrypted); // you can specify BASE64 encoding.
}
// ��
Public String encrypt (String sSrc) throws Exception {
Cipher cipher = Cipher. getInstance ("AES/CBC/PKCS5Padding ");
Byte [] raw = sKey. getBytes ();
SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES ");
IvParameterSpec iv = new IvParameterSpec (ivParameter. getBytes ()); // when CBC is used
Cipher. init (Cipher. ENCRYPT_MODE, skeySpec, iv );
Byte [] encrypted = cipher. doFinal (sSrc. getBytes ("UTF-8 "));
Return new BASE64Encoder (). encode (encrypted); // you can specify BASE64 encoding.
}
// ��
Public String decrypt (String sSrc) throws Exception {
Try {
Byte [] raw = sKey. getBytes ("ASCII ");
SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES ");
Cipher cipher = Cipher. getInstance ("AES/CBC/PKCS5Padding ");
IvParameterSpec iv = new IvParameterSpec (ivParameter. getBytes ());
Cipher. init (Cipher. DECRYPT_MODE, skeySpec, iv );
Byte [] encrypted1 = new BASE64Decoder (). decodeBuffer (sSrc); // �� base64 ��
Byte [] original = cipher. doFinal (encrypted1 );
String originalString = new String (original, "UTF-8 ");
Return originalString;
} Catch (Exception ex ){
Return null;
}
}
Public String decrypt (String sSrc, String key, String ivs) throws Exception {
Try {
Byte [] raw = key. getBytes ("ASCII ");
SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES ");
Cipher cipher = Cipher. getInstance ("AES/CBC/PKCS5Padding ");
IvParameterSpec iv = new IvParameterSpec (ivs. getBytes ());
Cipher. init (Cipher. DECRYPT_MODE, skeySpec, iv );
Byte [] encrypted1 = new BASE64Decoder (). decodeBuffer (sSrc); // �� base64 ��
Byte [] original = cipher. doFinal (encrypted1 );
String originalString = new String (original, "UTF-8 ");
Return originalString;
} Catch (Exception ex ){
Return null;
}
}
Public static String encodeBytes (byte [] bytes ){
StringBuffer strBuf = new StringBuffer ();
For (int I = 0; I <bytes. length; I ++ ){
StrBuf. append (char) (bytes [I]> 4) & 0xF) + (int) 'A ')));
StrBuf. append (char) (bytes [I]) & 0xF) + (int) 'A ')));
}
Return strBuf. toString ();
}
}