160705、總結:commons-codec.jar中常用方法


一、Base64編碼和解碼
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.binary.Base64;
public class TestBase64 {
    public static void main(String[] args) throws EncoderException, UnsupportedEncodingException {
        Base64 base64 = new Base64();
        String str = "AAaaa我";
        String result = base64.encodeToString(str.getBytes("UTF-8"));//編碼
        System.out.println(result);
        byte[] decode = base64.decode(result.getBytes());//解碼
        System.out.println(new String(decode));
    }
}
二、Hex編碼和解碼
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
public class TestHex {
    public static void main(String[] args) throws DecoderException, UnsupportedEncodingException {
        String str = "test";
        /**編碼*/
        String hexString = Hex.encodeHexString(str.getBytes("UTF-8"));//直接一步到位
        System.out.println(hexString);
        char[] encodeHex = Hex.encodeHex(str.getBytes(), true);//先轉換成char數組,第二個參數意思是是否全部轉換成小寫
        System.out.println(new String(encodeHex));
        /**解碼*/
        byte[] decodeHex = Hex.decodeHex(encodeHex);//char數組型的
        System.out.println(new String(decodeHex));
        byte[] decodeHex2 = Hex.decodeHex(hexString.toCharArray());//字符串類型的,該方法要求傳入的是char[]
        System.out.println(new String(decodeHex2));
    }
}
三、MD5加密(MD5是不可逆算法,只能加密)
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.digest.DigestUtils;
public class TestMD5 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "test";
        String md5 = DigestUtils.md5Hex(str.getBytes("UTF-8"));
        System.out.println(md5);
    }
}
四、SHA加密
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.digest.DigestUtils;
public class TestSHA {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "test中國";
        String sha1Hex = DigestUtils.sha1Hex(str.getBytes("UTF-8"));
        System.out.println(sha1Hex);
    }
}
五、Metaphone和Soundex
Metaphone 建立出相同的key給發音相似的單字, 比 Soundex 還要准確, 但是 Metaphone 沒有固定長度, Soundex 則是固定第一個英文字加上3個數字. 這通常是用在類似音比對, 也可以用在 MP3 的軟件開發
metaphone() 比 soundex() 函數更精確,因為 metaphone() 了解基本的英語發音規則

import org.apache.commons.codec.language.Metaphone;
import org.apache.commons.codec.language.RefinedSoundex;
import org.apache.commons.codec.language.Soundex;
public class TestMetaphoneAndSoundex {
    public static void main(String[] args) {
        String str = "testgggggg";
        /**Metaphone沒有固定長度*/
        Metaphone metaphone = new Metaphone();
        String metaphoneEncode = metaphone.encode(str);
        System.out.println(metaphoneEncode);
        /**RefinedSoundex*/
        RefinedSoundex refinedSoundex = new RefinedSoundex();
        String refinedSoundexEncode = refinedSoundex.encode(str);
        System.out.println(refinedSoundexEncode);
        /**Soundex固定第一個英文字加上3個數字*/
        Soundex soundex = new Soundex();
        String soundexEncode = soundex.encode(str);
        System.out.println(soundexEncode);
    }
}
六、URLCodec
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;
public class TestURLCodec {
    public static void main(String[] args) throws EncoderException, DecoderException {
        String url = "http://baidu.com?name=你好";
        URLCodec codec = new URLCodec();
        String encode = codec.encode(url);
        System.out.println(encode);
        String decode = codec.decode(encode);
        System.out.println(decode);
    }
}
除了這些還有很多算法比如HMAC等,大家可以根據需要選取,commons-codec-1.10.jar就是一個加密解碼相關的jar包


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM