Avatar of gudii9
gudii9
Flag for United States of America asked on

base64 decode encode

Hi,

I am going through below site.
https://www.base64decode.org/

when we encode and decode. What are advantages and disadvantages of base64 and what are other alternatives for this? please advise
JavaJava EEProgrammingProgramming Languages-OtherProgramming Theory

Avatar of undefined
Last Comment
Member_2_276102

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Russ Suter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
There are undocumented classes in the JRE but their use is not endorsed.
There are of course 3rd party libraries available

can you name some of popular 3rd party libraries and JRE classes?
gudii9

ASKER
tried below example

https://www.tutorialspoint.com/java8/java8_base64.htm
package test;
import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;

public class HelloWorld {
   public static void main(String args[]){
      try {
		
         // Encode using basic encoder
         String base64encodedString = Base64.getEncoder().encodeToString("TutorialsPoint?java8".getBytes("utf-8"));
         System.out.println("Base64 Encoded String (Basic) :" + base64encodedString);
		
         // Decode
         byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
		
         System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
         base64encodedString = Base64.getUrlEncoder().encodeToString("TutorialsPoint?java8".getBytes("utf-8"));
         System.out.println("Base64 Encoded String (URL) :" + base64encodedString);
		
         StringBuilder stringBuilder = new StringBuilder();
		
         for (int i = 0; i < 10; ++i) {
            stringBuilder.append(UUID.randomUUID().toString());
         }
		
         byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8");
         String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
         System.out.println("Base64 Encoded String (MIME) :" + mimeEncodedString);
         
      }catch(UnsupportedEncodingException e){
         System.out.println("Error :" + e.getMessage());
      }
   }
}

Open in new window


above gave below output


Base64 Encoded String (Basic) :VHV0b3JpYWxzUG9pbnQ/amF2YTg=
Original String: TutorialsPoint?java8
Base64 Encoded String (URL) :VHV0b3JpYWxzUG9pbnQ_amF2YTg=
Base64 Encoded String (MIME) :Njg0Y2E2ZWEtNDdiYi00MzRiLTgwYjItMGIwZDM3NTdhZDlkN2Y1ZmRhM2ItZjE4Yi00ZWQ2LWE3
ZTQtMTljOTZlMDZmYzU3ZjU5MzU2NzktNDQwOC00YjkxLWE4NjktMDc2MGI1MTQwZmI4OWIyOTlh
ZmUtYjdlYi00MmM5LWI2NjYtOWVhMzA2NjUzOTFhMmI2NzZiMjctMTI4ZS00ZmU3LWJkYzctYTYx
NjI0N2U3MmMzYjU0NzZkYmYtN2U4OS00ZmJkLTg0NmYtZDMwOTFlZjEwZWJhNTczZjMzZDgtNmYy
YS00ZTgzLThhY2YtNWM2ODk4OTgyYWM4ZjBkNzlhZGMtMzQ4Yi00NGE0LWE4NTUtNTc4ODdmMTll
MDE2MzYwNzdlYWUtYjQ3Mi00ZjM1LTk3YjktMjc3ZGJjYTA2NzcwOWM4YTAwMDYtZDliZC00Mjdh
LWJlNmItMzA5YTk1ODViNDFk

what is difference between

Base64 Encoded String (URL) :"

and

Base64 Encoded String (Basic) :"

and
Base64 Encoded String (MIME) :

when to use which one.

when we have to select UTF8, ASCII , CP1256 etc from dropdown of the site?

when we can encode and decode from that site any one can decode right? then how security of data transmission achieved?
CEHJ

Oops!


Since:
    1.8

they finally put it in the JRE!
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Russ Suter

Thanks for clarifying Sara. I did not intend to imply that base64 was in any way an encryption method but reading back what I wrote it could certainly be interpreted that way. What I was really saying is that if you take a string like "Hello World" and encrypt it using a nice, strong algorithm you will end up with a series of bytes with values ranging all the way from 0 to 255. Many of those characters are non-printable and won't survive even being pasted into a text message. Many more are invalid for URLs (like these characters for example: %/,!? ... and many others). Base64 encoding will ensure that the only characters you're sending are both printable and URL safe.

Base64 is encoding, not encryption.
gudii9

ASKER
Base64 is encoding, not encryption.

any further good reading related to these concepts. I am bit unclear still
Russ Suter

This provides a fairly detailed technical explanation of base64 encoding.

http://www.hcidata.info/base64.htm
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sarabande

a fairly detailed technical explanation of base64
there is one problem with the explanation: it uses the term ASCII as it would be an 8-bit code (256 code numbers). ASCII however is 7 bit only (128 code numbers) and the 8-bit enhancement is called ANSI. the first 128 code numbers of ANSI from 0 to 127 are identical to ASCII.

gudii9, i would recommend you to read the good article Russ has found nevertheless. the ASCII/ANSI mismatch is not important for base64, since base64 handles 7-bit and 8-bit character codes same way. it is simply that with ASCII Texts the bit 7 (the highest bit of range 0 ... 7) is 0, while for the enhanced ANSI characters the bit 7 is 1.

the base64 encoding is very simple. it doesn't care for characters and their representation but only for byte arrays. a byte is an unsigned 8-bit number, so it has a range from 0 to 255 decimal (2^8 == 256). in binary representation it is a sequence of 8 bits where each bit either is 0 or 1. if a bit is 1 we say the bit (at position) n is set. bit 0 is the least significant bit, bit 7 the most significant bit. the meaning of this should get clear if you bring to mind how the decimal value of a byte was calculated:

decimal = bit0*1 + bit1*2 + bit2*4 + bit3*8 + bit4*16 + bit5*32 + bit6*64 + bit7*128

since any bitx is 0 or 1 we would get decimal 255 when all bits are set by (1+2+4+8+16+32+64+128) und we would get decimal 0 if no bit is set. a number like 13 is built from (1+4+8) and therefore bit0, bit2, and bit3 are set and all others are 0.

base64 now looks at all bytes of a given text. as bytes also were used for binary data, base64 can take any input with an arbitrary number of consecutive bytes what is called a byte stream or a byte array. as the smallest unit for any data buffer is a byte, we could handle any contiguous data buffer as a byte stream and thus we can encode it with base64.

if n == length of byte stream, base64 creates or uses an empty output stream of (n/3)*4 bytes, what means the decoded buffer is one third bigger than the input stream.

bas64 now takes 3 bytes from input stream - what is 3*8==24==4*6 bits. then it takes the first 6 bits, bit0 to bit5, and puts them to an empty byte (all bits 0). we now have 6 bits from input byte what is a number from 0 to 63 and base64 uses fixed ASCII string literal

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

Open in new window


(length 64 characters) to convert the result to a printable ASCII character, for example  code 0 -> A, code 1 -> B, code 19 -> T, code 63 -> /.

because of this method the original character of the first input byte was completely different to the resulting base64 output character. base64 puts the calculated output character to the output stream. then it takes next 6 bits from input stream and does the same. after 3 input bytes base64 has created 4 output characters and would now repeat the algorithm with the next 3 bytes of the input stream until the input stream completely was encoded. at end of stream we have a little bit different handling if the length is not a multiple of 3.

decoding now does the inverse. the output stream now is input. base64 takes 4 ASCII characters from stream. each ascii character will be converted to the index it occurred in the "ABC..." literal. the result are 4 numbers each in the range  of 0 to 63, which is a 6 bit number. the 4 numbers were added to build a 24 bit number, what decodes to 3 bytes of the decoded output stream.

Sara
SOLUTION
Member_2_276102

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.