search
HomeJavajavaTutorialJAVA implementation of base64_encode and base64_decode

Base64 is one of the most common encoding methods for transmitting 8-bit byte codes on the Internet. You can check RFC2045~RFC2049, which has detailed specifications for MIME. Base64 requires converting every three 8Bit bytes into four 6Bit bytes (3*8 = 4*6 = 24), and then adding two high-bit 0s to the 6Bit to form four 8Bit bytes, that is Said, the converted string will theoretically be 1/3 longer than the original

php functions: base64_encode() and base64_decode()

base64 encoding and decoding principle

Base64 encoding actually converts 3 8-bit bytes into 4 6-bit bytes, (3*8 = 4*6 = 24) These 4 six-bit bytes are actually still 8 bits, but they are two higher bit is set to 0. When only 6 bits of a byte are valid, its value space is from 0 to 2 to the 6th power minus 1, which is 63, that is to say, the value space of each encoding of the converted Base64 encoding is (0~63).

In fact, there are many invisible characters in the ASCII code between 0 and 63, so another mapping should be done. The mapping table is

'A' ~ 'Z' ? ASCII (0 ~ 25)

'a' ~ 'z' ? ASCII (26 ~ 51)

'0' ~ '9' ? ASCII (52 ~ 61)

' ' ? ASCII (62)

'/' ? ASCII (63)

In this way, 3 8-bit bytes can be converted into 4 visible characters.

The specific byte splitting method is: (picture (the drawing is not good, understand the spirit:-))

aaaaaabb ccccdddd eeffffff //abcdef is actually 1 or 0, for the sake of viewing If it is clear, use abcdef instead

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

Byte 1 Byte 2 Byte 3

 || The first two digits are both 0.

When split like this, the number of bytes of the original text should be a multiple of 3. When this condition cannot be met, use all zero bytes

to make up for it. Use the = sign for Base64 encoding during conversion. Instead, this is why some Base64 encodings end with one or two equal signs

, but there are at most two equal signs, because: if F(origin) represents the number of bytes of the original text, F( remain) represents the remainder, then

F(remain) = F(origin) MOD 3 is established.

So the possible values ​​of F(remain) are 0,1,2.

If n = [F(origin) – F(remain)] / 3

When F(remain) = 0, it is converted to Base64 encoding of 4*n bytes.

When F(remain) = 1, since one original text byte can be split into two Base64-encoded bytes, in order to

make the Base64 encoding a multiple of 4, it should To add 2 equal signs.

When F(remain) = 2, since the two original text bytes can be split into three Base64-encoded bytes,

should be added with an equal sign.

base64 There will be 0 to 2 equal signs at the end of the encoded string. These equal signs are not necessary for decoding, so they can be deleted.
In the network GET and POST parameter lists, '+' cannot be transmitted normally, you can replace it with '|'
In this way, the base64-encoded string will only have '|' and '/', so The base64-encoded string processed in this way can be transmitted as a parameter value in the parameter list

========================== ===============================================
The following is an implementation written by a foreigner:
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] = &#39;= &#39;; 
  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])); 
 } 
 
}




For more articles related to the JAVA implementation of base64_encode and base64_decode, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools