How to encode and decode BASE64 in Java

Source: Internet
Author: User
Tags base64 md5 digest printable characters

BASE64 encoding is a commonly used character encoding and is used in many places. The JDK provides easy-to-use Base64encoder and Base64decoder, which makes it easy to complete BASE64-based encoding and decoding. Here are the two small functions I made, respectively, for BASE64 encoding and decoding:

Encode s for BASE64
public static string getBASE64 (string s) {
if (s = = null) return null;
Return (new Sun.misc.BASE64Encoder ()). Encode (S.getbytes ());
}

Decodes a BASE64 encoded string s
public static string getFromBASE64 (string s) {
if (s = = null) return null;
Base64decoder decoder = new Base64decoder ();
try {
Byte[] B = Decoder.decodebuffer (s);
return new String (b);
} catch (Exception e) {
return null;
}
}
Decodes a BASE64 encoded string InputStream
public static Java.nio.ByteBuffer Getfrombase64byte (String s) {
if (s = = null)
return null;
Base64decoder decoder = new Base64decoder ();
try {
return Decoder.decodebuffertobytebuffer (s);//decoder.decodebuffer (s);
} catch (Exception e) {
return null;
}
}

Decoding a BASE64 encoded file

Bytebuffer value = Base64utils.getfrombase64byte (Nl.item (i*2+1). Gettextcontent (). Trim ()); FileOutputStream fos = new FileOutputStream (filename); FileChannel FC = Fos.getchannel ();
Fc.write (value);
Fos.flush ();
Fc.close ();


Import Sun.misc.BASE64Encoder;
Import Sun.misc.BASE64Decoder;


Another simple way to calculate the digest of the paper is provided in Java, which is to use the Java.security.MessageDigest class. The following code snippet shows how to apply the MD5 digest to an algorithm (a 128-bit digest) to a password string:
Massagedigest md=
Messagedigest.getinstance ("MD5");
Md.update (Originalpwd.getbyetes ());
Byte[] Digestedbytes=md.digest ();

Also use newspaper digest to create a checksum, a unique ID for the text (also called a digital fingerprint). When you write a Arj file, the checksum is calculated based on the contents of the Arj file, then encrypted and stored in the MANIFEST.MF file in the Base64 encryption format. Base64 is a method of encoding arbitrary binary data, resulting in only printable characters (note that the Base64 encoded data occupies one-third more space than before the conversion). As the result of the digest to the algorithm output is a byte array, you can use Base64 encoding to convert the hash byte to a string so that the string can be stored in the varchar field of the database. There are many Base64 encoders now, but the simplest approach is to use the encoder in the Weblogic.jar library: weblogic.apache.xerces.utils.Base64. This class has minimal effect, as shown in the following code example:

String digestedpwdstring =
New String (Base64.encode (digestedpwdbytes));


Import javax.mail.internet.*;
Import java.security.*;
public string Getencodedhash (string cleartext) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
OutputStream out = Mimeutility.encode (BAOs, "base64");
MessageDigest MD = messagedigest.getinstance ("SHA");
if (cleartext = = null) cleartext = "";
byte [] in = Cleartext.getbytes ();
byte [] digested = md.digest (in);
Out.write (digested);
Out.close ();
return new String (Baos.tobytearray (), "iso-8859-1");
}

How to encode and decode BASE64 in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.