Codec of apache commons tool class

Codec provides some common codec implementations, such as Base64, Base32, Md5Crypt, Crypt, etc.
package commons;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.Md5Crypt;

public class TestBase64 {

	// Base64 encoding
	public static String encodeTest(String str) {
		Base64 base64 = new Base64();
		try {
			str = base64.encodeToString(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace ();
		}
		System.out.println("Base64 encoded: " + str);
		return str;
	}
    
	// Base64 decoding
	public static void decodeTest(String str) {
		str = new String(Base64.decodeBase64(str));
		System.out.println("Base64 decoded: " + str);
	}
    
	
	@SuppressWarnings("static-access")
	public static void main(String[] args) throws UnsupportedEncodingException {
		decodeTest(encodeTest("123456"));
		Md5Crypt md5Crypt = new Md5Crypt();
		System.out.println(md5Crypt.md5Crypt("123456".getBytes("UTF-8")));
		System.out.println(md5Crypt.apr1Crypt("123456".getBytes("UTF-8")));
	}

}



Running result
After Base64 encoding: MTIzNDU2 After
Base64 decoding: 123456
$1$t8PuI1u/$7B.krpsbywf6cDFYsb6RT0
$apr1$nl6D999g$ahRrFwNuW3V5gFfs.WS8P/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326583138&siteId=291194637