Base64 encoder/decoder [Java version]-post

Source: Internet
Author: User
Tags printable characters

Http://www.javaidea.net/list.jsp? Topic = 5

Author: Home cat

Base64 encoding is a common character encoding, which is used in many places. JDK provides very convenient base64encoder and base64decoder, which can be used to conveniently complete base64-based encoding and decoding. The following are two small functions compiled by myself for base64 encoding and decoding:

// Encode s with base64
Public static string getbase64 (string s ){
If (S = NULL) return NULL;
Return (new sun. Misc. base64encoder (). encode (S. getbytes ());
}

// Decodes the 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;
}
}

------

Reply to this article |

Import sun. Misc. base64encoder;
Import sun. Misc. base64decoder;

------

Reply to this article |

Http://dev2dev.bea.com.cn/techdoc/webser/20030573.html

By Tim pijpops

Use message digest

Java provides another simple method for calculating the message digest, that is, using the java. Security. messagedigest class. The following code snippet shows how to apply the MD5 message digest algorithm (128-bit digest) to a password string:
Massagedigest MD =
Messagedigest. getinstance ("MD5 ");
Md. Update (originalpwd. getbyetes ());
Byte [] digestedbytes = md. Digest ();

It also uses the message digest to create a unique ID (also called a digital fingerprint) of the checksum and text ). When the ARJ file is signed, the checksum is calculated based on the content of the ARJ file and then encrypted and stored in the manifest. MF file in base64 encrypted format. Base64 is a method for encoding any binary data. The result only contains printable characters. (Note that base64 encoding occupies 1/3 more space than before conversion ). Because the output result of the packet Digest algorithm is a byte array, you can use base64 encoding to convert the hash byte into a string so that the string can be stored in the varchar field of the database. There are many base64 encoders, but the simplest method is to use the encoder in the weblogic. Jar Library: weblogic. Apache. xerces. utils. base64. This class has little effect, as shown in the following code example:

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

------

Reply to this article |

Http://minnigerode.org/CA-SF/dave/BasicJBossAAC.html

Feb. 2004
M. David minnigerode
Minniger@minnigerode.org

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 ");
}

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.