base64
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
/**
* base64解析 工具类
*/
public class HttpBase64Utils {
private final static BASE64Decoder decoder = new BASE64Decoder();
private final static BASE64Encoder encoder = new BASE64Encoder();
/**
* 解密
*
* @param
* @return
*/
public static InputStream decoderInputStream(InputStream inputStream) throws Exception {
try {
byte[] bytes = decoder.decodeBuffer(inputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
return byteArrayInputStream;
} catch (Exception e) {
throw new Exception("数据解密异常!");
}
}
/**
* 解密
*
* @param
* @return
*/
public static InputStream decoderOfByte(byte[] bytes) throws Exception {
try {
byte[] bytes1 = decoder.decodeBuffer(new String(bytes, Charset.defaultCharset().name())
.replaceAll(" ", "")
.replaceAll("\n", ""));
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes1);
return byteArrayInputStream;
} catch (Exception e) {
throw new Exception("数据解密异常!");
}
}
/**
* 解密 字符
* @return
* @throws Exception
*/
public static String decoder(String buff) throws Exception {
try {
byte[] bytes = decoder.decodeBuffer(String.valueOf(buff));
return new String(bytes);
} catch (Exception e) {
throw new Exception("数据解密异常!");
}
}
/**
* 加密
*
* @param
* @return
* @throws Exception
*/
public static String encoder(String conter) throws Exception {
try {
byte[] bytes = conter.getBytes(Charset.defaultCharset().name());
String encode = encoder.encode(bytes);
return encode;
} catch (Exception e) {
throw new Exception("数据加密异常!");
}
}
}
MD5
import java.security.MessageDigest;
/**
* md5加密工具类
*/
public class Md5Utils {
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
/**
* 将字节数组转换成16进制字符串
* @param b
* @return
*/
public static String byteArraytoHexString(byte b[]){
StringBuffer stringBuffer=new StringBuffer();
for(int i=0;i<b.length;i++){
stringBuffer.append(byteToHexString(b[i]));
}
return stringBuffer.toString();
}
/**
* 字节 转换为 字符串
* 32为位
* @param b
* @return
*/
private static String byteToHexString(byte b) {
int flag=b;
if(flag<0){
flag+=256;
}
int flag1=flag/16;
int flag2=flag%16;
return hexDigits[flag1]+hexDigits[flag2];
}
/**
* md5加密
* @param origin 目标字符串
* @param charsetname 字符编码
* @return
*/
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArraytoHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArraytoHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
}