MD5和Base64

一. 简述

MD5: 全称为message digest algorithm 5(信息摘要算法), 可以进行加密, 但是不能解密, 属于单向加密, 通常用于文件校验

Base64: 把任意序列的8为字节描述为一种不易为人识别的形式, 通常用于邮件、http加密. 登陆的用户名和密码字段通过它加密, 可以进行加密和解密.


二. 代码

1. MD5: 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class MD5Utils {  
  2.     /** 
  3.      * 使用md5的算法进行加密 
  4.      * @param plainText 加密明文 
  5.      * @return 加密密文 
  6.      */  
  7.     public static String getDigest(String plainText) {  
  8.         byte[] secretBytes = null;  
  9.         try {  
  10.             secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());  
  11.         } catch (NoSuchAlgorithmException e) {  
  12.             throw new RuntimeException("error happens", e);  
  13.         }  
  14.         return new BigInteger(1, secretBytes).toString(16);  
  15.     }  
  16. }  

2. Base64: 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Base64Util {  
  2.     /** 
  3.      * 使用Base64进行编码 
  4.      * @param encodeContent 需要编码的内容 
  5.      * @return 编码后的内容 
  6.      */  
  7.     public static String encode(String encodeContent) {   
  8.         if (encodeContent == null) {  
  9.             return null;  
  10.         }  
  11.         BASE64Encoder encoder = new BASE64Encoder();   
  12.         return encoder.encode(encodeContent.getBytes());  
  13.     }  
  14.   
  15.     /** 
  16.      * 使用Base64进行编码 
  17.      * @param encodeContent 需要编码的内容 
  18.      * @return 编码后的内容 
  19.      */  
  20.     public static String encode(byte[] encodeText) {   
  21.         return encode(new String(encodeText));  
  22.     }  
  23.       
  24.     /** 
  25.      * 使用Base64进行解码 
  26.      * @param encodeContent 需要解码的内容 
  27.      * @return 解码后的内容 
  28.      */  
  29.     public static String decode(String decodeContent) {   
  30.         byte[] bytes = null;   
  31.         if (decodeContent == null) {  
  32.             return null;  
  33.         }  
  34.         try {  
  35.             bytes = new BASE64Decoder().decodeBuffer(decodeContent);  
  36.         } catch (IOException e) {  
  37.             throw new RuntimeException("error happens", e);  
  38.         } finally {  
  39.         }  
  40.         return new String(bytes);  
  41.     }  
  42. }  


3. 测试代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Test {  
  2.     /** 
  3.      * 先使用MD5算法加密, 再使用base64算法进行编码 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.          String plainText = "pwd";    
  8.          String encodedPassword = MD5Utils.getDigest(Base64Util.encode(plainText));   
  9.          System.out.println(encodedPassword);  
  10.     }  
  11. }  
为什么使用MD5加密后还要使用Base64编码呢? 用Base64算法编码后得到的是32位长度的字符串, 这样有利于在数据库中进行存储. 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值