java USES Hex encoding and decoding to implement the example of Aes encryption and decryption function

  • 2020-05-27 05:35:54
  • OfStack

The example of this paper describes the implementation of Aes encryption and decryption by java using Hex encoding and decoding. I will share it with you for your reference as follows:

The Aes encryption and decryption method here is encoded and decoded using Hex


package com.baidu.wallet.bdwallet.utils;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
public class Test {
   private static final String AES="AES";
   private static final String UTF8="UTF-8";
    /**
    * AES encryption 
    * @param content
    * @param pkey
    * @return
    * @throws DecoderException
    */
    private static byte[] encrypt(String content, String pkey) throws DecoderException {
      try {
        String private_key=pkey;
        byte[] encodeFormat=null;
        try {
          // The secret key  Hex Decoding why should the secret key be decoded, because the secret key is some secret key that is in clear text Hex The encoded value, so it should be decoded when used 
          encodeFormat = Hex.decodeHex(private_key.toCharArray());
        } catch (DecoderException e) {
          e.printStackTrace();
        }
        SecretKeySpec key = new SecretKeySpec(encodeFormat, AES);
        // Cipher The object actually does the encryption 
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        //  Encrypted content is encoded 
        byte[] byteContent = content.getBytes(UTF8);
        //  Initialize with a key Cipher object 
        cipher.init(Cipher.ENCRYPT_MODE, key);
        //  Formally perform the encryption operation 
        byte[] result = cipher.doFinal(byteContent);
        return result;
      } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
      } catch (NoSuchPaddingException e) {
        e.printStackTrace();
      } catch (InvalidKeyException e) {
        e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
      } catch (BadPaddingException e) {
        e.printStackTrace();
      }
      return null;
    }
    /**
     * AES decryption 
     * @param contents
     * @param password
     * @return
     * @throws DecoderException
     */
    private static byte[] decrypt(String contents, String password) throws DecoderException {
      try {
        // Cipher using Hex decoding 
        byte[]content = Hex.decodeHex(contents.toCharArray());
        // The secret key  Hex Decoding why should the secret key be decoded, because the secret key is some secret key that is in clear text Hex The encoded value, so it should be decoded when used 
        byte[] encodeFormat = Hex.decodeHex(password.toCharArray());
        SecretKeySpec key = new SecretKeySpec(encodeFormat, AES);
        // Cipher The object actually does the encryption 
        Cipher cipher = Cipher.getInstance(AES);
        //  Initialize with a key Cipher object 
        cipher.init(Cipher.DECRYPT_MODE, key);
        //  Officially execute the decryption operation 
        byte[] result = cipher.doFinal(content);
        return result;
      } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
      } catch (NoSuchPaddingException e) {
        e.printStackTrace();
      } catch (InvalidKeyException e) {
        e.printStackTrace();
      } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
      } catch (BadPaddingException e) {
        e.printStackTrace();
      }
      return null;
    }
    /**
     * Aes encryption 
     * @param context  clear 
     * @param private_key  The secret key 
     * @return
     * @throws DecoderException
     */
    public static String encryption(String context,String private_key) throws DecoderException{
      // The encrypted plaintext becomes ciphertext 
      byte[] encryptResult = encrypt(context, private_key);
      // Password, Hex coding 
      String encryptResultStr = Hex.encodeHexString(encryptResult);
      return encryptResultStr;
    }
    /**
    * Aes decryption 
    * @param context  cipher 
    * @param private_key  The secret key 
    * @return
    * @throws DecoderException
    * @throws UnsupportedEncodingException
    */
    public static String decryption(String context,String private_key) throws DecoderException, UnsupportedEncodingException{
     // The ciphertext here was decrypted before it was decrypted Hex decoding 
      byte[] decryptResult = decrypt(context, private_key);
      String result = new String(decryptResult, UTF8);
      return result;
    }
    public static void main(String[] args) throws UnsupportedEncodingException, DecoderException {
      // Encrypted content 
      String content = "123456787654321";
      //AES Encrypt the decryption key 
      String password = " This value 1 General are given, double hair know ";
      //  encryption 
      System.out.println(" Encrypted before: " + content);
      //  Call the encryption method 
      String encryptResultStr = encryption(content, password);
      System.out.println(" After the encryption: " + encryptResultStr);
      //  Call the decryption method 
      String result = decryption(encryptResultStr, password);
      //  Decrypted content is decoded 
      System.out.println(" After decryption: " + result);
    }
}

This method is already in use in formal projects, no problem, note that the AES encryption and decryption here you have to be right...

Used above is org. apache. commons. codec. binary. Hex methods of this class, in maven configuration is as follows:


<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.4</version>
</dependency>

Note: version 1.4 and above should be used here, because there is no Hex.encodeHexString (byte[]) method below 1.4!

PS: about encryption and decryption interested friends can also refer to the website online tools:

Password security online detection:
http://tools.ofstack.com/password/my_password_safe

High strength password generator:
http://tools.ofstack.com/password/CreateStrongPassword

Thunderbolt, express, whirlwind URL encryption/decryption tools:
http://tools.ofstack.com/password/urlrethunder

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

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

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

I hope this article is helpful to you java programming.


Related articles: