java uses Apache commons codec to encrypt MD5 BASE64 to encrypt and decrypt and execute system commands

  • 2020-12-05 17:15:51
  • OfStack

Before writing the code, let's introduce the two packages we will use.

commons-codec-1.10.jar
Package of tools used in the Commons project to handle common coding methods, such as DES, SHA1, MD5, Base64, URL, Soundx, and so on.

commons-exec-1.3.jar
Apache Commons Exec is an Java project on Apache that provides 1 of the common methods used to execute external processes

You can download the Apache Commons official package directly from this website

Let's look at the code structure of 1:


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

/**
* @author Delver_Si
*
*/
public class EncodeAndDecode {
  /**
   * Md5 encryption 
   * @param str
   * @return
   */
  public static String Md5encode(String str) {
    return DigestUtils.md5Hex(str);
  }
  
  /**
   * Base64 encryption 
   * @param str
   * @return
   */
  public static String Base64encode(String str) {
    byte[] b = Base64.encodeBase64(str.getBytes(), true);
    return new String(b);
  }
  /**
   * Base64 decryption 
   * @param str
   * @return
   */
  public static String Base64decode(String str) {
    byte[] b = Base64.decodeBase64(str.getBytes());
    return new String(b);
  }
  
  /**
   *  generate SHA1
   */
  public static String SHA1encode(String str) {
    return DigestUtils.sha1Hex(str);
  }

}

Put the main functions in one class file

Create a new Test class to reference the last file


import security.EncodeAndDecode;
import security.Exec;


public class Test {
  public static void main(String[] args) {
    System.out.println(EncodeAndDecode.Md5encode("ofstack.com"));//MD5 encryption 
    System.out.println(EncodeAndDecode.Base64encode("ofstack.com"));//Base64 encryption 
    System.out.println(EncodeAndDecode.Base64decode("amI1MS5uZXQ="));//Base64 decryption 
    
    String str = Exec.exec("ping ofstack.com");// Executive system Ping The command 
    System.out.println(str);
  }
}

Ok, so let's run 1 and see what happens

These are the basic functions of Apache commons package. You can download apache commons here


Related articles: