原作者博客:http://renjie120.iteye.com/
原文出处:http://renjie120.iteye.com/blog/717116
package com.lsframe.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Base64 {
private static sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
private static sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
/**
* base64加密字符串.
*
* @param oldStr
* @return
*/
public static String encode(String oldStr) {
return encoder.encode(oldStr.getBytes());
}
/**
* base64解密字符串.
* @param oldStr
* @return
* @throws IOException
*/
public static String decode(String oldStr) throws IOException {
return new String(decoder.decodeBuffer(oldStr));
}
/**
* base64编码输入流.
* @param inputStream 输入流
* @param outputStream 输出流
* @throws IOException
*/
public static void encode(InputStream inputStream, OutputStream outputStream)
throws IOException {
encoder.encode(inputStream, outputStream);
inputStream.close();
outputStream.close();
}
/**
* base64解密输入流.
* @param inputStream
* @param outputStream
* @throws IOException
*/
public static void decode(InputStream inputStream, OutputStream outputStream)
throws IOException {
decoder.decodeBuffer(inputStream, outputStream);
inputStream.close();
outputStream.close();
}
/**
* base64加密文件.
* @param inFileName 源文件
* @param outFileName 新的文件
* @throws IOException
*/
public static void encode(String inFileName,String outFileName) throws IOException{
File oldFile = new File(inFileName);
File newFile = new File(outFileName);
InputStream input = new BufferedInputStream(
new FileInputStream(oldFile));
OutputStream out = new BufferedOutputStream(
new FileOutputStream(newFile));
Base64.encode(input, out);
}
/**
* base64解密文件.
* @param inFileName 源文件
* @param outFileName 新的文件
* @throws IOException
*/
public static void decode(String inFileName,String outFileName) throws IOException{
File oldFile = new File(inFileName);
File newFile = new File(outFileName);
InputStream input = new BufferedInputStream(
new FileInputStream(oldFile));
OutputStream out = new BufferedOutputStream(
new FileOutputStream(newFile));
Base64.decode(input, out);
}
public static void main(String[] a) throws IOException {
Base64.encode("E:\\workplace\\testMyFrame\\bb.mp3","d:\\hah.base64");
Base64.decode("d:\\hah.base64","d:\\ddd.mp3");
System.out.println("ok");
}
}
2.MD5的加密方法:
public class MD5 {
public String getMD5(byte[] source) {
String s = null;
char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f' };
try {
java.security.MessageDigest md = java.security.MessageDigest
.getInstance("MD5");
md.update(source);
// MD5 的计算结果是一个 128 位的长整数,
byte tmp[] = md.digest();
// 用字节表示就是 16 个字节
char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符,
// 所以表示成 16 进制需要 32 个字符
int k = 0; // 表示转换结果中对应的字符位置
for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节
// 转换成 16 进制字符的转换
byte byte0 = tmp[i]; // 取第 i 个字节
str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
// >>> 为逻辑右移,将符号位一起右移
str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
}
s = new String(str); // 换后的结果转换为字符串
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
public static void main(String[] args) {
MD5 md5 = new MD5();
System.out.println(md5.getMD5("ABC".getBytes()));
System.out.println(md5.getMD5("ABC".getBytes()));
}
}