当前位置:编程学习 > 网站相关 >>

Base64以及Md5的使用

利用md5,和base64对java应用中的敏感数据进行的加密和编码。
1. md5和base64在维基百科中的定义:
   MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致。 计算机广泛使用的杂凑算法之一(又译摘要算法、哈希算法),主流编程语言普遍已有MD5实现。将数据(如汉字)运算为另一固定长度值,是杂凑算法的基础原理,MD5的前身有MD2、MD3和MD4。md5 运算结果是一个固定长度为128位的二进制数,经过一系列的运算得到32个16进制数。
   Base64是一种使用64基的位置计数法。它使用2的最大次方来代表仅可打印的ASCII 字符。这使它可用来作为电子邮件的传输编码。在Base64中的变量使用字符A-Z、a-z和0-9 ,这样共有62个字符,用来作为开始的64个数字,最后两个用来作为数字的符号在不同的系统中而不同。一些如uuencode的其他编码方法,和之后binhex的版本使用不同的64字符集来代表6个二进制数字,但是它们不叫Base64。base64算法在维基百科里面的例子讲的很好很详细。
 
   link:   md5   http://zh.wikipedia.org/wiki/MD5
        base64   http://zh.wikipedia.org/wiki/Base64
2. 下面我将用代码的形式给出如何使用base64和md5算法(如果有其他的方法或者比较好的使用方式,期望同胞们不吝赐教。因为我还没有实际工作过,先谢谢了。)
Java代码 
     
package com.piedra.base64;  
import java.io.IOException;  
 
import sun.misc.*;  
/** 
 * 通过这个类实现利用base64算法进行编码和解码。 
 * @author  
 * 
 */ 
public class Base64 {  
      
    @SuppressWarnings("restriction")  
    public String encode(String toEncodeContent){  
        if(toEncodeContent == null){  
            return null;  
        }  
        BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(toEncodeContent.getBytes());  
    }  
      
    public String encode(byte [] toEncodeContent){  
        return encode(new String(toEncodeContent));  
    }  
      
    @SuppressWarnings("restriction")  
    public String decode(String toDecodeContent){  
        if(toDecodeContent == null) {  
            return null;  
        }  
        byte[] buf = null;  
        try {  
            buf = new BASE64Decoder().decodeBuffer(toDecodeContent);  
        } catch(IOException e){  
            e.printStackTrace();  
        } finally {  
        }  
        return new String(buf);  
    }  

 
  
package com.piedra.base64;
import java.io.IOException;
 
import sun.misc.*;
/**
 * 通过这个类实现利用base64算法进行编码和解码。
 * @author
 *
 */
public class Base64 {
        
         @SuppressWarnings("restriction")
         public String encode(String toEncodeContent){
                   if(toEncodeContent == null){
                            return null;
                   }
                   BASE64Encoder encoder = new BASE64Encoder();
                   return encoder.encode(toEncodeContent.getBytes());
         }
        
         public String encode(byte [] toEncodeContent){
                   return encode(new String(toEncodeContent));
         }
        
         @SuppressWarnings("restriction")
         public String decode(String toDecodeContent){
                   if(toDecodeContent == null) {
                            return null;
                   }
                   byte[] buf = null;
                   try {
                            buf = new BASE64Decoder().decodeBuffer(toDecodeContent);
                   } catch(IOException e){
                            e.printStackTrace();
                   } finally {
                   }
                   return new String(buf);
         }
}
下面是测试代码:
Java代码 
package com.piedra.base64;  
 
import static org.junit.Assert.*;  
 
import org.junit.After;  
import org.junit.Before;  
import org.junit.Test;  
 
public class Base64Test {  
    private Base64 base64;  
      
    @Before 
 &nbs
补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,