• commons-codec中常用方法


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

    五、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等,大家可以根据需要选取

  • 相关阅读:
    VECTOR的用法
    【转】C/C++中的Split函数(字符串自动分割)
    【转】动态树
    【实验】vector性质
    【转】Android操作系统安全研究系列——键盘记录
    D8神贴
    【操作系统】修改WIN7下的文件关联与默认程序
    【转】探寻C++最快的读取文件的方案(方法未论证)
    如何在60分钟之内过掉600个单词
    【转】动态树:实现
  • 原文地址:https://www.cnblogs.com/deityjian/p/11449389.html
Copyright © 2020-2023  润新知