使用commons-codec 进行加密的一些操作

package com.jiepu.ApacheDigest;

import java.io.FileInputStream;

import org.apache.commons.codec.CharEncoding;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.codec.digest.Crypt;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.codec.digest.Sha2Crypt;
import org.apache.commons.codec.digest.UnixCrypt;
import org.apache.commons.codec.net.BCodec;
import org.apache.commons.codec.net.QCodec;
import org.apache.commons.codec.net.URLCodec; /**
*
* apache commons-codec 与Apache Digest demo实例,支持md5 sha1 base64 hmac urlencode
*
maven 依赖:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
*/
public class App {
public static void main(String[] args) throws Exception {
String str = "你好";
String key="key";
URLCodec urlCodec = new URLCodec();
String code = urlCodec.encode(str, CharEncoding.UTF_8);
System.out.println(code);
String data = urlCodec.decode(code, CharEncoding.UTF_8);
System.out.println(data); QCodec qcodec = new QCodec();
code = qcodec.encode(str, CharEncoding.UTF_8);
System.out.println(code);
data = qcodec.decode(code);
System.out.println(data); BCodec bcodec = new BCodec();
code = bcodec.encode(str, CharEncoding.UTF_8);
System.out.println(code);
data = bcodec.decode(code);
System.out.println(data); System.out.println(DigestUtils.md5Hex(str));
System.out.println(DigestUtils.md2Hex(str));
System.out.println(DigestUtils.sha1Hex(str));
System.out.println(DigestUtils.sha256Hex(str));
System.out.println(DigestUtils.sha384Hex(str));
System.out.println(DigestUtils.sha512Hex(str));
System.out.println(DigestUtils.shaHex(str)); System.out.println(Sha2Crypt.sha512Crypt(str.getBytes()));
System.out.println(Sha2Crypt.sha512Crypt(str.getBytes()));
System.out.println(Md5Crypt.md5Crypt(str.getBytes()));
System.out.println(UnixCrypt.crypt(str.getBytes())); System.out.println(HmacUtils.hmacMd5Hex(key, str));
System.out.println(HmacUtils.hmacSha1Hex(key, str));
System.out.println(HmacUtils.hmacSha256Hex(key, str));
System.out.println(HmacUtils.hmacSha384Hex(key, str));
System.out.println(HmacUtils.hmacSha512Hex(key, str)); System.out.println( Md5Crypt.apr1Crypt(str));
System.out.println(Crypt.crypt(str)); code=Base64.encodeBase64String(str.getBytes());
System.out.println(Base64.isBase64(code));
System.out.println(StringUtils.newStringUtf8(Base64.decodeBase64(code))); char[] chs=Hex.encodeHex(str.getBytes());
System.out.println(chs);
System.out.println(StringUtils.newStringUtf8(Hex.decodeHex(chs)));
System.out.println("Hello World!"); System.out.println(DigestUtils.md5Hex(new FileInputStream("c:/gc.log")));
System.out.println(DigestUtils.sha1Hex(new FileInputStream("c:/gc.log")));
//DigestUtils.getDigest("md5").digest(input);
}
}

@文转 http://blog.csdn.net/earbao/article/details/42552285

【java】之 apache commons-codec 与Apache Digest demo实例,支持md5 sha1 base64 hmac urlencode的更多相关文章

  1. Java之加密(信息摘要)工具类(依赖:java.security.MessageDigest或org.apache.commons.codec.digest.DigestUtils)

    依赖于java.security.MessageDigest,支持MD5,SHA-1,SHA-256 import java.security.MessageDigest; import java.s ...

  2. Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.codec.digest.DigestUtils.sha1Hex(Ljava/lang/String;)Ljava/lang/String;

    异常:Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.co ...

  3. java 调用apache.commons.codec的包简单实现MD5加密

    转自:https://blog.csdn.net/mmd1234520/article/details/70210002/ import java.security.MessageDigest; im ...

  4. md5加密(3)---org.apache.commons.codec.digest.DigestUtils.md5Hex(input)

    import org.apache.commons.codec.digest.DigestUtils;String sig = DigestUtils.md5Hex("str")

  5. 【报错】引入jar包import org.apache.commons.codec.digest.DigestUtils 报错,jar不存在

    import org.apache.commons.codec.digest.DigestUtils报错.缺少jar maven引用jar包地址: <!-- https://mvnreposit ...

  6. ANDROID : java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String in android

    Andriod系统包中现在已经自带加密函数,如果用apache的codec包则会报以上错误,用android.util.Base64以下方法代替org.apache.commons.codec.bin ...

  7. Apache Commons Codec 编码解码

    Apache Commons Codec jar包官方下载地址 下载解压后把commons-codec-1.9.jar 放到lib中 关于SHA1算法的介绍可以参看Wiki:http://en.wik ...

  8. Apache Commons Codec 与消息摘要算法(hash算法)

    首先我们要明白 Codec 是什么含义.它是 Coder + decoder = Codec,也就是编码器解码器.即是编码器,也是解码器. 官网地址:http://commons.apache.org ...

  9. Apache Commons Codec的Base64加解密库

    下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi import org.apache.commons.cod ...

随机推荐

  1. 胡说REST(REpresentational State Transfer)

    Roy T. Fielding的2000年在他的博士论文中提出REpresentational State Transfer这一软件架构风格,相比"表述性状态转移"等等类似的拗口的 ...

  2. Play jQuery with Node.js

    Motivation Move the fucking browser interactions out of javascript development cycle, since Chrome i ...

  3. angular使用echarts折线图

    echarts是开源的画图工具,在angular框架中引入echarts不能直接使用.需要新建一个directive //echarts基本参数 app.factory('$echartsConfig ...

  4. Android驱动开发前的准备(二)

    搭建android开发环境 2.1 Android底层开发需要哪些工具 2.2 安装 JDK 2.3 搭建Android 应用程序开发环境 2.4安装Android NDK开发环境 2.5安装交叉编译 ...

  5. Javascript.//DOM

    文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.Document Object Model的历史可以追溯至1990年代后期微 ...

  6. 自己瞎捣腾的Win7下Linux安装之路-----理论篇

    接着上回说道,我把双系统做好啦,开心.... 之后我就在想几个问题: 1.在Ubuntu装好后,重启电脑却还是win7,等我用EasyBCD之后,才可选择使用装好的Ubuntu呢? 2.在用EasyB ...

  7. Advanced SystemCare 系统优化软件

    这是一款国外非常流行的系统优化软件,功能很全面,而且中文版用起来也没有语言障碍. Pro版: C0184-8F31F-4B337-296F7 8DE4A-352D9-B4531-D60F7 5B91B ...

  8. 简易c语言文法

    <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...

  9. Git 基础

    取得项目的 Git 仓库 有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库.第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来. 在工作目录中初 ...

  10. 用js判断页面刷新或关闭的方法

    Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定.区别在于on ...