BASE64编码解码,md5编码:package com.me.util;import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import java.security.MessageDigest;public class base64md5 {
private char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
public base64md5() {
} public String getBASE64(String s) { //将 字符串 s 进行BASE64编码
if (s == null) {
return null;
}
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
}// 将 BASE64 编码的字符串 s 进行解码
public 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;
}
} public String getMd5(String s) { //将 字符串 s 进行MD5编码
try {
byte[] strTemp = s.getBytes();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(strTemp);
byte[] md = messageDigest.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) {
return null;
} }
}
import sun.misc.BASE64Decoder;
import java.security.MessageDigest;public class base64md5 {
private char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
public base64md5() {
} public String getBASE64(String s) { //将 字符串 s 进行BASE64编码
if (s == null) {
return null;
}
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
}// 将 BASE64 编码的字符串 s 进行解码
public 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;
}
} public String getMd5(String s) { //将 字符串 s 进行MD5编码
try {
byte[] strTemp = s.getBytes();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(strTemp);
byte[] md = messageDigest.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) {
return null;
} }
}
import sun.misc.BASE64Decoder;
import java.security.MessageDigest;
是不是还要去下专门的包?