Package com.john.j2se.util;
/**
* MD5 Encryption algorithm
*/
Import Java.security.MessageDigest;
public class Md5util {
Public final static string MD5 (string s) {
Char hexdigits[] = {' 0′, ' 1′, ' 2′, ' 3′, ' 4′,
' 5′, ' 6′, ' 7′, ' 8′, ' 9′,
' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
try {
byte[] Btinput = S.getbytes ();
MessageDigest mdinst = messagedigest.getinstance ("Md5″");
Mdinst.update (Btinput);
byte[] MD = Mdinst.digest ();
int j = Md.length;
Char str[] = new CHAR[J * 2];
int k = 0;
for (int i = 0; i < J; i++) {
byte byte0 = md[i];
str[k++] = hexdigits[byte0 >>> 4 & 0xf];
str[k++] = hexdigits[byte0 & 0xf];
}
return new String (str);
}
catch (Exception e) {
E.printstacktrace ();
return null;
}
}
}
MD5 encryption algorithm is a one-way encryption algorithm.
Import java.io.UnsupportedEncodingException;
Public class Base64 {
private static char[] Base64encodechars = new char[] {
' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ',
' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ',
' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ',
' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ',
' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ',
' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ',
' W ', ' x ', ' Y ', ' Z ', ' 0′, ' 1′, ' 2′, ' 3′,
' 4′, ' 5′, ' 6′, ' 7′, ' 8′ ', ' 9′, ' + ', '/'};
private static byte[] Base64decodechars = new byte[] {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1, 1,-1,,-62, 1,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1,-1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1,-1};
Coding
public static String encode (byte[] data) {
StringBuffer sb = new StringBuffer ();
int len = data.length;
int i = 0;
int B1, B2, B3;
while (I < Len) {
B1 = data[i++] & 0xff;
if (i = = len)
{
Sb.append (BASE64ENCODECHARS[B1 >>> 2]);
Sb.append (base64encodechars[(B1 & 0x3) << 4]);
Sb.append ("= =");
Break
}
B2 = data[i++] & 0xff;
if (i = = len)
{
Sb.append (BASE64ENCODECHARS[B1 >>> 2]);
Sb.append (base64encodechars[(B1 & 0x03) << 4) | ((B2 & 0xf0) >>> 4)]);
Sb.append (base64encodechars[(B2 & 0x0f) << 2]);
Sb.append ("=");
Break
}
B3 = data[i++] & 0xff;
Sb.append (BASE64ENCODECHARS[B1 >>> 2]);
Sb.append (base64encodechars[(B1 & 0x03) << 4) | ((B2 & 0xf0) >>> 4)]);
Sb.append (base64encodechars[(B2 & 0x0f) << 2) | ((B3 & 0xc0) >>> 6)]);
Sb.append (Base64encodechars[b3 & 0x3f]);
}
return sb.tostring ();
}
Decoding
public static byte[] Decode (String str) throws Unsupportedencodingexception {
StringBuffer sb = new StringBuffer ();
byte[] data = str.getbytes ("Us-ascii");
int len = data.length;
int i = 0;
int B1, B2, B3, B4;
while (I < Len) {
* B1 * *
do {
B1 = base64decodechars[data[i++]];
while (I < len && B1 = = 1);
if (B1 = = 1) break;
* B2 * *
do {
B2 = Base64decodechars
[data[i++]]; while (i < len && B2 = = 1);
if (B2 = = 1) break;
Sb.append ((char) (B1 << 2) | ((B2 & 0x30) >>> 4));
* B3 * *
do {
B3 = data[i++];
if (B3 = =) return sb.tostring (). GetBytes ("Iso8859-1″)";
B3 = base64decodechars[b3];
while (i < len && B3 = = 1);
if (B3 = = 1) break;
Sb.append ((char) ((B2 & 0x0f) << 4) | ((B3 & 0x3c) >>> 2));
* B4 * *
do {
B4 = data[i++];
if (B4 = =) return sb.tostring (). GetBytes ("Iso8859-1″)";
B4 = base64decodechars[b4];
while (i < len && b4 = = 1);
if (B4 = = 1) break;
Sb.append ((char) ((B3 & 0x03) << 6) (B4));
}
Return sb.tostring (). GetBytes ("Iso8859-1″)";
}
}
Base64 is a bidirectional encryption algorithm