commons-codec.jar中常用方法

一、Base64编码和解码
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.binary.Base64;
public class TestBase64 {
    public static void main(String[] args) throws EncoderException, UnsupportedEncodingException {
        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编码和解码
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
public class TestHex {
    public static void main(String[] args) throws DecoderException, UnsupportedEncodingException {
        String str = "test";
        /**编码*/
        String hexString = Hex.encodeHexString(str.getBytes("UTF-8"));//直接一步到位
        System.out.println(hexString);
        char[] encodeHex = Hex.encodeHex(str.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加密(MD5是不可逆算法,只能加密)
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.digest.DigestUtils;
public class TestMD5 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "test";
        String md5 = DigestUtils.md5Hex(str.getBytes("UTF-8"));
        System.out.println(md5);
    }
}
四、SHA加密
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.digest.DigestUtils;
public class TestSHA {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "test中国";
        String sha1Hex = DigestUtils.sha1Hex(str.getBytes("UTF-8"));
        System.out.println(sha1Hex);
    }
}
五、Metaphone和Soundex
Metaphone 建立出相同的key给发音相似的单字, 比 Soundex 还要准确, 但是 Metaphone 没有固定长度, Soundex 则是固定第一个英文字加上3个数字. 这通常是用在类似音比对, 也可以用在 MP3 的软件开发
metaphone() 比 soundex() 函数更精确,因为 metaphone() 了解基本的英语发音规则

import org.apache.commons.codec.language.Metaphone;
import org.apache.commons.codec.language.RefinedSoundex;
import org.apache.commons.codec.language.Soundex;
public class TestMetaphoneAndSoundex {
    public static void main(String[] args) {
        String str = "testgggggg";
        /**Metaphone没有固定长度*/
        Metaphone metaphone = new Metaphone();
        String metaphoneEncode = metaphone.encode(str);
        System.out.println(metaphoneEncode);
        /**RefinedSoundex*/
        RefinedSoundex refinedSoundex = new RefinedSoundex();
        String refinedSoundexEncode = refinedSoundex.encode(str);
        System.out.println(refinedSoundexEncode);
        /**Soundex固定第一个英文字加上3个数字*/
        Soundex soundex = new Soundex();
        String soundexEncode = soundex.encode(str);
        System.out.println(soundexEncode);
    }
}
六、URLCodec
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;
public class TestURLCodec {
    public static void main(String[] args) throws EncoderException, DecoderException {
        String url = "http://baidu.com?name=你好";
        URLCodec codec = new URLCodec();
        String encode = codec.encode(url);
        System.out.println(encode);
        String decode = codec.decode(encode);
        System.out.println(decode);
    }
}
除了这些还有很多算法比如HMAC等,大家可以根据需要选取,commons-codec-1.10.jar就是一个加密解码相关的jar包
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.tydic.common.utils; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; /* * AES加解密算法 * * @author jueyue * 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定 此处使用AES-128-CBC加密模式,key需要为16位。 也是使用0102030405060708 */ public class AES { // 加密 public static String Encrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位 if (sKey.length() != 16) { System.out.print("Key长度不是16位"); return null; } byte[] raw = sKey.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式" IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(sSrc.getBytes()); return Base64.encodeBase64String(encrypted);//此处使用BAES64做转码功能,同时能起到2次加密的作用。 } // 解密 public static String Decrypt(String sSrc, String sKey) throws Exception { try { // 判断Key是否正确 if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位 if (sKey.length() != 16) { System.out.print("Key长度不是16位"); return null; } byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher ciphe
注:下文的 *** 代表文件名的组件名称。 # 包含: 文-英文对照文档:【***-javadoc-API文档-文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【***.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【***-sources.jar下载地址(官方地址+国内镜像地址).txt】 # 本文件关键字: 文-英文对照文档,英对照文档,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【***.jar文文档.zip】,再解压其的 【***-javadoc-API文档-文(简体)版.zip】,双击 【index.html】 文件,即可用浏览器打开、进行查看。 # 特殊说明: ·本文档为人性化翻译,精心制作,请放心使用。 ·本文档为双语同时展示,一行原文、一行译文,可逐行对照,避免了原文/译文来回切换的麻烦; ·有原文可参照,不再担心翻译偏差误导; ·边学技术、边学英语。 ·只翻译了该翻译的内容,如:注释、说明、描述、用法讲解 等; ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件不会散落一地); (2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件;

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值