Apache commons 之 codec 简介和入门代码

Java JDK有一个 java.security 的 package, 提供了 MessageDigest 的编码方式, Digest Algorithms 包括了 MD2, MD5, SHA-1, SHA-256, SHA-384, 及 SHA-512 等等;

Codec 是Commons项目中用来处理常用的编码方法的工具类包,例如HEX、SHA1、MD5、Base64等等目前最新版本为1.4。

Base64/HEX是对称加密,MD5是不可逆加密,千万要注意

一些简单的加密示例代码:

package test.ffm83.commons.codec;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.DecoderException;

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

import org.apache.commons.codec.binary.Hex;

import org.apache.commons.codec.digest.DigestUtils;

/**

* 通过commons
codec 进行一些简单的加密解密等

* @author范芳铭

*/

public
class
CodecUsage {

public
static void
main(String[] args){

Stringstr = "FanFangming";

Stringbase64 = encodeBaseTest(str);

decodeBaseTest(base64);

Stringhex = encodeHexTest(str);

decodeHexTest(hex);

MD5Test(str);

ShaTest(str);

}

// Base64编解码

private
static
StringencodeBaseTest(String str) {

Base64base64 = newBase64();

try {

str= base64.encodeToString(str.getBytes("UTF-8"));

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}

System.out.println("Base64
编码后:" + str);

return str;

}

private
static void
decodeBaseTest(Stringstr) {

str= newString(Base64.decodeBase64(str));

System.out.println("Base64
解码后:" + str);

}

// Hex编解码

private
static
StringencodeHexTest(String str) {

try {

str= Hex.encodeHexString(str.getBytes("UTF-8"));

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}

System.out.println("Hex
编码后:" + str);

return str;

}

private
static
StringdecodeHexTest(String str) {

Hexhex = newHex();

try {

str= newString((byte[])hex.decode(str));

}catch(DecoderException e) {

e.printStackTrace();

}

System.out.println("Hex
编码后:" + str);

return str;

}

// MD5加密

private
static
String MD5Test(Stringstr) {

try {

str= newString(DigestUtils.md5Hex(str.getBytes("UTF-8")));

System.out.println("MD5
编码后:" + str);

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}

return str;

}

// SHA编码

private
static
String ShaTest(Stringstr) {

try {

str= newString(DigestUtils.sha256(str.getBytes("UTF-8")));

System.out.println("SHA
编码后:"+ str);

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}

return str;

}

}

运行结果如下:

Base64 编码后:RmFuRmFuZ21pbmc=

Base64 解码后:FanFangming

Hex 编码后:46616e46616e676d696e67

Hex 编码后:FanFangming

MD5 编码后:922b65d5455be7d9a4875b6b1bde638d

SHA 编码后:31fcbfd641e54747d20288f883458664a05788a9

时间: 2024-07-30 18:27:44

Apache commons 之 codec 简介和入门代码的相关文章

Apache commons 之 codec 深入学习以及URLCodec

我们在入门代码里,写了一些示例,感觉貌似这个类,就只有这么些内容,打开javadoc再看看,发现这个包里货还真不少. Commons Codec 1.4 API Packages org.apache.commons.codec A small set of interfaces used by the various implementations in the sub-packages. org.apache.commons.codec.binary Base64, Binary, and

Apache Commons Configuration之一简介

1    简介 Commons Configuration软件类库提供通用配置接口,使Java应用程序从多种源读取配置文件.Commons Configuration提供简单类型访问和通过以下代码演示的多义配置参数: Double double = config.getDouble("number"); Integer integer = config.getInteger("number"); 配置参数可以从以下源加载: Properties文件 XML文档 Pr

Apache Commons工具集简介

Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于JavaBean进行各种操作,克隆对象,属性等等. Betwixt XML与Java对象之间相互转换. Codec 处理常用的编码方法的工具类包 例如DES.SHA1.MD5.Base64等. Collections java集合框架操作. Compress java提供文件打包 压缩类库. Con

(转)Apache Commons工具集简介

Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. 一.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 使用示例:功能有很多,网站上有详细

apache commons Java包简介

更多信息,请参考:http://commons.apache.org/ 一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI说明:这是一个处理命令的工具.比如main方法输入的string[]需要解析.你可以预先定义好参数的规则,然后就可以调用CLI来解析. 三.Commons Codec说明:这个工具是用来编码和解码的,包括Base64,URL,Sound

apache commons 之 transcation 简介和文件事务系统的实现

Apache Commons Transaction 旨在提供一个轻量级.标准化.高效的 Java 事务多线程编程的工具包,实现了多级锁.事务集合和事务级文件访问. 事务处理系统对数据库使用者来说,是非常熟悉的事情:但是如果将事务处理系统从数据库转移到文件系统上,估计很多同学都要一筹莫展了.实际上,无论库/框架.语言,或者文件系统级别,对文件系统操作事务的支持一直都很薄弱. 单独看一些文件系统操作(比如文件重命名.删除等),它们是原子的,但是从目前的情况看,很少有解决办法能够形成一组综合的API

apache commons Math的简介和使用

apache commons Math是一组偏向科学计算为主的函数,主要是针对线性代数,数学分析,概率和统计等方面. 我虽然是数学专业毕业,当年也是抱着<数学分析>啃,但是好久不用,这些概念都开始生疏,写一点例子,仅作参考. packagetest.ffm83.commons.math; importorg.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.LUDec

python简介及入门代码(hello world)

在学习之前,首先了解一下python的前世今生 一.python简介与发展: python 是一种面向对象的解释性计算机程序设计语言. python由荷兰人Guido van Rossum 于1989年发明. 1991年,第一个Python编译器诞生.它是用C语言实现的,并能够调用C语言的库文件.从一出生,Python已经具有了:类,函数,异常处理,包含表和词典在内的核心数据类型,以及模块为基础的拓展系统. 1994年1月 python 1.0诞生,增加lambda,map,filter and

apache commons 之 DbUtils简介

apachecommons dbutils 是 Apache 组织提供的一个开源 JDBC 工具类库,对传统操作数据库的类进行二次封装,可以把结果集转化成List. 目前最新的版本是1.6. 官网地址为:http://commons.apache.org/proper/commons-dbutils/ DBUtils是个小巧的JDBC轻量级封装的工具包,其最核心的特性是结果集的封装,可以直接将查询出来的结果集封装成JavaBean,这就为我们做了最枯燥乏味.最容易出错的一大部分工作. 在使用DB