AES encryption algorithm, user-sensitive information encryption, and aes Encryption Algorithm

Source: Internet
Author: User

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 ();
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.