AES Algorithm Encryption Java implementation

Source: Internet
Author: User
Tags decrypt

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/Fill method
*/
public static final String cipher_algorithm = "aes/ecb/pkcs5padding";

/**
* Convert secret key
* @param key Binary key
* @return Key key
* @throws Exception
*/
private static Key Tokey (byte[] key) throws Exception {
Instantiating AES keying material
Secretkey Secretkey = new Secretkeyspec (key, Key_algorithm);
return secretkey;
}

/**
* @param data with decryption
* @param key key
* @return byte[] Decrypt data
* @throws Exception
*/
public static byte[] Decrypt (byte[] data, byte[] key) throws Exception {
Restore Secret Keys
Key k = Tokey (key);
Instantiation of
Cipher Cipher = cipher.getinstance (cipher_algorithm);
Initialize, set decryption mode
Cipher.init (Cipher.decrypt_mode, k);
Perform actions
return cipher.dofinal (data);
}

/**
* Encryption
* @param data with encryption
* @param key key
* @return byte[] Encrypt data
* @throws Exception
*/
public static byte[] Encrypt (byte[] data, byte[] key) throws Exception {
Restore Secret Keys
Key k = Tokey (key);
Instantiation of
Cipher Cipher = cipher.getinstance (cipher_algorithm);
Initialize, set encryption mode
Cipher.init (Cipher.encrypt_mode, k);
Perform actions
return cipher.dofinal (data);
}

/**
* Generate secret key
* Java7 only supports 56-bit keys
* Bouncy Castle support 64-bit secret
* @return Binary key
* @throws Exception
*/
public static byte[] Initkey () throws Exception {
/**
* Instantiate key generator
* If you want to use the 64-bit key to be replaced by Keygenerator.getinstance (Cipher__algorithm, "BC");
* "BC" is an abbreviation for Bouncy castle security provider.
*/
keygenerator kg = keygenerator.getinstance (key_algorithm);
Initialize key generator
Kg.init (128);
Generate secret Key
Secretkey Secretkey = Kg.generatekey ();
Get the binary encoded form of the secret key
return secretkey.getencoded ();
}

}


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


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 ("secret key: \ t" + base64.encodebase64string (key));
Encryption
Inputdata = Descoder.encrypt (Inputdata, key);
SYSTEM.OUT.PRINTLN ("after encryption: \ t" + base64.encodebase64string (inputdata));
byte[] Outputdtat = Descoder.decrypt (Inputdata, key);
String outputstr = new string (OUTPUTDTAT);
System.out.println ("After decryption: \ t" + outputstr);
Boolean bool = inputstr.equals (OUTPUTSTR);
SYSTEM.OUT.PRINTLN (BOOL);

}
}

AES Algorithm Encryption Java implementation

Related Article

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.