Base64 encoding

zhaozj2021-02-12  158

public final class Base64 {static private final int BASELENGTH = 255; static private final int LOOKUPLENGTH = 64; static private final int TWENTYFOURBITGROUP = 24; static private final int EIGHTBIT = 8; static private final int SIXTEENBIT = 16; static private final int SIXBIT = 6; static private final int FOURBYTE = 4; static private final int SIGN = -128; static private final byte PAD = (byte) '='; static private byte [] base64Alphabet = new byte [BASELENGTH]; static private byte [ ] lookupbase64alphabet = new byte [lookuplength];

Static {for (int i = 0; i

For (int i = 'z'; i> = 'a'; i -) {base64alphabet [i] = (byte) (i - 'a');}

For (int i = 'z'; i> = 'a'; I -) {base64alphabet [i] = (byte) (i - 'a' 26);}

For (int i = '9'; i> = '0'; I -) {base64alphabet [i] = (byte) (i - '0' 52);}

Base64alphabet [' '] = 62; Base64alphabet ['/'] = 63;

For (int i = 0; i <= 25; i ) lookupbase64alphabet [i] = (byte) ('a' i);

For (int i = 26, j = 0; i <= 51; i , j ) lookupbase64alphabet [i] = (byte) ('a' j);

For (int i = 52, j = 0; i <= 61; i , j ) lookupbase64alphabet [i] = (byte) ('0' J);

lookUpBase64Alphabet [62] = (byte) ' '; lookUpBase64Alphabet [63] = (byte) '/';} public static boolean isBase64 (String isValidString) {return isArrayByteBase64 (isValidString.getBytes ());}

Public Static Boolean ISBase64 (Byte Octect) {// Shall We ignore White Space? Jeff ?? Return ((Octect == PAD) || (Base64alphabet [Octect]! = -1));}

Public Static Boolean IsarrayBytebase64 (Byte [] ArrayOctect) {Int length = arrayoctect.length

IF (Length == 0) {// Shouldn't A 0 Length Array Be Valid Base64 Data? // Return False; Return True;}

For (int i = 0; i

Return True;}

Public static string encode (string strdata) {byte [] TMP = Encode (STRDATA.GETBYTES ());

Return New String (TMP);

/ ** * Encodes hex octects into Base64. * * @Param binaryData Array containing binary data to encode. * @Return Base64-encoded data. * / Public static byte [] encode (byte [] binaryData) {int lengthDataBits = binaryData. LENGTH * EIGHTBIT; INT FEWERTHAN24BITS = LengthDatabase; int number = NULL;

IF (FewERTHAN24BITS! = 0) {// data not divisible by 24 bit encodeddata = new byte [(Numbertriplets 1) * 4];} else {//6 or 8 bit encodeddata = new byte [Numbertriplets * 4];}

Byte k = 0; byte b1 = 0; byte b2 = 0; byte b3 = 0; int encodedindex = 0; int DataIndex = 0; INT i = 0;

For (i = 0; i

l = (Byte) (B2 & 0x0F); k = (BYTE) (B1 & 0x03);

EncodedIndex = i * 4;

BYTE VAL1 = ((B1 & Sign) == 0)? (BYTE) (BYTE) ((BYTE) (((b1) >> 2) ^ 0xc0); Byte Val2 = ((B2 & Sign) = = 0)? (BYTE) (B2 >> 4): (((b2) >> 4) ^ 0xF0); Byte Val3 = ((B3 & Sign) == 0)? (BYTE) (B3> > 6): (BYTE) (((b3) >> 6) ^ 0xFC);

encodedData [encodedIndex] = lookUpBase64Alphabet [val1]; encodedData [encodedIndex 1] = lookUpBase64Alphabet [val2 | (k << 4)]; encodedData [encodedIndex 2] = lookUpBase64Alphabet [(l << 2) | val3]; encodedData [ EncodedIndex 3] = lookupbase64alphabet [b3 & 0x3f];

// Form Integral Number of 6-bit Groups DataIndex = i * 3; EncodedIndex = i * 4;

IF (FEWERTHAN24BITS == Eightbit) {b1 = binarydata [dataindex]; k = (byte) (B1 & 0x03);

BYTE VAL1 = ((B1 & Sign) == 0)? (BYTE) (BYTE) ((Byte) (((b1) >> 2) ^ 0xc0); EncodedData [EncodeDed Index] = lookupbase64alphabet [var); encodedData [encodedIndex 1] = lookUpBase64Alphabet [k << 4]; encodedData [encodedIndex 2] = PAD; encodedData [encodedIndex 3] = PAD;} else if (fewerThan24bits == SIXTEENBIT) {b1 = binaryData [dataIndex]; B2 = binarydata [dataDex 1]; l = (B2 & 0x0F); k = (byte) (B1 & 0x03); Byte Val1 = ((B1 & Sign) == 0)? (BYTE) (B1) >> 2): (Byte) (((b1) >> 2) ^ 0xc0); Byte Val2 = ((B2 & Sign) == 0)? (BYTE) (B2 >> 4): (Byte) (b2) >> 4) ^ 0xF0);

encodedData [encodedIndex] = lookUpBase64Alphabet [val1]; encodedData [encodedIndex 1] = lookUpBase64Alphabet [val2 | (k << 4)]; encodedData [encodedIndex 2] = lookUpBase64Alphabet [l << 2]; encodedData [encodedIndex 3] = PAD;

Return encodeddata;

/ ** * Decodes Base64 data into octects * * @param binaryData Byte array containing Base64 data * @return Array containing decoded data. * / Public static byte [] decode (byte [] base64Data) {// handle the edge case, so We don't have to worry about it lat == 0) {Return New Byte [0];

INT NUMBERQUADRUPLE = Base64Data.LENGTH / FOURBYTE; BYTE [] decodeddata = NULL; BYTE B1 = 0; BYTE B2 = 0; BYTE B3 = 0; Byte B4 = 0; Byte Marker0 = 0; BYTE Marker1 = 0; // Throw Away Anything not in base64Data int encodedindex = 0; int DataIndex = 0;

{// this Sizes The Output Array Properly - rlw int lastdata = base64data.length

// ignore the '=' padding while (base64data - 1] == PAD) {if (--lastdata == 0) {Return New Byte [0];}}

DecodedData = new byte [LastData - Numberquadruple];

For (int i = 0; i

B1 = base64alphabet [base64data [dataindex]]; b2 = base64alphabet [base64data [dataIndex 1]];

IF ((Marker0! = PAD) && (Marker1! = PAD)) {// no Pad E.G 3CQL B3 = Base64alphabet [Marker0]; b4 = base64alphabet [marker1];

DecodedData [EncodeDex] = (BYTE) ((B1 << 2) | (B2 >> 4)); decodeddata [encodeDex 1] = (Byte) (((B2 & 0xF) << 4) | ((B3> > 2) & 0xF)); decodeddata [EncodeDex 2] = (Byte) ((B3 << 6) | b4);} else if (Marker0 == PAD) {// Two Pad EG 3C [PAD] [PAD ] DecodedData [EncodeDex] = (BYTE) ((B1 << 2) | (B2 >> 4));} else if (Marker1 == PAD) {// One Pad EG 3CQ [PAD] b3 = base64alphabet [marker0] DecodedData [EncodeDex] = (Byte) ((B1 << 2) | (B2 >> 4)); decodedData [EncodeDex 1] = (Byte) (((B2 & 0xF) << 4) | ((B3) >> 2) & 0xF);

EncodedIndex = 3;

Return decodeddata;

Public static void main (string [] args) {string a = "bonjovi@yahoo.com.cn"; system.out.println (Base64.encode (a));}}

转载请注明原文地址:https://www.9cbs.com/read-6329.html

New Post(0)