Java Base64 Encode and Decode21 Apr 2025 | 5 min read 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 DecodingIt 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 DecodingIt 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. MIMEIt 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
Base64 Methods
Base64.Decoder Methods
Base64.Encoder Methods
Java Base64 Example: Basic Encoding and DecodingOutput: 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 DecodingOutput: Encoded URL: aHR0cDovL3d3dy5qYXZhdHBvaW50LmNvbS9qYXZhLXR1dG9yaWFsLw== Decoded URL: http://www.javatpoint.com/java-tutorial/ Java Base64 Example: MIME Encoding and DecodingOutput: Encoded MIME message: SGVsbG8sIApZb3UgYXJlIGluZm9ybWVkIHJlZ2FyZGluZyB5b3VyIGluY29uc2lzdGVuY3kgb2Yg d29yaw== Decoded message: Hello, You are informed regarding your inconsistency of work Next TopicJava Default Methods |
We request you to subscribe our newsletter for upcoming updates.