AES encryption, aes

Source: Internet
Author: User

AES encryption, aes

Package com.edu. hpu;

 

Import java. math. BigInteger;

Import java. security. MessageDigest;

Import java. security. SecureRandom;

 

Import javax. crypto. Cipher;

Import javax. crypto. KeyGenerator;

Import javax. crypto. spec. SecretKeySpec;

 

Import sun. misc. BASE64Decoder;

Import sun. misc. BASE64Encoder;

 

Public class No14 {

Public static void main (String [] args) throws Exception {

String content = "***";

System. out. println ("before encryption:" + content );

String key = "123 ";

System. out. println ("encryption key and decryption key:" + (int) '1' + "" + (int) '2' + "" + (int) '3 ');

String encrypt = aesEncrypt (content, key );

System. out. println ("ciphertext:" + encrypt );

String decrypt = aesDecrypt (encrypt, key );

System. out. println ("decrypted:" + decrypt );

}

/**

* Convert byte [] to various hexadecimal strings.

* @ Param bytes byte []

* @ Param radix: converts the hexadecimal range from Character. MIN_RADIX to Character. MAX_RADIX. If the range is exceeded, it changes to 10 hexadecimal.

* @ Return refers to the converted string.

*/

Public static String binary (byte [] bytes, int radix ){

Return new BigInteger (1, bytes). toString (radix); // here 1 represents a positive number

}

/**

* Base 64 encode

* @ Param bytes byte to be encoded []

* @ Return encoded base 64 code

*/

Public static String base64Encode (byte [] bytes ){

Return new BASE64Encoder (). encode (bytes );

}

/**

* Base 64 decode

* @ Param base64Code base 64 code to be decoded

* @ Return the decoded byte []

* @ Throws Exception

*/

Public static byte [] base64Decode (String base64Code) throws Exception {

Return QStringUtil. isEmpty (base64Code )? Null: new BASE64Decoder (). decodeBuffer (base64Code );

}

/**

* Obtain the md5 value of byte []

* @ Param bytes byte []

* @ Return md5

* @ Throws Exception

*/

Public static byte [] md5 (byte [] bytes) throws Exception {

MessageDigest md = MessageDigest. getInstance ("MD5 ");

Md. update (bytes );

Return md. digest ();

}

/**

* Obtain the md5 value of a string.

* @ Param msg

* @ Return md5

* @ Throws Exception

*/

Public static byte [] md5 (String msg) throws Exception {

Return QStringUtil. isEmpty (msg )? Null: md5 (msg. getBytes ());

}

/**

* Implements md5 encryption with base64

* @ Param msg the string to be encrypted

* @ Return convert the md5 value to base64

* @ Throws Exception

*/

Public static String md5Encrypt (String msg) throws Exception {

Return QStringUtil. isEmpty (msg )? Null: base64Encode (md5 (msg ));

}

/**

* AES Encryption

* @ Param content to be encrypted

* @ Param encryptKey: encryption key

* @ Return encrypted byte []

* @ Throws Exception

*/

Public static byte [] aesEncryptToBytes (String content, String encryptKey) throws Exception {

// Encryption and decryption are added; otherwise, the encryption results of different machines are inconsistent.

KeyGenerator kgen = KeyGenerator. getInstance ("AES ");

SecureRandom random = SecureRandom. getInstance ("SHA1PRNG ");

Random. setSeed (encryptKey. getBytes ());

Kgen. init (128, random );

// General statement

// KeyGenerator kgen = KeyGenerator. getInstance ("AES ");

// Kgen. init (128, new SecureRandom (encryptKey. getBytes ()));

Cipher cipher = Cipher. getInstance ("AES ");

Cipher. init (Cipher. ENCRYPT_MODE, new SecretKeySpec (kgen. generateKey (). getEncoded (), "AES "));

Return cipher. doFinal (content. getBytes ("UTF-8 "));

}

/**

* AES encrypted to base 64 code

* @ Param content to be encrypted

* @ Param encryptKey: encryption key

* @ Return encrypted base 64 code

* @ Throws Exception

*/

Public static String aesEncrypt (String content, String encryptKey) throws Exception {

Return base64Encode (aesEncryptToBytes (content, encryptKey ));

}

/**

* AES decryption

* @ Param encryptBytes the byte to be decrypted []

* @ Param decryptKey decryption key

* @ Return refers to the decrypted String

* @ Throws Exception

*/

Public static String aesdecryptbytes (byte [] encryptBytes, String decryptKey) throws Exception {

// Encryption and decryption are added; otherwise, the encryption results of different machines are inconsistent.

KeyGenerator kgen = KeyGenerator. getInstance ("AES ");

SecureRandom random = SecureRandom. getInstance ("SHA1PRNG ");

Random. setSeed (decryptKey. getBytes ());

Kgen. init (128, random );

// General statement

// KeyGenerator kgen = KeyGenerator. getInstance ("AES ");

// Kgen. init (128, new SecureRandom (decryptKey. getBytes ()));

Cipher cipher = Cipher. getInstance ("AES ");

Cipher. init (Cipher. DECRYPT_MODE, new SecretKeySpec (kgen. generateKey (). getEncoded (), "AES "));

Byte [] decryptBytes = cipher. doFinal (encryptBytes );

Return new String (decryptBytes );

}

/**

* Decrypt base 64-code AES

* @ Param encryptStr base 64 code to be decrypted

* @ Param decryptKey decryption key

* @ Return refers to the decrypted string

* @ Throws Exception

*/

Public static String aesDecrypt (String encryptStr, String decryptKey) throws Exception {

Return QStringUtil. isEmpty (encryptStr )? Null: aesdecryptbytes (base64Decode (encryptStr), decryptKey );

}

}

 

Class QStringUtil {

Public static boolean isEmpty (String str ){

If (str = null ){

Return true;

} Else {

Return false;

}

 

}

}

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.