Before writing the code, let’s introduce the two packages we want to use;
commons-codec-1.10.jar
Tool packages used in the Commons project to handle common coding methods, such as DES, SHA1, MD5, Base64, URL, Soundx, etc.
commons-exec-1.3.jar
Apache Commons Exec is a Java project on Apache, which provides some common methods to execute external processes.
You can download the official Apache Commons package directly from this site
Let's take a look at the code structure:
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 decrypt* @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 refer to the previous file
import security.EncodeAndDecode;import security.Exec;public class Test { public static void main(String[] args) { System.out.println(EncodeAndDecode.Md5encode("VeVB.COM"));//MD5 encryption System.out.println(EncodeAndDecode.Base64encode("VeVB.COM"));//Base64 encryption System.out.println(EncodeAndDecode.Base64decode("amI1MS5uZXQ="));//Base64 decrypt String str = Exec.exec("ping VeVB.COM");//Execute the system's Ping command System.out.println(str); }}OK, run it to see the final result
These are just the basic functions of the Apache commons package. For other functions, you can download the apache commons instructions for use in Chinese WORD version. Detailed research on the other functions.