java利用Apache commons codec進行MD5加密,BASE64加密解密,執行系統命令,commonsbase64

來源:互聯網
上載者:User

java利用Apache commons codec進行MD5加密,BASE64加密解密,執行系統命令,commonsbase64

編寫代碼之前先來介紹一下我們要用到的兩個包;

commons-codec-1.10.jar
Commons項目中用來處理常用的編碼方法的工具類包,例如DES、SHA1、MD5、Base64,URL,Soundx等等。

commons-exec-1.3.jar
Apache Commons Exec 是 Apache 上的一個 Java 項目,提供一些常用的方法用來執行外部進程

你可以到本站直接下載 Apache Commons 官方包

下面看一下代碼結構:

import org.apache.commons.codec.binary.Base64;import org.apache.commons.codec.digest.DigestUtils;/*** @author Delver_Si**/public class EncodeAndDecode {  /**   * Md5加密   * @param str   * @return   */  public static String Md5encode(String str) {    return DigestUtils.md5Hex(str);  }    /**   * Base64加密   * @param str   * @return   */  public static String Base64encode(String str) {    byte[] b = Base64.encodeBase64(str.getBytes(), true);    return new String(b);  }  /**   * Base64解密   * @param str   * @return   */  public static String Base64decode(String str) {    byte[] b = Base64.decodeBase64(str.getBytes());    return new String(b);  }    /**   * 產生SHA1   */  public static String SHA1encode(String str) {    return DigestUtils.sha1Hex(str);  }}

把主要功能都放在一個類檔案中

建立Test類引用上個檔案

import security.EncodeAndDecode;import security.Exec;public class Test {  public static void main(String[] args) {    System.out.println(EncodeAndDecode.Md5encode("jb51.net"));//MD5加密    System.out.println(EncodeAndDecode.Base64encode("jb51.net"));//Base64加密    System.out.println(EncodeAndDecode.Base64decode("amI1MS5uZXQ="));//Base64解密        String str = Exec.exec("ping jb51.net");//執行系統的Ping命令    System.out.println(str);  }}

好了 ,運行一下看看最終結果

這些只是Apache commons 包的準系統,其它功能大家可以到這裡下載 apache commons 使用說明 中文WORD版 詳細研究

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.