Base64是網路上最常見的傳送8Bit位元組代碼的編碼方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的詳細規格。 Base64要求把每三個8Bit的位元組轉換為四個6Bit的位元組(3*8 = 4*6 = 24),然後把6Bit再添兩位高位0,組成四個8Bit的位元組,也就是說,轉換後的字符串理論上將要比原來的長1/3
php 的函數:base64_encode() 和base64_decode()
base64的編,解碼原理
Base64 編碼其實是將3個8位字節轉換為4個6位元組,( 3*8 = 4*6 = 24 ) 這4個六位元組其實仍然是8位元,只不過高兩位被設定為0. 當一個位元組只有6位元有效時,它的取值空間為0 到2的6次方減1 即63,也就是說被轉換的Base64編碼的每一個編碼的取值空間為(0~63) 。
事實上,0~63之間的ASCII碼有許多不可見字符,所以應該再做一個映射,映射表為
'A' ~ 'Z' ? ASCII(0 ~ 25)
'a' ~ 'z' ? ASCII(26 ~ 51)
'0' ~ '9' ? ASCII(52 ~ 61)
' ' ? ASCII(62)
'/' ? ASCII (63)可以將3個8位元組,轉換為4個可見字元。
特定的字節分割方法為:(圖(畫得不好,領會精神:-))
aaaaaabb ccccdddd eeffffff //abcdef其實就是1或0,為了看的清楚就用abcdefbiddddd
~ ~~~~~~~ ~~~~~~~~ ~~~~~~~~
字節1 位元組2 位元組3
||
/
ff00cc bboffof 07070000cc 0707870787007870787007878787878787878 -87x bbffoff;上面的三個位元組位元原文,下面四個位元組為Base64編碼,其前兩位均為0。
這樣分割的時候,原文的位元組數量應該是3的倍數,當這個條件不能滿足時,用全零字節
補足,轉換時Base64編碼用=號代替,這就是為什麼有些Base64編碼以一個或兩個等號結
束的原因,但等號最多有兩個,因為:若F(origin)代表原文的位元組數,F(remain)代
表餘數,則
F (remain) = F(origin) MOD 3 成立。
所以F(remain)的可能取值為0,1,2.
如果設n = [F(origin) – F(remain)] / 3
當F(remain) = 0 時,恰好轉換為4*n個位元組的Base64編碼。
當F(remain) = 1 時,由於一個原文字節可以分割為屬於兩個Base64編碼的位元組,為了
讓Base64編碼是4的倍數,所以應該要補2個等號。
當F(remain) = 2 時,由於兩個原文字節可以分割為屬於3個Base64編碼的位元組,同理,
應該補上一個等號。
base64 編碼後的字串末尾會有0到2個等號,這些等號在解碼是並不必要,所以可以刪除。
在網路GET 和POST參數列表的時候,'+'不能正常傳輸,可以把它替換成'|'
這樣經過base64編碼後的字符串就只有'|'和'/',所以經過這樣處理base64編碼的字串可以作為參數列表的以個參數值來傳送
================================= =======================================
以下是老外寫的一個實作:
package com.meterware.httpunit;
/******************************************************************************************************************** <br>* $Id: Base64.java,v 1.4 2002/12/24 15:17:17 russgold Exp $ <br>* <br>* Copyright (c) 2000-2002 by Russell Gold <br>* <br>* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated <br>* documentation files (the "Software "), to deal in the Software without restriction, including without limitation <br>* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and <br>* to permit persons to whom the Software is furnished to do so, subject to the following conditions: <br>* <br>* The above copyright notice and this permission notice shall be included in all copies or substantial portions <br>* of the Software. <br>* <br>* THE SOFTWARE IS PROVIDED "AS IS ", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO <br>* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE <br>* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF <br>* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER <br>* DEALINGS IN THE SOFTWARE. <br>* <br>*******************************************************************************************************************/ <br><br>/** <br>* A utility class to convert to and from base 64 encoding. <br>* <br>* @author <a href= "mailto:russgold@httpunit.org "> Russell Gold </a> <br>**/ <br> 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])); } }
更多base64_encode和base64_decode的JAVA實現相關文章請關注PHP中文網!

本文分析了2025年的前四個JavaScript框架(React,Angular,Vue,Susve),比較了它們的性能,可伸縮性和未來前景。 儘管由於強大的社區和生態系統,所有這些都保持占主導地位,但它們的相對人口

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

Node.js 20通過V8發動機改進可顯著提高性能,特別是更快的垃圾收集和I/O。 新功能包括更好的WebSembly支持和精製的調試工具,提高開發人員的生產率和應用速度。

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

本文介紹了SnakeyAml中的CVE-2022-1471漏洞,這是一個允許遠程代碼執行的關鍵缺陷。 它詳細介紹瞭如何升級春季啟動應用程序到Snakeyaml 1.33或更高版本的降低風險,強調了依賴性更新

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

本文使用lambda表達式,流API,方法參考和可選探索將功能編程集成到Java中。 它突出顯示了通過簡潔性和不變性改善代碼可讀性和可維護性等好處


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver CS6
視覺化網頁開發工具