AES encryption tool,

Source: Internet
Author: User

AES encryption tool,

Public class AES {

/**
* Encryption
*
* @ Param content
* Content to be encrypted
* @ Param password
* Encrypted password
* @ Return
*/
Public static byte [] encrypt (String content, String password ){
Try {
KeyGenerator kgen = KeyGenerator. getInstance ("AES ");
Kgen. init (128, new SecureRandom (password. getBytes ()));
SecretKey secretKey = kgen. generateKey ();
Byte [] enCodeFormat = secretKey. getEncoded ();
SecretKeySpec key = new SecretKeySpec (enCodeFormat, "AES ");
Cipher cipher = Cipher. getInstance ("AES"); // create a secret
Byte [] byteContent = content. getBytes ("UTF-8 ");
Cipher. init (Cipher. ENCRYPT_MODE, key); // Initialization
Byte [] result = cipher. doFinal (byteContent );
Return result; // Encryption
} Catch (NoSuchAlgorithmException e ){
E. printStackTrace ();
} Catch (NoSuchPaddingException e ){
E. printStackTrace ();
} Catch (InvalidKeyException e ){
E. printStackTrace ();
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
} Catch (IllegalBlockSizeException e ){
E. printStackTrace ();
} Catch (BadPaddingException e ){
E. printStackTrace ();
}
Return null;
}

/**
* Decryption
*
* @ Param content
* Content to be decrypted
* @ Param password
* Decryption key
* @ Return
*/
Public static byte [] decrypt (byte [] content, String password ){
Try {
KeyGenerator kgen = KeyGenerator. getInstance ("AES ");
Kgen. init (128, new SecureRandom (password. getBytes ()));
SecretKey secretKey = kgen. generateKey ();
Byte [] enCodeFormat = secretKey. getEncoded ();
SecretKeySpec key = new SecretKeySpec (enCodeFormat, "AES ");
Cipher cipher = Cipher. getInstance ("AES"); // create a secret
Cipher. init (Cipher. DECRYPT_MODE, key); // Initialization
Byte [] result = cipher. doFinal (content );
Return result; // Encryption
} Catch (NoSuchAlgorithmException e ){
E. printStackTrace ();
} Catch (NoSuchPaddingException e ){
E. printStackTrace ();
} Catch (InvalidKeyException e ){
E. printStackTrace ();
} Catch (IllegalBlockSizeException e ){
E. printStackTrace ();
} Catch (BadPaddingException e ){
E. printStackTrace ();
}
Return null;
}

/**
* Convert binary to hexadecimal
*
* @ Param buf
* @ Return
*/
Public static String parseByte2HexStr (byte buf []) {
StringBuffer sb = new StringBuffer ();
For (int I = 0; I <buf. length; I ++ ){
String hex = Integer. toHexString (buf [I] & 0xFF );
If (hex. length () = 1 ){
Hex = '0' + hex;
}
Sb. append (hex. toUpperCase ());
}
Return sb. toString ();
}

/**
* Convert hexadecimal to binary.
*
* @ Param hexStr
* @ Return
*/
Public static byte [] parseHexStr2Byte (String hexStr ){
If (hexStr. length () <1)
Return null;
Byte [] result = new byte [hexStr. length ()/2];
For (int I = 0; I Int high = Integer. parseInt (hexStr. substring (I * 2, I * 2 + 1), 16 );
Int low = Integer. parseInt (hexStr. substring (I * 2 + 1, I * 2 + 2 ),
16 );
Result [I] = (byte) (high * 16 + low );
}
Return result;
}

/**
* Encryption
*
* @ Param content
* Content to be encrypted
* @ Param password
* Encrypted password
* @ Return
*/
Public static byte [] encrypt2 (String content, String password ){
Try {
SecretKeySpec key = new SecretKeySpec (password. getBytes (), "AES ");
Cipher cipher = Cipher. getInstance ("AES/ECB/NoPadding ");
Byte [] byteContent = content. getBytes ("UTF-8 ");
Cipher. init (Cipher. ENCRYPT_MODE, key); // Initialization
Byte [] result = cipher. doFinal (byteContent );
Return result; // Encryption
} Catch (NoSuchAlgorithmException e ){
E. printStackTrace ();
} Catch (NoSuchPaddingException e ){
E. printStackTrace ();
} Catch (InvalidKeyException e ){
E. printStackTrace ();
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
} Catch (IllegalBlockSizeException e ){
E. printStackTrace ();
} Catch (BadPaddingException e ){
E. printStackTrace ();
}
Return null;
}

Public static void main (String [] args ){
String content = "test ";
String password = "12345678 ";
// Encryption
System. out. println ("before encryption:" + content );
Byte [] encryptResult = encrypt (content, password );
// String tt4 = Base64.encode (encryptResult );
// System. out. println (new String (tt4 ));
System. out. println (encryptResult );
// Decrypt
Byte [] decryptResult = decrypt (encryptResult, password );
System. out. println ("decrypted:" + new String (decryptResult ));
}

}

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.