Encoder Decoder Notes
Encoder Decoder Notes
Java provides a class Base64 to deal with encryption. You can encrypt and decrypt your data by using provided
methods. You need to import java.util.Base64 in your source file to use its methods.
This class provides three different encoders and decoders to encrypt information at each level. You can use these
methods at the following levels.
Basic Encoding and Decoding
It uses the Base64 alphabet specified by Java in RFC 4648 and RFC 2045 for encoding and decoding operations.
The encoder does not add any line separator character. The decoder rejects data that contains characters outside
the base64 alphabet.
URL and Filename Encoding and Decoding
It uses the Base64 alphabet specified by Java in RFC 4648 for encoding and decoding operations. The encoder
does not add any line separator character. The decoder rejects data that contains characters outside the base64
alphabet.
MIME
It uses the Base64 alphabet as specified in RFC 2045 for encoding and decoding operations. The
encoded output must be represented in lines of no more than 76 characters each and uses a carriage
return '\r' followed immediately by a linefeed '\n' as the line separator. No line separator is added to
the end of the encoded output. All line separators or other characters not found in the base64 alphabet
table are ignored in decoding operation.
Nested Classes of Base64
Class Description
Base64.Decoder This class implements a decoder for decoding byte data using the Base64 encoding scheme
as specified in RFC 4648 and RFC 2045.
Base64.Encoder This class implements an encoder for encoding byte data using the Base64 encoding scheme
as specified in RFC 4648 and RFC 2045.
Base64 Methods
Methods Description
public static Base64.Decoder getDecoder() It returns a Base64.Decoder that decodes using the Basic type
base64 encoding scheme.
public static Base64.Encoder getEncoder() It returns a Base64.Encoder that encodes using the Basic type
base64 encoding scheme.
public static Base64.Decoder It returns a Base64.Decoder that decodes using the URL and
getUrlDecoder() Filename safe type base64 encoding scheme.
public static Base64.Decoder It returns a Base64.Decoder that decodes using the MIME type
getMimeDecoder() base64 decoding scheme.
public static Base64.Encoder It Returns a Base64.Encoder that encodes using the MIME type
getMimeEncoder() base64 encoding scheme.
public static Base64.Encoder It returns a Base64.Encoder that encodes using the MIME type
getMimeEncoder(int lineLength, byte[] base64 encoding scheme with specified line length and line
lineSeparator) separators.
public static Base64.Encoder getUrlEncoder() It returns a Base64.Encoder that encodes using the URL and
Filename safe type base64 encoding scheme.
1
Dr. Punit Kumar Chaubey
Base64.Decoder Methods
Methods Description
public byte[] It decodes all bytes from the input byte array using the Base64 encoding scheme,
decode(byte[] src) writing the results into a newly-allocated output byte array. The returned byte
array is of the length of the resulting bytes.
public byte[] It decodes a Base64 encoded String into a newly-allocated byte array using the
decode(String src) Base64 encoding scheme.
public int decode(byte[] It decodes all bytes from the input byte array using the Base64 encoding scheme,
src, byte[] dst) writing the results into the given output byte array, starting at offset 0.
public ByteBuffer It decodes all bytes from the input byte buffer using the Base64 encoding scheme,
decode(ByteBuffer buffer) writing the results into a newly-allocated ByteBuffer.
public InputStream It returns an input stream for decoding Base64 encoded byte stream.
wrap(InputStream is)
Base64.Encoder Methods
Methods Description
public byte[] It encodes all bytes from the specified byte array into a newly-allocated byte array
encode(byte[] src) using the Base64 encoding scheme. The returned byte array is of the length of the
resulting bytes.
public int encode(byte[] It encodes all bytes from the specified byte array using the Base64 encoding
src, byte[] dst) scheme, writing the resulting bytes to the given output byte array, starting at offset
0.
public String It encodes the specified byte array into a String using the Base64 encoding
encodeToString(byte[] scheme.
src)
public ByteBuffer It encodes all remaining bytes from the specified byte buffer into a newly-allocated
encode(ByteBuffer buffer) ByteBuffer using the Base64 encoding scheme. Upon return, the source buffer's
position will be updated to its limit; its limit will not have been changed. The
returned output buffer's position will be zero and its limit will be the number of
resulting encoded bytes.
public OutputStream It wraps an output stream for encoding byte data using the Base64 encoding
wrap(OutputStream os) scheme.
public Base64.Encoder It returns an encoder instance that encodes equivalently to this one, but without
withoutPadding() adding any padding character at the end of the encoded byte data.
Java Base64 Example: Basic Encoding and Decoding
1. import java.util.Base64;
2. publicclass Base64BasicEncryptionExample {
3. publicstaticvoid main(String[] args) {
4. // Getting encoder
5. Base64.Encoder encoder = Base64.getEncoder();
6. // Creating byte array
7. bytebyteArr[] = {1,2};
8. // encoding byte array
9. bytebyteArr2[] = encoder.encode(byteArr);
2
Dr. Punit Kumar Chaubey
10. System.out.println("Encoded byte array: "+byteArr2);
11. bytebyteArr3[] = newbyte[5]; // Make sure it has enough size to store copied bytes
12. intx = encoder.encode(byteArr,byteArr3); // Returns number of bytes written
13. System.out.println("Encoded byte array written to another array: "+byteArr3);
14. System.out.println("Number of bytes written: "+x);
15. // Encoding string
16. String str = encoder.encodeToString("JavaTpoint".getBytes());
17. System.out.println("Encoded string: "+str);
18. // Getting decoder
19. Base64.Decoder decoder = Base64.getDecoder();
20. // Decoding string
21. String dStr = new String(decoder.decode(str));
22. System.out.println("Decoded string: "+dStr);
23. }
24. }
Output:
Encoded byte array: [B@6bc7c054
Encoded byte array written to another array: [B@232204a1
Number of bytes written: 4
Encoded string: SmF2YVRwb2ludA==
Decoded string: JavaTpoint
Java Base64 Example: URL Encoding and Decoding
1. import java.util.Base64;
2. publicclass Base64BasicEncryptionExample {
3. publicstaticvoid main(String[] args) {
4. // Getting encoder
5. Base64.Encoder encoder = Base64.getUrlEncoder();
6. // Encoding URL
7. String eStr = encoder.encodeToString("http://www.javatpoint.com/java-tutorial/".getBytes());
8. System.out.println("Encoded URL: "+eStr);
9. // Getting decoder
10. Base64.Decoder decoder = Base64.getUrlDecoder();
11. // Decoding URl
12. String dStr = new String(decoder.decode(eStr));
13. System.out.println("Decoded URL: "+dStr);
14. }
15. }
Output:
Encoded URL: aHR0cDovL3d3dy5qYXZhdHBvaW50LmNvbS9qYXZhLXR1dG9yaWFsLw==
Decoded URL: http://www.javatpoint.com/java-tutorial/
Java Base64 Example: MIME Encoding and Decoding
1. package Base64Encryption;
2. import java.util.Base64;
3. publicclass Base64BasicEncryptionExample {
4. publicstaticvoid main(String[] args) {
5. // Getting MIME encoder
6. Base64.Encoder encoder = Base64.getMimeEncoder();
7. String message = "Hello, \nYou are informed regarding your inconsistency of work";
8. String eStr = encoder.encodeToString(message.getBytes());
9. System.out.println("Encoded MIME message: "+eStr);
10. // Getting MIME decoder
3
Dr. Punit Kumar Chaubey
11. Base64.Decoder decoder = Base64.getMimeDecoder();
12. // Decoding MIME encoded message
13. String dStr = new String(decoder.decode(eStr));
14. System.out.println("Decoded message: "+dStr);
15. }
16. }
Output:
Encoded MIME message: SGVsbG8sIApZb3UgYXJlIGluZm9ybWVkIHJlZ2FyZGluZyB5b3VyIGluY29uc2lzdGVuY3kgb2Yg
d29yaw==
Decoded message: Hello,
You are informed regarding your inconsistency of work
4
Dr. Punit Kumar Chaubey