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 * */ 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); } } |
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 | 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(){ } } |
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 | 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; } } |
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 | 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(){ } } |
联系客服