Java uses Apache commons codec for MD5 encryption, BASE64 encryption and decryption, and executes system commands, commonsbase64

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.