Base64和Md加解密
2016-06-18 13:55
357 查看
Base64加密算法
/** * Created by Jay-Tang on 2016/6/18. */ import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; import java.io.IOException; public class Base64 { public static String src = "shuaige"; public static void main(String[] args) throws IOException { jiami(); ccjiami(); bcjiami(); } public static void jiami() throws IOException { BASE64Encoder encoder = new BASE64Encoder(); String strdecoder = encoder.encode(src.getBytes()); System.out.println("加密:" + strdecoder); BASE64Decoder decoder = new BASE64Decoder(); byte[] strdecoder2 = decoder.decodeBuffer(strdecoder); //字节数组转字符串 String str = new String(strdecoder2); System.out.print("解密:" + str); } //用cc加密base64的算法 public static void ccjiami() { byte[] encodeBase64 = org.apache.commons.codec.binary.Base64.encodeBase64(src.getBytes()); System.out.print(new String(encodeBase64)); byte[] decodeBase64=org.apache.commons.codec.binary.Base64.decodeBase64(encodeBase64); System.out.print(new String(decodeBase64)); } //用bc的base64加密算法 public static void bcjiami(){ byte[] encodeBase64 = org.bouncycastle.util.encoders.Base64.encode(src.getBytes()); System.out.print(new String(encodeBase64)); byte[] decodeBase64=org.bouncycastle.util.encoders.Base64.decode(encodeBase64); System.out.print(new String(decodeBase64)); } }
Md消息摘要算法
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.digests.MD4Digest; import org.bouncycastle.crypto.digests.MD5Digest; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * Created by Jay-Tang on 2016/6/18. */ public class Md { public static String src = "shuaige"; public static void main(String args[]) throws NoSuchAlgorithmException { jiamimd5(); jiamimd2(); bcMD4(); bcMD5(); ccMD5(); ccMD2(); } public static void jiamimd5() throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD5"); //是16位的字节数组 byte[] toChapter1Digest = md.digest(src.getBytes()); System.out.println(Hex.encodeHex(toChapter1Digest)); } public static void jiamimd2() throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD2"); //是16位的字节数组 byte[] toChapter1Digest = md.digest(src.getBytes()); System.out.println(Hex.encodeHex(toChapter1Digest)); } //bcmd4 bc独有的md4 public static void bcMD4() { Digest digest = new MD4Digest(); //调用update方法 digest.update(src.getBytes(), 0, src.getBytes().length); byte[] md4Bytes = new byte[digest.getDigestSize()]; digest.doFinal(md4Bytes, 0); System.out.println(org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes)); } //bc的md值 public static void bcMD5() { Digest digest = new MD5Digest(); //调用update方法 digest.update(src.getBytes(), 0, src.getBytes().length); byte[] md4Bytes = new byte[digest.getDigestSize()]; digest.doFinal(md4Bytes, 0); System.out.println(org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes)); } //cc的md5 public static void ccMD5() { String str = DigestUtils.md5Hex(src.getBytes()); System.out.println(str); } //cc的md2 简化了jdk里面md2里面的操作 public static void ccMD2() { String str = DigestUtils.md2Hex(src.getBytes()); System.out.println(str); } }
相关文章推荐
- MongoDB复制集自适应oplog管理
- Windows7下安装搭建redis教程和配置详解
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- ios开发APP必须要了解的基本配置
- APP数据分析的常用指标
- leetcode题解日练--2016.6.16
- ThinkPHP框架基础知识二
- php如何实现文件下载
- java apache commons HttpClient发送get和post请求的学习整理
- Android中Activity跳转过程中的反向传值
- Git II: 操作远程Repository基础
- 一个省市县三级联动的回显(带数据库)
- Android高级_视频播放控件
- 编译安装httpd
- 第十六周项目 电子词典
- Leetcode题解 226. Invert Binary Tree
- Spring各jar包的作用
- java中调用matlab的函数
- thymeleaf 内联语法
- 软件测试必须要知道十个关键点