Example of AES algorithm implemented by Java native method

  • 2021-07-02 23:59:15
  • OfStack

In this paper, an example is given to realize AES algorithm by Java native method. Share it for your reference, as follows:

AES (Advanced Encryption Standard) advanced encryption standard, also known as Rijndael encryption method in cryptography, is a block encryption standard adopted by the US federal government. This standard is used to replace the original DES, which has been analyzed by many parties and widely used all over the world. Now it has become one of the most popular algorithms in symmetric key encryption.


/**
 * AES  Algorithm 
 * <p/>
 *  The algorithm adopts encryption mode: CBC ; Data block: 128 ; Fill: PKCS5Padding
 * <p/>
 * key  And the length of the vector string is  16  Bit 
 *
 * @author Deniro Li (lisq037@163.com)
 *     2018/3/17
 */
public class AES {
  /**
   *  Algorithm name 
   */
  public static final String NAME = "AES";
  /**
   *  Encryption mode: CBC ; Data block: 128 ; Fill: PKCS5Padding
   */
  public final String MODE = "AES/CBC/PKCS5Padding";
  /**
   * KEY  And   Length of vector string 
   */
  public static final int LENGTH = 16;
  /**
   *  For encryption  KEY
   */
  private String key;
  /**
   *  Vector, used to increase encryption strength 
   */
  private String ivParameter;
  /**
   * @param key      For encryption  KEY
   * @param ivParameter  Offset 
   */
  public AES(String key, String ivParameter) {
    if (key == null || key.length() != LENGTH) {
      throw new AESException("KEY  Does not exist, or the length is not  " + LENGTH);
    }
    if (ivParameter == null || ivParameter.length() != LENGTH) {
      throw new AESException("ivParameter  Does not exist, or the length is not  " + LENGTH);
    }
    this.key = key;
    this.ivParameter = ivParameter;
  }
  /**
   *  Encryption 
   *
   * @param s  String to encrypt 
   * @return  Encrypted string 
   */
  public String encode(String s) {
    String result;
    try {
      Cipher cipher = Cipher.getInstance(MODE);
      IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
      cipher.init(ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), NAME), iv);
      byte[] bytes = cipher.doFinal(s.getBytes(ENCODING));
      result = new BASE64Encoder().encode(bytes);
    } catch (Exception e) {
      throw new AESException(" Encryption ", e);
    }
    return result;
  }
  /**
   *  Decryption 
   *
   * @param s  String to be decrypted 
   * @return  Decrypted string 
   */
  public String decode(String s) {
    try {
      SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("ASCII"), NAME);
      Cipher cipher = Cipher.getInstance(MODE);
      IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
      cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
      return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(s)), ENCODING);
    } catch (Exception e) {
      throw new AESException(" Decryption ", e);
    }
  }
}

Unit testing:


public class AESTest {
  AES aes;
  @Before
  public void init(){
    aes=new AES("12345abcdef67890","1234567890abcdef");
  }
  @Test
  public void testEncode() throws Exception {
    Assert.assertEquals("jANei3LBvnLCaZ2XddWHZw==", aes.encode("123456"));
  }
  @Test
  public void testDecode() throws Exception {
    Assert.assertEquals("123456", aes.decode("jANei3LBvnLCaZ2XddWHZw=="));
  }
}

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools.ofstack.com/password/txt_encode

MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
http://tools.ofstack.com/password/sha_encode

More readers interested in java can check the topics of this site: "Summary of Java Mathematical Operation Skills", "Java Data Structure and Algorithm Tutorial", "Summary of Java Character and String Operation Skills", "Summary of DOM Node Operation Skills of Java" and "Summary of Java Cache Operation Skills"

I hope this article is helpful to everyone's java programming.


Related articles: