java基础----Base64算法的使用
Base64是網絡上最常見的用于傳輸8Bit字節代碼的編碼方式之一,可用于在HTTP環境下傳遞較長的標識信息。詳細的Base64信息,可以參見維基百科:https://en.wikipedia.org/wiki/Base64。今天,我們開始java中base64算法的使用。
?
結構導航
?
Java自帶的Base64算法
?項目結構如下
一、 引入jdk自帶的rt.jar包
要使用Base64Encoder,卻發現在Eclipse中無法找到該類,原來Base64Encoder并不屬于JDK標準庫范疇,但是又包含在了JDK中。
問題的解決方案: Project->Properties,選擇Java Build Path設置項,再選擇Libraries標簽,Add External Jars添加%JAVA_HOME%\jre\lib\rt.jar就行。
?
二、 我們通過junit方法,來加以對Base64算法的測試,明文:my name is huhx。
private static String src = "my name is huhx";@Test public void huhxBase64() {try {BASE64Encoder encoder = new BASE64Encoder();String encode = encoder.encode(src.getBytes());System.out.println("encode: " + encode);BASE64Decoder decoder = new BASE64Decoder();String decode = new String(decoder.decodeBuffer(encode));System.out.println("decode: " + decode);} catch (IOException e) {e.printStackTrace();} }?
三、 運行結果如下:
encode: bXkgbmFtZSBpcyBodWh4 decode: my name is huhx?
commons-codec的Base64算法
一、 引入commons-codec-1.10.jar包,提供對base64算法的支持
commons-codec-1.10.jar地址:https://www.bouncycastle.org/latest_releases.html
?
二、 我們通過junit方法,來加以對Base64算法的測試,明文:my name is huhx。
@Test public void commonCodeBase64() {byte[] encodeBytes = Base64.encodeBase64(src.getBytes());System.out.println("encode: " + new String(encodeBytes));byte[] decodeBytes = Base64.decodeBase64(encodeBytes);System.out.println("decode: " + new String(decodeBytes)); }?
三、 運行結果如下:?
encode: bXkgbmFtZSBpcyBodWh4 decode: my name is huhx?
bcprov的Base64算法
一、 引入bcprov-jdk15on-154.jar,提供對base64算法的支持
bcprov-jdk15on-154.jar地址: https://commons.apache.org/proper/commons-codec/download_codec.cgi
?
二、 我們通過junit方法,來加以對Base64算法的測試,明文:my name is huhx。
@Test public void bouncyCodeBase64() {byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());System.out.println("encode: " + new String(encodeBytes));byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encodeBytes);System.out.println("decode: " + new String(decodeBytes)); }?
三、 運行結果如下:
encode: bXkgbmFtZSBpcyBodWh4 decode: my name is huhx?
自定義的base64算法
一、 HuhxBase64.java的自定義算法,內容如下:
package com.huhx.base64.jdk;/*** writer: huhx*/ public class HuhxBase64 {private static final String CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";// base64解密private static byte[] base64Decode(String input) {if (input.length() % 4 != 0) {throw new IllegalArgumentException("Invalid base64 input");}byte decoded[] = new byte[((input.length() * 3) / 4) - (input.indexOf('=') > 0 ? (input.length() - input.indexOf('=')) : 0)];char[] inChars = input.toCharArray();int j = 0;int b[] = new int[4];for (int i = 0; i < inChars.length; i += 4) {// This could be made faster (but more complicated) by precomputing// these index locations.b[0] = CODES.indexOf(inChars[i]);b[1] = CODES.indexOf(inChars[i + 1]);b[2] = CODES.indexOf(inChars[i + 2]);b[3] = CODES.indexOf(inChars[i + 3]);decoded[j++] = (byte) ((b[0] << 2) | (b[1] >> 4));if (b[2] < 64) {decoded[j++] = (byte) ((b[1] << 4) | (b[2] >> 2));if (b[3] < 64) {decoded[j++] = (byte) ((b[2] << 6) | b[3]);}}}return decoded;}// base64加密private static String base64Encode(byte[] in) {StringBuilder out = new StringBuilder((in.length * 4) / 3);int b;for (int i = 0; i < in.length; i += 3) {b = (in[i] & 0xFC) >> 2;out.append(CODES.charAt(b));b = (in[i] & 0x03) << 4;if (i + 1 < in.length) {b |= (in[i + 1] & 0xF0) >> 4;out.append(CODES.charAt(b));b = (in[i + 1] & 0x0F) << 2;if (i + 2 < in.length) {b |= (in[i + 2] & 0xC0) >> 6;out.append(CODES.charAt(b));b = in[i + 2] & 0x3F;out.append(CODES.charAt(b));} else {out.append(CODES.charAt(b));out.append('=');}} else {out.append(CODES.charAt(b));out.append("==");}}return out.toString();}public static void main(String[] args) {String input = "I love you, huhx!";String encode = base64Encode(input.getBytes());System.out.println("encode: " + encode);String decode = new String(base64Decode(encode));System.out.println("decode: " + decode);} }?
二、 運行結果如下:
encode: SSBsb3ZlIHlvdSwgaHVoeCE= decode: I love you, huhx!?
友情鏈接
- base64算法的測試代碼:?http://pan.baidu.com/s/1eSueem2
?
作者:?huhx?出處:?www.cnblogs.com/huhx
總結
以上是生活随笔為你收集整理的java基础----Base64算法的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Scanner 类
- 下一篇: java基础----Java的格式化输出