base64_encode和base64_decode的JAVA實(shí)現(xiàn)
Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)代碼的編碼方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的詳細(xì)規(guī)范。 Base64要求把每三個(gè)8Bit的字節(jié)轉(zhuǎn)換為四個(gè)6Bit的字節(jié)(3*8 = 4*6 = 24),然后把6Bit再添兩位高位0,組成四個(gè)8Bit的字節(jié),也就是說,轉(zhuǎn)換后的字符串理論上將要比原來的長(zhǎng)1/3
php 的函數(shù):base64_encode() 和 base64_decode()
base64的編,解碼原理
Base64 編碼其實(shí)是將3個(gè)8位字節(jié)轉(zhuǎn)換為4個(gè)6位字節(jié),( 3*8 = 4*6 = 24 ) 這4個(gè)六位字節(jié) 其實(shí)仍然是8位,只不過高兩位被設(shè)置為0. 當(dāng)一個(gè)字節(jié)只有6位有效時(shí),它的取值空間為0 到 2的6次方減1 即63,也就是說被轉(zhuǎn)換的Base64編碼的每一個(gè)編碼的取值空間為(0~63) 。
事實(shí)上,0~63之間的ASCII碼有許多不可見字符,所以應(yīng)該再做一個(gè)映射,映射表為
'A' ~ 'Z' ? ASCII(0 ~ 25)
'a' ~ 'z' ? ASCII(26 ~ 51)
'0' ~ '9' ? ASCII(52 ~ 61)
' ' ? ASCII(62)
'/' ? ASCII(63)
這樣就可以將3個(gè)8位字節(jié),轉(zhuǎn)換為4個(gè)可見字符。
具體的字節(jié)拆分方法為:(圖(畫得不好,領(lǐng)會(huì)精神 :-))
aaaaaabb ccccdddd eeffffff //abcdef其實(shí)就是1或0,為了看的清楚就用abcdef代替
~~~~~~~~ ~~~~~~~~ ~~~~~~~~
字節(jié) 1 字節(jié) 2 字節(jié) 3
||
\/
00aaaaaa 00bbcccc 00ddddee 00ffffff
注:上面的三個(gè)字節(jié)位原文,下面四個(gè)字節(jié)為Base64編碼,其前兩位均為0。
這樣拆分的時(shí)候,原文的字節(jié)數(shù)量應(yīng)該是3的倍數(shù),當(dāng)這個(gè)條件不能滿足時(shí),用全零字節(jié)
補(bǔ)足,轉(zhuǎn)化時(shí)Base64編碼用=號(hào)代替,這就是為什么有些Base64編碼以一個(gè)或兩個(gè)等號(hào)結(jié)
束的原因,但等號(hào)最多有兩個(gè),因?yàn)椋喝绻鸉(origin)代表原文的字節(jié)數(shù),F(xiàn)(remain)代
表余數(shù),則
F(remain) = F(origin) MOD 3 成立。
所以F(remain)的可能取值為0,1,2.
如果設(shè) n = [F(origin) – F(remain)] / 3
當(dāng)F(remain) = 0 時(shí),恰好轉(zhuǎn)換為4*n個(gè)字節(jié)的Base64編碼。
當(dāng)F(remain) = 1 時(shí),由于一個(gè)原文字節(jié)可以拆分為屬于兩個(gè)Base64編碼的字節(jié),為了
讓Base64編碼是4的倍數(shù),所以應(yīng)該為補(bǔ)2個(gè)等號(hào)。
當(dāng)F(remain) = 2 時(shí),由于兩個(gè)原文字節(jié)可以拆分為屬于3個(gè)Base64編碼的字節(jié),同理,
應(yīng)該補(bǔ)上一個(gè)等號(hào)。
base64 編碼后的字符串末尾會(huì)有0到2個(gè)等號(hào),這些等號(hào)在解碼是并不必要,所以可以刪除。
在網(wǎng)絡(luò)GET 和 POST參數(shù)列表的時(shí)候,‘+'不能正常傳輸,可以把它替換成‘|'
這樣經(jīng)過base64編碼后的字符串就只有‘|'和‘/‘,所以經(jīng)過這樣處理base64編碼的字符串可以作為參數(shù)列表的以個(gè)參數(shù)值來傳輸
========================================================================
以下是老外寫的一個(gè)實(shí)現(xiàn):
package com.meterware.httpunit;
/********************************************************************************************************************
* $Id: Base64.java,v 1.4 2002/12/24 15:17:17 russgold Exp $
*
* Copyright (c) 2000-2002 by Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software "), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS ", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
/**
* A utility class to convert to and from base 64 encoding.
*
* @author <a href= "mailto:russgold@httpunit.org "> Russell Gold </a>
**/
public class Base64 { final static String encodingChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ "; /** * Returns the base 64 encoded equivalent of a supplied string. * @param source the string to encode */ public static String encode( String source ) { char[] sourceBytes = getPaddedBytes( source ); int numGroups = (sourceBytes.length + 2) / 3; char[] targetBytes = new char[4]; char[] target = new char[ 4 * numGroups ]; for (int group = 0; group < numGroups; group++) { convert3To4( sourceBytes, group*3, targetBytes ); for (int i = 0; i < targetBytes.length; i++) { target[ i + 4*group ] = encodingChar.charAt( targetBytes[i] ); } } int numPadBytes = sourceBytes.length - source.length(); for (int i = target.length-numPadBytes; i < target.length; i++) target[i] = '= '; return new String( target ); } private static char[] getPaddedBytes( String source ) { char[] converted = source.toCharArray(); int requiredLength = 3 * ((converted.length+2) /3); char[] result = new char[ requiredLength ]; System.arraycopy( converted, 0, result, 0, converted.length ); return result; } private static void convert3To4( char[] source, int sourceIndex, char[] target ) { target[0] = (char) ( source[ sourceIndex ] > > > 2); target[1] = (char) (((source[ sourceIndex ] & 0x03) < < 4) | (source[ sourceIndex+1 ] > > > 4)); target[2] = (char) (((source[ sourceIndex+1 ] & 0x0f) < < 2) | (source[ sourceIndex+2 ] > > > 6)); target[3] = (char) ( source[ sourceIndex+2 ] & 0x3f); } /** * Returns the plaintext equivalent of a base 64-encoded string. * @param source a base 64 string (which must have a multiple of 4 characters) */ public static String decode( String source ) { if (source.length()%4 != 0) throw new RuntimeException( "valid Base64 codes have a multiple of 4 characters " ); int numGroups = source.length() / 4; int numExtraBytes = source.endsWith( "== " ) ? 2 : (source.endsWith( "= " ) ? 1 : 0); byte[] targetBytes = new byte[ 3*numGroups ]; byte[] sourceBytes = new byte[4]; for (int group = 0; group < numGroups; group++) { for (int i = 0; i < sourceBytes.length; i++) { sourceBytes[i] = (byte) Math.max( 0, encodingChar.indexOf( source.charAt( 4*group+i ) ) ); } convert4To3( sourceBytes, targetBytes, group*3 ); } return new String( targetBytes, 0, targetBytes.length - numExtraBytes ); } private static void convert4To3( byte[] source, byte[] target, int targetIndex ) { target[ targetIndex ] = (byte) (( source[0] < < 2) | (source[1] > > > 4)); target[ targetIndex+1 ] = (byte) (((source[1] & 0x0f) < < 4) | (source[2] > > > 2)); target[ targetIndex+2 ] = (byte) (((source[2] & 0x03) < < 6) | (source[3])); } }
相關(guān)文章
SpringBoot項(xiàng)目中處理返回json的null值(springboot項(xiàng)目為例)
本文以spring boot項(xiàng)目為例給大家介紹SpringBoot項(xiàng)目中處理返回json的null值問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下2019-10-10Java實(shí)現(xiàn)讀取html文本內(nèi)容并按照格式導(dǎo)出到excel中
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)讀取html文本提取相應(yīng)內(nèi)容按照格式導(dǎo)出到excel中,文中的示例代碼講解詳細(xì),需要的可以參考下2024-02-02Java構(gòu)建JDBC應(yīng)用程序的實(shí)例操作
在本篇文章里小編給大家整理了一篇關(guān)于Java構(gòu)建JDBC應(yīng)用程序的實(shí)例操作,有興趣的朋友們可以學(xué)習(xí)參考下。2021-03-03springboot2.3 整合mybatis-plus 高級(jí)功能(圖文詳解)
這篇文章主要介紹了springboot2.3 整合mybatis-plus 高級(jí)功能,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08spring是如何實(shí)現(xiàn)聲明式事務(wù)的
這篇文章主要介紹了spring是如何實(shí)現(xiàn)聲明式事務(wù)的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Mybatis中SqlSession接口中selectList方法詳解
這篇文章主要給大家介紹了關(guān)于Mybatis中SqlSession接口中selectList方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03MyBatis使用動(dòng)態(tài)SQL標(biāo)簽的小陷阱
MyBatis是一個(gè)支持普通SQL查詢,存儲(chǔ)過程和高級(jí)映射的優(yōu)秀持久層框架,MyBatis越來越受大家的喜愛了。下面給大家分享MyBatis使用動(dòng)態(tài)SQL標(biāo)簽的小陷阱,感興趣的朋友一起看看吧2016-10-10