Java examples of using MD5 and BASE64

Source: Internet
Author: User
Tags base64 md5 md5 encryption printable characters stringbuffer

0X00 Introduction

The most recent use of BASE64 encoding and MD5 encryption in your code makes it easy to find your notes here.
When the Postfix mail server was configured, it was found that the message body received was encoded using BASE64, so it was understood.
MD5 is a common means of encryption. Although MD5 is not a cryptographic algorithm, it can be used as encryption.
0X01 BASE64 Code

Base64 is a representation that represents binary data based on 64 printable characters. Because 2 of the 6 times equals 64, each 6 bit is a unit, corresponding to a printable character. Three bytes have 24 bits, corresponding to 4 Base64 units, that is, 3 bytes need to be represented by 4 printable characters. It can be used as a transport encoding for e-mail messages. printable characters in Base64 include letters A-Z, a-Z, number 0-9, 62 characters in total, and two printable symbols that differ in different systems. Some other coding methods, such as Uuencode, and later versions of BinHex use different 64 character sets to represent 6 binary digits, but they are not called Base64. -----------Wikipedia

Code requires import Sun.misc.BASE64Encoder;
public static string encodeing (String str) {
Byte[] B = null;
String s = null;
try{
b = Str.getbytes ("Utf-8");
}catch (Exception e) {
E.printstacktrace ();
}
if (b!= null) {
s = new Base64encoder (). Encode (b);
}
return s;
}
0X02 BASE64 Decoding

Code requires import Sun.misc.BASE64Decoder;
public static string decoding (String str) {
Byte[] B = null;
String result = null;
if (str!= null) {
Base64decoder decoder = new Base64decoder ();
try{
b = Decoder.decodebuffer (str);
result = new String (b, "Utf-8");
}catch (Exception e) {
E.printstacktrace ();
}
}
return result;
}
0x03 MD5 Encryption

MD5 Message digest Algorithm (English: MD5 message-digest algorithm), a widely used cryptographic hash function, produces a 128-bit (16-byte) hash value (hash value) to ensure that the information is transmitted in a consistent way. MD5 by Ronald · The Leevist design was made public in 1992 to replace the MD4 algorithm. The procedures for this algorithm are regulated in RFC 1321.
The basic principle of hashing algorithm is to change the data (such as a paragraph of text) into another fixed length value.
1996 years later, it is proven that weaknesses can be cracked, and for data requiring high security, experts generally recommend switching to other algorithms, such as SHA-1. 2004, confirmed that the MD5 algorithm can not prevent collisions, so can not be applied to security authentication, such as SSL public key authentication or digital signature purposes. -----------Wikipedia

Although MD5 has been shown to be unsafe, it is not a problem to use as an experimental landing verification. (In fact, many a lot of Web site passwords are MD5, do not believe you can go to the social Work library to see)
Code requires import java.security.MessageDigest;
public static string GetMd5 (string text) {
try{
MessageDigest MD = messagedigest.getinstance ("MD5");
Md.update (Text.getbytes ());
byte b[] = Md.digest ();
int i;
StringBuffer buf = new StringBuffer ("");
for (int offset = 0; offset < b.length; offset++) {
i = B[offset];
if (I < 0) {
i + 256;
}
if (I < 16) {
Buf.append ("0");
}
Buf.append (integer.tohexstring (i));
}
return buf.tostring (); 32-bit encryption
Return buf.tostring (). SUBSTRING (8, 24); 16-bit encryption
}catch (Exception e) {
E.printstacktrace ();
return null;
}
}

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.