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