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);
  • 相关阅读:
    常用的输出方法
    使用Action()和ActionLink()生成链接
    "??"运算符
    使用路由数据
    路由匹配总结
    routes.MapRoute()定义路由的要求
    控制器和视图数据传递小结
    跨请求数据传递TempData
    Redis安装创建
    JAVA获取当前时间加一天
  • 原文地址:https://www.cnblogs.com/myitnews/p/12287044.html
Copyright © 2011-2022 走看看