Base64以及Md5的使用

来源:互联网 发布:西方建筑欣赏知乎 编辑:程序博客网 时间:2024/06/08 09:50
利用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这个包下面的类就不会报错了。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.piedra.base64;
import java.io.IOException;
 
import sun.misc.*;
/**
 * 通过这个类实现利用base64算法进行编码和解码。
 * @author
 *
 */
publicclass Base64 {
     
    @SuppressWarnings("restriction")
    publicString encode(String toEncodeContent){
        if(toEncodeContent == null){
            returnnull;
        }
        BASE64Encoder encoder = newBASE64Encoder();
        returnencoder.encode(toEncodeContent.getBytes());
    }
     
    publicString encode(byte [] toEncodeContent){
        returnencode(newString(toEncodeContent));
    }
     
    @SuppressWarnings("restriction")
    publicString decode(String toDecodeContent){
        if(toDecodeContent == null) {
            returnnull;
        }
        byte[] buf = null;
        try{
            buf = newBASE64Decoder().decodeBuffer(toDecodeContent);
        catch(IOException e){
            e.printStackTrace();
        } finally {
        }
        returnnew String(buf);
    }
}
下面是测试代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
packagecom.piedra.base64;
 
importstatic org.junit.Assert.*;
 
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
 
publicclass Base64Test {
    privateBase64 base64;
     
    @Before
    publicvoid init(){
        base64 = newBase64();
    }
     
    @Test
    publicvoid testEncode() {
        String toEncodeContent = "I am grade to learn java.";
        String encodedContent = base64.encode(toEncodeContent);
        //由于要测试toEncodeContent经过BASE64编码后的字符序列。因此就直接打印,没有用Assert的方法。
        System.out.println(encodedContent);
    }
 
    @Test
    publicvoid testDecode() {
        String toDecodeContent = "SSBhbSBncmFkZSB0byBsZWFybiBqYXZhLg==";
        String decodedContent = base64.decode(toDecodeContent);
        String expected = "I am grade to learn java.";
        String actual = decodedContent;
        assertEquals(expected,actual);
    }
 
    @After
    publicvoid 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.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
packagecom.piedra.base64;
 
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
/**
 * 通过这个类我们可以利用getDigest方法对我们需要加密的数据进行加密。
 * @author
 *
 */
publicclass Md5 {
     
    /**
     * 通过这个方法可以获得特定输入数据的文摘
     * @param input 需要进行获取文摘的字节数组
     * @return 特定数据的文摘
     */
    publicbyte[] 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();
        }
        returndigestedValue;
    }
}
md5的测试代码以及base64和md5的结合使用:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
packagecom.piedra.base64;
 
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
 
publicclass Md5Test {
    privateMd5 md5;
    privateBase64 base64;
     
    @Before
    publicvoid init(){
        md5 = newMd5();
        base64 = newBase64();
    }
     
    @Test
    publicvoid testGetDigest() {
        String toDigest = "just a test.";
        byte[] digestedValue = md5.getDigest(toDigest.getBytes());
        System.out.println(newString(digestedValue));
    }
     
    @Test
    publicvoid testEncrypt(){
        String toEncrypt = "This is my password.";
        byte[] encrypted = md5.getDigest(toEncrypt.getBytes());
        String encodedPassword = base64.encode(encrypted);
        System.out.println(encodedPassword);
    }
     
    @After
    publicvoid destroy(){
    }
}
为什么用md5算法加密后又要利用base64算法进行编码:因为md5加密后得到的数据是128位的字节数组,将字节数组用base64算法加密后得到的是字符串,这样有利于在其在数据库中的存储。
0 0