Introduction to Java Base64
Java provides Base64 class to achieve the security feature; Base64 is a class that is used to deal with the encryption and decryption process using various methods. To use the Base64 class and its method in Java, we need to import the java.util.Base64 package in a source file. Note that the Base64 class can be used in Java 8 or upper versions. There are three types of Base64 encoding; each has been used to encrypt the data at each level.
How Does the Base64 Class Work in Java?
There are three types of Base64 encoding.
1. Simple/Basic Encoding and Decoding
In this type, to perform the encoding and decoding operations, the Base64 alphabets are being used that are specified by Java in RFC 4648 and RFC 2045. The encoder does not add any character that separates the line, and the decoder does not allow characters outside the Base64 alphabet.
2. URL based Encoding and Decoding
In this type, to perform the encoding and decoding operation, the Base64 alphabets are being used that are specified by java in RFC 4648. This type’s working is the same as basic encoding, but the output of this is URL.
3. MIME
To perform encoding and decoding operations, it uses the Base64 alphabet specified in RFC 2045. The encrypted result must be in lines of no more than 76 characters. The line separator is not being added at the end of the encrypted result. In the decryption process, characters or line separators that are not present in the Base64 alphabet table are ignored.
Types of Base64 Classes
There are two nested classes of Base64.
1. Base64.Encoder
This class is used to implement an encoder to encrypt the data bytes using the Base64 encoding technique as specified in RFC 2045 and RFC 4648.
Below are the Methods of Base64.An encoder is as follows:
- public byte[] encode(byte[] src): It is used to encrypt all data bytes of the specified byte array to a new byte array. For encryption, Base64 encoding techniques are used. The length of the new byte array is equal to the length of the resulting bytes.
- public int encodes (byte[] src, byte[] dst): Using the Base64 encoding technique, it encrypts all data bytes of the specified byte array by writing the resulting bytes to the given output byte array. The starting offset is 0.
- public OutputStreamwrap(OutputStreamos): Using the Base64 encoding technique, it wraps the output stream to encrypt the data bytes.
- public String encodedToString(byte[] src): Using the Base64 encoding technique, it encrypts the specified byte array into a string.
- public Base64.Encoder withoutPadding(): This method is used to create an encoder instance that encrypts equivalently. Here the encoding is done without adding any extra padding character at the end of the encrypted data byte.
- public ByteBufferencode(ByteBuffer buffer): Using the Base64 technique, it encrypts remaining data bytes from specified ByteBuffer into a new byte buffer. Based on the result, the position of the source buffer is updated to its limit. Note that once the limit is declared, it cannot be changed. Thus, the output buffer position will be zero, and the limit of this is equal to the number of resulting encoded bytes.
2. Base64.Decoder
This class is used to implement a decoder to decrypt the data bytes using the Base64 encoding technique as specified in RFC 2045 and RFC 4648
Methods of Base64.A decoder is as follows:
- public byte[] decode(byte[] src): Using the Base64 encoding technique, from an input byte array, this method decrypts all data bytes by writing the result into a new output byte array. The length of the returned byte array is equal to the resulting bytes.
- public byte[] decode(String src): Using the Base64 encoding technique, this method decrypts Base64 encrypted string into a byte array.
- public byte[] decode(String src): This method returns the input stream to decrypt the Base64 encrypted byte stream.
- public int decode(byte[] src, byte[] dst): Using the Base64 encoding technique, from an input byte array, this method decrypts all data bytes by writing the result into an output byte array. The starting offset is 0.
- public ByteBuffer decode(ByteBuffer buffer): Using the Base64 encoding technique, from input ByteBuffer, this method decrypts all data bytes by writing the result into new ByteBuffer.Base64 Methods
Methods of Base64 classes are as follows:
- public static Base64.Encoder getEncoder(): This method returns Base64.Encoder that encrypts the data using the basic Base64 encoding technique.
- public static Base64.Encoder getMimeEncoder(): This method returns Base64.Encoder that encrypts the data using MIME Base64 encoding technique.
- public static Base64.Encoder getUrlEncoder(): This method returns Base64.Encoder that encrypts the data using URL Base64 encoding technique.
- public static Base64.Encoder getMimeEncoder(intlineLength, byte[] lineSeparator): This method returns Base64.Encoder that encrypts the data using MIMEL Base64 encoding technique with specified line length and line separator.
- public static Base64.Decoder getDecoder(): This method returns Base64.decoder decrypts the data using the basic Base64 encoding technique.
- public static Base64.Decoder getUrlDecoder(): This method returns Base64.decoder decrypts the data using the URL Base64 encoding technique.
- public static Base64.Decoder getMimeDecoder(): This method returns Base64.Decoder decrypts the data using the MIME Base64 encoding technique.
Recommended Articles
This is a guide to Java Base64. Here we discuss the Introduction to Java Base64 and its different Meth and Examples and Code Implementation. You can also go through our other suggested articles to learn more –