Base64以及Md5的使用

openkk 12年前發布 | 106K 次閱讀 加密/解密軟件包 BASE64

利用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);
    

    } }</pre></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(){
}

}</pre></span>接著來看看如何使用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; } }</pre>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(){
}

}</pre>為什么用md5算法加密后又要利用base64算法進行編碼:因為md5加密后得到的數據是128位的字節數組,將字節數組用base64算法加密后得到的是字符串,這樣有利于在其在數據庫中的存儲。
轉自:http://wenbinemail-163-com.iteye.com/blog/1404290

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!