使用Commons-codec包加密
来源:互联网 发布:mac用itunes制作铃声 编辑:程序博客网 时间:2024/04/28 07:50
一、关于加密算法的一些分类。
1:消息摘要:(数字指纹):既对一个任意长度的一个数据块进行计算,产生一个唯一指纹。MD5/SHA1
发送给其他人你的信息和摘要,其他人用相同的加密方法得到摘要,最后进行比较摘要是否相同。
SHA(Secure Hash Algorithm,安全散列算法)
DES(Data Encryption Standard)是发明最早的最广泛使用的分组对称加密算法。DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密
3:数字签名:就是信息发送者用其私钥对从所传报文中提取出的特征数据(或称数字指纹)进行RSA算法操作,以保证发信人无法抵赖曾发过该信息(即不可抵赖性),同时也确保信息报文在经签名后末被篡改(即完整性)。当信息接收者收到报文后,就可以用发送者的公钥对数字签名进行验证。
代表:DSA
4:非对称密匙密码体制(公匙体系):加密密匙不同于解密密匙,加密密匙公之于众,谁都可以使用,解密密匙只有解密人自己知道。代表:RSA
二、使用Commons-codec加密
Apache Commons家族中的Commons Codec就提供了一些公共的编解码实现,比如Base64, Hex, MD5,Phonetic and URLs等等
下面内容是之前从网上搜的,忘了在哪里找的了:
1、 Base64编解码
private static String encodeTest(String str){<span style="white-space:pre"></span>Base64 base64 = new Base64();<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>str = base64.encodeToString(str.getBytes(“UTF-8″));<span style="white-space:pre"></span>} catch (UnsupportedEncodingException e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>System.out.println(“Base64 编码后:”+str);<span style="white-space:pre"></span>return str;} private static void decodeTest(String str){<span style="white-space:pre"></span>Base64 base64 = new Base64();<span style="white-space:pre"></span>//str = Arrays.toString(Base64.decodeBase64(str));<span style="white-space:pre"></span>str = new String(Base64.decodeBase64(str));<span style="white-space:pre"></span>System.out.println(“Base64 解码后:”+str);}
2、 Hex编解码
private static String encodeHexTest(String str){
try {
str = Hex.encodeHexString(str.getBytes(“UTF-8″));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(“Hex 编码后:”+str);
return str;
}
private static String decodeHexTest(String str){
Hex hex = new Hex();
try {
str = new String((byte[])hex.decode(str));
} catch (DecoderException e) {
e.printStackTrace();
}
System.out.println(“Hex 编码后:”+str);
return str;
}
3、 MD5加密
private static String MD5Test(String str){
try {
System.out.println(“MD5 编码后:”+new String(DigestUtils.md5Hex(str.getBytes(“UTF-8″))));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
4、 SHA编码
private static String ShaTest(String str){
try {
System.out.println(“SHA 编码后:”+new String(DigestUtils.shaHex(str.getBytes(“UTF-8″))));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
5、 Metaphone和Soundex
这个例子来源于网上,网址见:
http://350129923.blog.163.com/blog/static/17959113200763144659125/
Metaphone 建立出相同的key给发音相似的单字, 比 Soundex 还要准确, 但是 Metaphone 没有固定长度, Soundex 则是固定第一个英文字加上3个数字. 这通常是用在类似音比对, 也可以用在 MP3 的软件开发.
import org.apache.commons.codec.language.*;
import org.apache.commons.codec.*;
public class LanguageTest {
public static void main(String args[]) {
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”);
}
}
}
- 使用Commons-codec包加密
- 使用commons-codec包加密字符串(MD5,SHA1,BASE64)
- 使用commons-codec包加密字符串(MD5,SHA1,BASE64)
- 使用commons-codec包加密字符串(MD5,SHA1,BASE64)
- commons-codec包简介
- commons-codec包简介
- 使用org.apache.commons.codec做md5&SHA1&BASE64加密
- Java使用commons codec进行Md5,sha1,base64加密
- Apache组件commons的Codec包的使用实例
- commons-codec使用介绍
- 【JavaWeb】commons-codec包简介
- java 调用apache.commons.codec的包简单实现MD5加密
- apaceh commons-codec 加密工具类 DigestUtils
- commons-codec 加密(MD5 SHA1 BASE64)
- commons-codec-1.8.jar的加密
- Apache Commons Codec 使用小结
- 【android】android中使用commons-codec框架处理各种加密解密
- 使用commons-codec-1.10.jar的Base64实现字符串的加密解密
- Facebook的CIO都做些什么?
- Failed to start monitoring
- 20150301题解
- 一台机器上配置多个JBOSS
- iOS静态库的编译和lipo命令的使用
- 使用Commons-codec包加密
- 娱乐之 ——用C语言打印 helloword 的 N 中做法(待续)
- 手机内存的理解
- 高质量c/c++读书笔记
- java将字符串逆序递归方式输出
- 【Garry逆袭校招】之 计算机网络 知识点学习--Android地基系列(二)
- java基础for循环一个细节
- CSS Combinator
- Android Studio系列教程六--Gradle多渠道打包