程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> base64_encode和base64_decode的JAVA完成

base64_encode和base64_decode的JAVA完成

編輯:關於JAVA

base64_encode和base64_decode的JAVA完成。本站提示廣大學習愛好者:(base64_encode和base64_decode的JAVA完成)文章只能為提供參考,不一定能成為您想要的結果。以下是base64_encode和base64_decode的JAVA完成正文


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,為了看的清晰就用abcdef取代

~~~~~~~~ ~~~~~~~~ ~~~~~~~~

字節 1 字節 2 字節 3

    ||
    \/

00aaaaaa 00bbcccc 00ddddee 00ffffff

注:下面的三個字節位原文,上面四個字節為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;

/******************************************************************************************************************** 
* $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:[email protected] "> 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])); } }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved