commons-codec method commonly used

A, Base64 encoding and decoding

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));
    }
}

Two, Hex encoding and decoding

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" ;
         / ** coding * / 
        String hexstring = Hex.encodeHexString (str.getBytes ( "UTF-. 8")); // directly in one step 
        System.out.println (hexstring);
         char [] = encodeHex Hex.encodeHex (STR. the getBytes (), to true ); // be converted to char array, whether the second parameter is the mean of all converted into lowercase
        System.out.println ( new new String (encodeHex));
         / ** decoding * / 
        byte [] = decodeHex Hex.decodeHex (encodeHex); // char array type 
        System.out.println ( new new String (decodeHex));
         byte [] = decodeHex2 Hex.decodeHex (hexString.toCharArray ()); // string type, this method requires that the incoming char [] 
        System.out.println ( new new string (decodeHex2)); 
    } 
}

Three, MD5 encryption (MD5 algorithm is not reversible, can encrypt)

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);
    }
}

Four, SHA encryption

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);
    }
}

Five, 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);
    }
}

In addition to these, there are many algorithms such as HMAC, etc., you may need to select

Guess you like

Origin www.cnblogs.com/deityjian/p/11449389.html