Java Implementation of AES algorithm Encryption

Source: Internet
Author: User

Java Implementation of AES algorithm Encryption
Package cn. itcast. coderUtils;

Import java. security. Key;
Import javax. crypto. Cipher;
Import javax. crypto. KeyGenerator;
Import javax. crypto. SecretKey;
Import javax. crypto. spec. SecretKeySpec;



Public class AESCoder {

Public static final String KEY_ALGORITHM = "AES ";

/**
* Encryption, decryption, working mode, and filling mode
*/
Public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding ";

/**
* Key Conversion
* @ Param key binary key
* @ Return Key
* @ Throws Exception
*/
Private static Key toKey (byte [] key) throws Exception {
// Instantiate the AES key material
SecretKey secretKey = new SecretKeySpec (key, KEY_ALGORITHM );
Return secretKey;
}


/**
* @ Param data decrypts data
* @ Param key
* @ Return byte [] decrypts data
* @ Throws Exception
*/
Public static byte [] decrypt (byte [] data, byte [] key) throws Exception {
// Restore the key
Key k = toKey (key );
// Instantiate
Cipher cipher = Cipher. getInstance (CIPHER_ALGORITHM );
// Initialize and set the decryption Mode
Cipher. init (Cipher. DECRYPT_MODE, k );
// Perform the operation
Return cipher. doFinal (data );
}

/**
* Encryption
* @ Param data with encrypted data
* @ Param key
* @ Return byte [] encrypt data
* @ Throws Exception
*/
Public static byte [] encrypt (byte [] data, byte [] key) throws Exception {
// Restore the key
Key k = toKey (key );
// Instantiate
Cipher cipher = Cipher. getInstance (CIPHER_ALGORITHM );
// Initialize and set the encryption mode
Cipher. init (Cipher. ENCRYPT_MODE, k );
// Perform the operation
Return cipher. doFinal (data );
}

/**
* Generate a secret key
* Java 7 only supports 56-bit keys.
* Bouncy Castle supports 64-bit secrets
* @ Return binary key
* @ Throws Exception
*/
Public static byte [] initKey () throws Exception {
/**
* Instantiate the key generator.
* To use a 64-bit key, replace it with KeyGenerator. getInstance (cipherpolicalgorithm, "BC ");
* "BC" is the abbreviation of Bouncy Castle security provider.
*/
KeyGenerator kg = KeyGenerator. getInstance (KEY_ALGORITHM );
// Initialize the key generator
Kg. init (128 );
// Generate a secret key
SecretKey secretKey = kg. generateKey ();
// Obtain the binary encoding form of the key
Return secretKey. getEncoded ();
}

}


The above code implementation is relatively common and can be used for DES, DESede (3DES), RC2, RC4 and other algorithms. You only need to slightly adjust the algorithm name.


Test Case code:

Package cn. itcast. testUtils;

Import org. apache. commons. codec. binary. Base64;
Import org. junit. Test;
Import com. sun. enterprise. security. auth. login. AssertedCredentials;
Import cn. itcast. coderUtils. DESCoder;
Public class AESCoderTest {


@ Test
Public void testAES () throws Exception {
String inputStr = "AES ";
Byte [] inputData = inputStr. getBytes ();
System. out. println ("Original: \ t" + inputStr );
Byte [] key = DESCoder. initKey ();
System. out. println ("key: \ t" + Base64.encodeBase64String (key ));
// Encryption
InputData = DESCoder. encrypt (inputData, key );
System. out. println ("encrypted: \ t" + Base64.encodeBase64String (inputData ));
Byte [] outputDtat = DESCoder. decrypt (inputData, key );
String outputStr = new String (outputDtat );
System. out. println ("decrypted: \ t" + outputStr );
Boolean bool = inputStr. equals (outputStr );
System. out. println (bool );

}
}

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.