欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

base64_encode和base64_decode的JAVA实现

程序员文章站 2024-03-11 18:55:55
base64是网络上最常见的用于传输8bit字节代码的编码方式之一,大家可以查看rfc2045~rfc2049,上面有mime的详细规范。 base64要求把每三个8bit...

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: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])); } }