Java implementation of AES encryption algorithm simple example sharing _java

Source: Internet
Author: User
Tags decrypt

Advanced Encryption Standard (English: Advanced encryption Standard, abbreviation: AES), also known as the Rijndael encryption method in cryptography, is a block encryption standard used by the U.S. federal government. This standard, which replaces the original DES, has been analyzed and widely used worldwide.
Most AES calculations are done in a particular finite field.
The AES encryption process operates on a 4x4 byte matrix, also known as "State", whose initial value is a plaintext block (one element in a matrix is a byte in a plain text block). (Rijndael encryption method to support a larger block, its matrix row number can increase visibility) encryption, the rounds of AES encryption cycle (except the last round) contains 4 steps:

    • Each byte in the addroundkey-matrix is XOR with the second wheel secret key (round key), and each subkey is generated by the key generation scheme.
    • Subbytes-uses a non-linear substitution function to replace each byte with the corresponding byte in a lookup table.
    • shiftrows-shifts each row in the matrix in a cyclic style.
    • mixcolumns-to fully blend the operations of each straight line in the matrix. This step uses a linear transformation to mix four bytes of each column.

Omit the Mixcolumns step in the last cryptographic loop and replace it with another addroundkey.

Java Basic implementation:

Package com.stone.security; 
 
Import Java.util.Arrays; 
Import Javax.crypto.Cipher; 
Import Javax.crypto.KeyGenerator; 
Import Javax.crypto.SecretKey; 
 
Import Javax.crypto.spec.IvParameterSpec; 
 /** * AES Algorithm symmetric encryption, cryptography, the Advanced Encryption Standard 2005 become a valid standard * * public class AES {static Cipher Cipher; 
 Static final String key_algorithm = "AES"; 
 Static final String CIPHER_ALGORITHM_ECB = "aes/ecb/pkcs5padding"; 
Static final String CIPHER_ALGORITHM_CBC = "aes/cbc/pkcs5padding"; /** * aes/cbc/nopadding requirement * Key must be 16 bits, initialization vector (IV) must be 16 bits * The length of the content to be encrypted must be a multiple of 16, if not multiples of 16, the following exception will occur: * j 
 Avax.crypto.IllegalBlockSizeException:Input length not multiple of bytes * * * Due to the fixed number of digits, so for the encrypted data has Chinese, add, decrypt incomplete * * Can be seen, in the original data length of 16 integer n times, if the original data length equals 16*n, then use nopadding when the data length is equal to 16*n, * Other cases encrypted data length equal to 16* (n+1). 
 In the case of less than 16 integer times, if the original data length equals 16*n+m[where M is less than 16], the encrypted data length equals 16* (n+1) in any way except for the nopadding padding. 
 
 * * Static final String cipher_algorithm_cbc_nopadding = "aes/cbc/nopadding"; Static SecreTKey Secretkey; 
 public static void Main (string[] args) throws Exception {method1 ("A*jal) k32j8czx 囙 Country is country wide"); 
 METHOD2 ("A*jal) k32j8czx 囙 State"); 
  
 Method3 ("A*jal) k32j8czx 囙 State"); METHOD4 ("123456781234 囙 for Country Wide");/length = METHOD4 ("12345678abcdefgh");/length = 16}/** * uses AES algorithm to encrypt, default mode 
 Type AES/ECB */static void method1 (String str) throws Exception {cipher = Cipher.getinstance (key_algorithm); 
 Keygenerator generates AES algorithm key Secretkey = Keygenerator.getinstance (key_algorithm). GenerateKey (); 
  
 System.out.println ("The length of the key is:" + secretkey.getencoded (). length); Cipher.init (Cipher.encrypt_mode, Secretkey);//Use encryption mode to initialize the key byte[] ENCRYPT = cipher.dofinal (Str.getbytes ()); 
  
 Encrypt or decrypt data in one-part operation, or end a multiple-part operation. 
 SYSTEM.OUT.PRINTLN ("method1-encryption:" + arrays.tostring (encrypt)); 
 Cipher.init (Cipher.decrypt_mode, Secretkey)//Use decryption mode to initialize the key byte[] DECRYPT = cipher.dofinal (encrypt); 
  
 System.out.println ("method1-after decryption:" + new String (decrypt)); /** * uses AES algorithm encryption, default mode aes/ecb/pkcs5padding */static void Method2 (String str) throws Exception {cipher = Cipher.getinstance (cipher_algorithm_e 
 CB); 
 Keygenerator generates AES algorithm key Secretkey = Keygenerator.getinstance (key_algorithm). GenerateKey (); 
  
 System.out.println ("The length of the key is:" + secretkey.getencoded (). length); Cipher.init (Cipher.encrypt_mode, Secretkey);//Use encryption mode to initialize the key byte[] ENCRYPT = cipher.dofinal (Str.getbytes ()); 
  
 Encrypt or decrypt data in one-part operation, or end a multiple-part operation. 
 SYSTEM.OUT.PRINTLN ("method2-encryption:" + arrays.tostring (encrypt)); 
 Cipher.init (Cipher.decrypt_mode, Secretkey)//Use decryption mode to initialize the key byte[] DECRYPT = cipher.dofinal (encrypt); 
  
 System.out.println ("method2-after decryption:" + new String (decrypt));  
 Static byte[] Getiv () {String IV = "1234567812345678";//iv length:must is bytes long return iv.getbytes (); /** * Using AES algorithm encryption, default mode aes/cbc/pkcs5padding/static void method3 (String str) throws Exception {cipher 
 = Cipher.getinstance (CIPHER_ALGORITHM_CBC); Keygenerator generate AES Algorithm key Secretkey = KeYgenerator.getinstance (Key_algorithm). GenerateKey (); 
  
 System.out.println ("The length of the key is:" + secretkey.getencoded (). length); Cipher.init (Cipher.encrypt_mode, Secretkey, New Ivparameterspec (Getiv ());//Use encryption mode to initialize the key byte[] ENCRYPT = Cipher.dofinal (Str.getbytes ()); 
  
 Encrypt or decrypt data in one-part operation, or end a multiple-part operation. 
 SYSTEM.OUT.PRINTLN ("method3-encryption:" + arrays.tostring (encrypt)); Cipher.init (Cipher.decrypt_mode, Secretkey, New Ivparameterspec (Getiv ()))//Use decryption mode to initialize the key byte[] DECRYPT = 
 Cipher.dofinal (encrypt); 
  
 System.out.println ("method3-after decryption:" + new String (decrypt));  /** * uses AES algorithm encryption, default mode aes/cbc/nopadding see above data limits for this mode/static void Method4 (String str) throws Exception 
 {cipher = Cipher.getinstance (cipher_algorithm_cbc_nopadding); 
 Keygenerator generates AES algorithm key Secretkey = Keygenerator.getinstance (key_algorithm). GenerateKey (); 
  
 System.out.println ("The length of the key is:" + secretkey.getencoded (). length); Cipher.init (Cipher.encrypt_mode, Secretkey, New Ivparameterspec (Getiv ()));//Use encryption mode to initialize the secretKey byte[] Encrypt = cipher.dofinal (str.getbytes (), 0, Str.length ()); 
  
 Encrypt or decrypt data in one-part operation, or end a multiple-part operation. 
 SYSTEM.OUT.PRINTLN ("method4-encryption:" + arrays.tostring (encrypt)); Cipher.init (Cipher.decrypt_mode, Secretkey, New Ivparameterspec (Getiv ()))//Use decryption mode to initialize the key byte[] DECRYPT = 
  
 Cipher.dofinal (encrypt); 
  
 System.out.println ("method4-after decryption:" + new String (decrypt));  } 
 
}

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.