Base64以及Md5的使用

openkk 12年前

利用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算法(如果有其他的方法或者比较好的使用方式,期望同胞们不吝赐教。因为我还没有实际工作过,先谢谢了。)
注意:在Eclipse中需要将 windows->preferences->Java->Compiler->Errors/Warning中的 Deprecated and restricted Api下面的access rules修改为warning。这样使用sun.misc这个包下面的类就不会报错了。

     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);   }  }
</span>下面是测试代码:
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   public void init(){    base64 = new Base64();   }      @Test   public void testEncode() {    String toEncodeContent = "I am grade to learn java.";    String encodedContent = base64.encode(toEncodeContent);    //由于要测试toEncodeContent经过BASE64编码后的字符序列。因此就直接打印,没有用Assert的方法。    System.out.println(encodedContent);   }     @Test   public void testDecode() {    String toDecodeContent = "SSBhbSBncmFkZSB0byBsZWFybiBqYXZhLg==";    String decodedContent = base64.decode(toDecodeContent);    String expected = "I am grade to learn java.";    String actual = decodedContent;    assertEquals(expected,actual);   }     @After   public void destroy(){   }  }
接着来看看如何使用md5算法进行加密:
在java API中对于MessageDigest对象的用法有这样的描述:
The data is processed through it using the update methods. At any point reset
can be called to reset the digest. Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash
computation.
package com.piedra.base64;    import java.security.MessageDigest;  import java.security.NoSuchAlgorithmException;  /**   * 通过这个类我们可以利用getDigest方法对我们需要加密的数据进行加密。   * @author    *   */  public class Md5 {      /**    * 通过这个方法可以获得特定输入数据的文摘    * @param input 需要进行获取文摘的字节数组    * @return 特定数据的文摘    */   public byte[] getDigest(byte [] input){    byte [] digestedValue = null;    try {     MessageDigest md = MessageDigest.getInstance("MD5");     //下面两个方法相当于适用 md.digest(input);     md.update(input);     digestedValue = md.digest();    } catch (NoSuchAlgorithmException e) {     e.printStackTrace();    }    return digestedValue;   }  }
md5的测试代码以及base64和md5的结合使用:
package com.piedra.base64;    import org.junit.After;  import org.junit.Before;  import org.junit.Test;    public class Md5Test {   private Md5 md5;   private Base64 base64;      @Before   public void init(){    md5 = new Md5();    base64 = new Base64();   }      @Test   public void testGetDigest() {    String toDigest = "just a test.";    byte [] digestedValue = md5.getDigest(toDigest.getBytes());    System.out.println(new String(digestedValue));   }      @Test   public void testEncrypt(){    String toEncrypt = "This is my password.";    byte [] encrypted = md5.getDigest(toEncrypt.getBytes());    String encodedPassword = base64.encode(encrypted);    System.out.println(encodedPassword);   }      @After   public void destroy(){   }  }
为什么用md5算法加密后又要利用base64算法进行编码:因为md5加密后得到的数据是128位的字节数组,将字节数组用base64算法加密后得到的是字符串,这样有利于在其在数据库中的存储。
转自:http://wenbinemail-163-com.iteye.com/blog/1404290