How To Encode Decode String Inward Coffee Base64 Encoding
Encoding too Decoding of String inwards Java using base64 are extremely slowly if yous are using Apache green code opened upward source library. it provides convenient static utility method Base64.encodeBase64(byte[]) too Base64.decodeBase64(byte []) for converting binary information into base64 encoded binary Stream too and then decoding dorsum from encoded information inwards base64 encoding mechanism. Many times nosotros remove to encode sensitive information hold upward it binary String format transferring over a socket or transferring or storing information inwards XML files. Though at that spot are other opened upward source libraries available which supply back upward to encode whatever String into base64 e.g. MimeUtil from javax.mail package merely Apache green codec bundle is really handy too doesn't add together a lot of extra stuff. its API is likewise real corking too clean. In this article, nosotros volition run across how to encode too decode String into base64 encoding.
Base64 Encoding Algorithm
Encoding & Decoding String into Base64 Java
Here is a quick example of encoding too decoding of String inwards base64 encoding algorithm. nosotros volition rootage encode String past times converting it into a byte array too and then passing downward to Base64.encodeBase64(byte[]) which encodes byte array
as per base64 encoding algorithm too returns too encoded byte array, which tin move hold upward converted into String. inwards Later
half, nosotros volition decode the String past times calling Base64.decodeBase64(byte[]) method.
import org.apache.commons.codec.binary.Base64;
/**
* Java programme to encode too decode String inwards Java using Base64 encoding algorithm
* @author http://javarevisited.blogspot.com
*/
public degree Base64EncodingExample{
populace static void main(String args[]) throws IOException {
String orig = "original String earlier base64 encoding inwards Java";
//encoding byte array into base of operations 64
byte[] encoded = Base64.encodeBase64(orig.getBytes());
System.out.println("Original String: " + orig );
System.out.println("Base64 Encoded String : " + novel String(encoded));
//decoding byte array into base64
byte[] decoded = Base64.decodeBase64(encoded);
System.out.println("Base 64 Decoded String : " + novel String(decoded));
}
}
Output:
Original String: master copy String earlier base64 encoding inwards Java
Base64 Encoded String : b3JpZ2luYWwgU3RyaW5nIGJlZm9yZSBiYXNlNjQgZW5jb2RpbmcgaW4gSmF2YQ==
Base 64 Decoded String : master copy String earlier base64 encoding inwards Java
Dependency
In lodge to execute this Base64 Encoding Example inwards Java, yous remove to download too add together commons-codec-1.2.jar into your application classpath. if yous are using Maven for managing your projection dependency yous tin move include the following
dependency inwards pom.xml:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>20041127.091804</version>
</dependency>
This is real uncomplicated too construct clean base64 encoding instance inwards Java merely serves the purpose. That's all inwards how to
encode Java String into Base64 encoding too how to decode Java String. allow me know if yous expression upward whatever issue.
Further Learning
Complete Java Masterclass
Why multiple inheritances is non supported inwards Java
Komentar
Posting Komentar