160705、总结: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包
160705、总结:commons-codec.jar中常用方法的更多相关文章
- Apache Commons Codec 编码解码
Apache Commons Codec jar包官方下载地址 下载解压后把commons-codec-1.9.jar 放到lib中 关于SHA1算法的介绍可以参看Wiki:http://en.wik ...
- 【报错】引入jar包import org.apache.commons.codec.digest.DigestUtils 报错,jar不存在
import org.apache.commons.codec.digest.DigestUtils报错.缺少jar maven引用jar包地址: <!-- https://mvnreposit ...
- 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 ...
- java 调用apache.commons.codec的包简单实现MD5加密
转自:https://blog.csdn.net/mmd1234520/article/details/70210002/ import java.security.MessageDigest; im ...
- ANDROID : java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String in android
Andriod系统包中现在已经自带加密函数,如果用apache的codec包则会报以上错误,用android.util.Base64以下方法代替org.apache.commons.codec.bin ...
- Commons Codec基本使用(转载)
在实际的应用中,我们经常需要对字符串进行编解码,Apache Commons家族中的Commons Codec就提供了一些公共的编解码实现,比如Base64, Hex, MD5,Phonetic an ...
- commons-lang3-3.2.jar中的常用工具类的使用
这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...
- Apache Commons Codec 与消息摘要算法(hash算法)
首先我们要明白 Codec 是什么含义.它是 Coder + decoder = Codec,也就是编码器解码器.即是编码器,也是解码器. 官网地址:http://commons.apache.org ...
- org.apache.commons.lang.StringUtils中常用的方法
org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...
随机推荐
- 百度地图和高德地图坐标系的互相转换 四种Sandcastle方法生成c#.net帮助类帮助文档 文档API生成神器SandCastle使用心得 ASP.NET Core
百度地图和高德地图坐标系的互相转换 GPS.谷歌.百度.高德坐标相互转换 一.在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系: 1.WGS-84原始坐标系,一般用国际GPS纪录仪记 ...
- int.TryParse非预期执行引发的思考
问题出现 这天在写一个页面,想谨慎些就用了int.TryParse,结果出问题了. 代码如下: int id = 1000; //Request.QueryString["id"] ...
- c#中abstract、override、new、virtual、sealed使用
abstract 修饰类名为抽象类,修饰方法为抽象方法.如果一个类为抽象类,则这个类智能是其他某个类的基类.抽象方法在抽象类中没有函数体.抽象类中的抽象方法是没有方法体的,继承其的子类必须实现 ...
- FreeSWITCH小结:关于sip的UDP、TCP与MTU
1.关于SIP的UDP与MTU的关系 如果sip消息的大小超过了MTU,则有可能被网络中的某一节点分片,而UDP处理分片会有很大的问题,从而导致sip消息传输失败.要解决该问题的话,两种方案: 1)减 ...
- WebView中input file的解决方法
public class MyWb extends Activity { /** Called when the activity is first created. */ WebView web; ...
- 怎么解决ORACLE 中 CHAR类型的索引问题
在很多场景中,都有如下情况 trim(a.colunm1) = trim(b.colunm2) 应该怎么优化呢? 用到 TRIM 的很多原因是某些系统为了提高查询效率,不使用 ORACLE 的特有的 ...
- 项目红色感叹号eclipse因Web App Libraries中的jar包missing导致项目红色感叹号
症状: 如题 分析: 修改.更换或者删除了WEB-INF/lib中的jar包 解决方案: 右击项目>build path>Libraries 直接remove Web App Librar ...
- Tomcat nginx log日志按天分割切割
利用 Linux 自带的 logrotate 工具来实现按天切割日志.下方已 centos 7 系统为例来实践讲解. 原理 Logrotate是基于CRON来运行的,其脚本是/etc/cron.dai ...
- 第一百五十二节,封装库--JavaScript,表单验证--年月日注入
封装库--JavaScript,表单验证--年月日注入 效果图 html <div id="reg"> <h2 class="tuo"> ...
- 从客户端检测到有潜在危险的Request.Form 值”错误提示
http://www.cnblogs.com/UouHt/archive/2008/10/30/1322697.html asp.net开发中,经常遇到“从客户端检测到有潜在危险的Request.Fo ...