commons-codec中[md5,sha,base64加密算法]的实现demo

项目用到给用户密码加密,下载了apache的commons-codec jar包,贴出对几种加密算法实现的demo。记之。

commons-codec-1.10下载链接:

http://commons.apache.org/proper/commons-codec/download_codec.cgi

package demo;

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;
import org.apache.commons.codec.language.Metaphone;
import org.apache.commons.codec.language.RefinedSoundex;
import org.apache.commons.codec.language.Soundex;

public class CodecDemo {
	public static void main(String[] args) throws DecoderException {

		String strPsw = "123456"; // 原密码
		String ecPsw = ""; // 加密密码
		String dcPsw = ""; // 解密密码

		// md5:消息摘要算法第五(Message Digest Algorithm)
		System.out.println("MD5:");

		ecPsw = DigestUtils.md5Hex(strPsw);

		System.out.println("Original:" + strPsw);
		System.out.println("MD5:" + ecPsw + "\n");

		// SHA1:安全哈希算法(Secure Hash Algorithm)
		System.out.println("SHA1:");

		ecPsw = DigestUtils.sha1Hex(strPsw);

		System.out.println("Original:" + strPsw);
		System.out.println("SHA1:" + ecPsw + "\n");

		// BASE64算法:网络上最常见的用于传输8Bit字节代码的编码方式之一
		System.out.println("Base64:");

		byte[] ec = null;
		byte[] dc = null;
		// 加密
		ec = Base64.encodeBase64(strPsw.getBytes(), true);
		ecPsw = new String(ec).replaceAll("\r|\n", ""); // str.replaceAll("\r|\n",
														// "") 去掉str末尾换行
		// 解密:(base64Psw=new String(ec)为要解密的字符串)
		dc = Base64.decodeBase64(ecPsw.getBytes());
		dcPsw = new String(dc).replaceAll("\r|\n", "");

		System.out.println("Original:" + strPsw);
		System.out.println("Base64:" + ecPsw);
		System.out.println("deBase64:" + dcPsw + "\n");

		// Hex编解码
		System.out.println("Hex:");

		char[] cc = null;
		cc = Hex.encodeHex(strPsw.getBytes(), true);
		ecPsw = new String(cc).replace("\r|\n", "");

		dc = Hex.decodeHex(ecPsw.toCharArray());
		dcPsw = new String(dc).replaceAll("\r|\n", "");

		System.out.println("Original:" + strPsw);
		System.out.println("Hex:" + ecPsw);
		System.out.println("deHex:" + dcPsw + "\n");

		// Metaphone及 Soundex编码
		// Metaphone 建立出相同的key给发音相似的单字, 比 Soundex 还要准确, 但是 Metaphone 没有固定长度,
		// Soundex 则是固定第一个英文字加上3个数字. 这通常是用在类似音比对, 也可以用在 MP3 的软件开发.
		System.out.println("Metaphone or Soundex:");

		Metaphone metaphone = new Metaphone();
		RefinedSoundex refinedSoundex = new RefinedSoundex();
		Soundex soundex = new Soundex();
		for (int i = 0; i < 2; i++) {
			String str = (i == 0) ? "resume" : "resin";
			String mString = null;
			String rString = null;
			String sString = null;
			try {
				mString = metaphone.encode(str);
				rString = refinedSoundex.encode(str);
				sString = soundex.encode(str);
			} catch (Exception ex) {
				;
			}
			System.out.println("Original:" + str);
			System.out.println("Metaphone:" + mString);
			System.out.println("RefinedSoundex:" + rString);
			System.out.println("Soundex:" + sString + "\n");
		}
	}

}

参考文章:

http://www.oschina.net/question/12_4981?fromerr=ZTPdDmBt


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值