Java uses Apache commons codec for MD5 encryption, BASE64 encryption and decryption, and executes system commands, commonsbase64
Before writing the code, let's first introduce the two packages we will use;
Commons-codec-1.10.jar
Tool packages used to process common encoding methods in the Commons project, such as DES, SHA1, MD5, Base64, URL, and Soundx.
Commons-exec-1.3.jar
Apache Commons Exec is a Java project on Apache. It provides common methods for executing external processes.
You can download the official Apache Commons package from this site.
The code structure is as follows:
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 all the main functions in a class file
Create a Test class to reference the previous file
Import security. encodeAndDecode; import security. exec; public class Test {public static void main (String [] args) {System. out. println (EncodeAndDecode. md5encode ("jb51.net"); // MD5 encryption System. out. println (EncodeAndDecode. base64encode ("jb51.net"); // Base64 encrypted System. out. println (EncodeAndDecode. base64decode ("amI1MS5uZXQ ="); // Base64 decrypt String str = Exec.exe c ("ping jb51.net"); // execute the System Ping command System. out. println (str );}}
Okay. Run the command to check the final result.
These are only the basic functions of the Apache commons package. For other functions, you can download the instructions for using apache commons in Chinese.