zoukankan      html  css  js  c++  java
  • commons-codec介绍

    commons-codec是Apache开源组织提供的用于摘要运算、编码解码的包。常见的编码解码工具Base64、MD5、Hex、SHA1、DES等。

    /**
     * *********** Base64编码和解码  ***********
     * 核心类
     *      org.apache.commons.codec.binary.Base64
     * 核心方法
     *      encodeToString 编码
     *      decode 解码
     */
    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编码和解码  ***********
     * 核心类
     *      org.apache.commons.codec.binary.Hex
     * 核心方法
     *      encodeHexString, encodeHex 编码
     *      decodeHex 解码
     */
    String str_1 = "test";
    /**编码*/
    String hexString = Hex.encodeHexString(str_1.getBytes("UTF-8"));//直接一步到位
    System.out.println(hexString);
    char[] encodeHex = Hex.encodeHex(str_1.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加密  ***********
     * 核心类
     *      org.apache.commons.codec.digest.DigestUtils
     * 核心方法
     *      md5Hex 编码
     */
    String str_2 = "test";
    String md5 = DigestUtils.md5Hex(str_2.getBytes("UTF-8"));
    System.out.println(md5);
    
    /**
     * *********** SHA加密  ***********
     * 核心类
     *      org.apache.commons.codec.digest.DigestUtils
     * 核心方法
     *      sha1Hex 编码
     */
    String str_3 = "test中国";
    String sha1Hex = DigestUtils.sha1Hex(str_3.getBytes("UTF-8"));
    System.out.println(sha1Hex);
    
    /**
     * *********** URLCodec  ***********
     * 核心类
     *      org.apache.commons.codec.net.URLCodec
     * 核心方法
     *      encode 编码
     *      decode 解码
     */
    String url = "http://baidu.com?name=你好";
    URLCodec codec = new URLCodec();
    String encode = codec.encode(url);
    System.out.println(encode);
    String decodes = codec.decode(encode);
    System.out.println(decodes);
  • 相关阅读:
    winform登录成功后打开主窗体的合理写法
    C#将字符串转换为整型的三种方法的总结
    win7下安装Oracle11g
    无法将文件“.exe”复制到“.exe”。文件“.exe”正由另一进程使用,因此该进程无法访问该文件
    GDI+
    “System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常
    winform中为ComboBox控件添加“请选择”或“全部”选项
    IE8不能上传、插入图片的解决办法!
    asp.net mvc实现上传文件
    winform关闭窗体时确认框提示两次
  • 原文地址:https://www.cnblogs.com/myitnews/p/12287044.html
Copyright © 2011-2022 走看看