应用
/** * 利用MD5进行加密 * * @param str 待加密的字符串 * @return 加密后的字符串 * @throws NoSuchAlgorithmException 没有这种产生消息摘要的算法 * @throws UnsupportedEncodingException */ public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {// 确定计算方法 MessageDigest md5 = MessageDigest.getInstance("MD5"); BASE64Encoder base64en = new BASE64Encoder(); // 加密后的字符串 String newstr = base64en.encode(md5.digest(str.getBytes("utf-8"))); return newstr; }
两个工具类搞定
import java.io.IOException; import java.io.OutputStream; public class BASE64Encoder extends CharacterEncoder {private static final char[] pem_array = 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', '+', '/'}; public BASE64Encoder() {}protected int bytesPerAtom() {return 3; }protected int bytesPerLine() {return 57; }protected void encodeAtom(OutputStream outStream, byte[] data, int offset, int len) throws IOException {byte a; if (len == 1) {a = data[offset]; byte b = 0; outStream.write(pem_array[a >>> 2 & 63]); outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]); outStream.write(61); outStream.write(61); } else {byte b; if (len == 2) {a = data[offset]; b = data[offset + 1]; byte c = 0; outStream.write(pem_array[a >>> 2 & 63]); outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]); outStream.write(pem_array[(b << 2 & 60) + (c >>> 6 & 3)]); outStream.write(61); } else {a = data[offset]; b = data[offset + 1]; byte c = data[offset + 2]; outStream.write(pem_array[a >>> 2 & 63]); outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]); outStream.write(pem_array[(b << 2 & 60) + (c >>> 6 & 3)]); outStream.write(pem_array[c & 63]); }}} }
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.nio.ByteBuffer; public abstract class CharacterEncoder {protected PrintStream pStream; public CharacterEncoder() {}protected abstract int bytesPerAtom(); protected abstract int bytesPerLine(); protected void encodeBufferPrefix(OutputStream aStream) throws IOException {this.pStream = new PrintStream(aStream); }protected void encodeBufferSuffix(OutputStream aStream) throws IOException {}protected void encodeLinePrefix(OutputStream aStream, int aLength) throws IOException {}protected void encodeLineSuffix(OutputStream aStream) throws IOException {this.pStream.println(); }protected abstract void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException; protected int readFully(InputStream in, byte[] buffer) throws IOException {for(int i = 0; i < buffer.length; ++i) {int q = in.read(); if (q == -1) {return i; }buffer[i] = (byte)q; }return buffer.length; }public void encode(InputStream inStream, OutputStream outStream) throws IOException {byte[] tmpbuffer = new byte[this.bytesPerLine()]; this.encodeBufferPrefix(outStream); while(true) {int numBytes = this.readFully(inStream, tmpbuffer); if (numBytes == 0) {break; }this.encodeLinePrefix(outStream, numBytes); for(int j = 0; j < numBytes; j += this.bytesPerAtom()) {if (j + this.bytesPerAtom() <= numBytes) {this.encodeAtom(outStream, tmpbuffer, j, this.bytesPerAtom()); } else {this.encodeAtom(outStream, tmpbuffer, j, numBytes - j); }}if (numBytes < this.bytesPerLine()) {break; }this.encodeLineSuffix(outStream); }this.encodeBufferSuffix(outStream); }public void encode(byte[] aBuffer, OutputStream aStream) throws IOException {ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer); this.encode((InputStream)inStream, aStream); }public String encode(byte[] aBuffer) {ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer); String retVal = null; try {this.encode((InputStream)inStream, outStream); retVal = outStream.toString("8859_1"); return retVal; } catch (Exception var6) {throw new Error("CharacterEncoder.encode internal error"); }}private byte[] getBytes(ByteBuffer bb) {byte[] buf = null; if (bb.hasArray()) {byte[] tmp = bb.array(); if (tmp.length == bb.capacity() && tmp.length == bb.remaining()) {buf = tmp; bb.position(bb.limit()); }}if (buf == null) {buf = new byte[bb.remaining()]; bb.get(buf); }return buf; }public void encode(ByteBuffer aBuffer, OutputStream aStream) throws IOException {byte[] buf = this.getBytes(aBuffer); this.encode(buf, aStream); }public String encode(ByteBuffer aBuffer) {byte[] buf = this.getBytes(aBuffer); return this.encode(buf); }public void encodeBuffer(InputStream inStream, OutputStream outStream) throws IOException {byte[] tmpbuffer = new byte[this.bytesPerLine()]; this.encodeBufferPrefix(outStream); int numBytes; do {numBytes = this.readFully(inStream, tmpbuffer); if (numBytes == 0) {break; }this.encodeLinePrefix(outStream, numBytes); for(int j = 0; j < numBytes; j += this.bytesPerAtom()) {if (j + this.bytesPerAtom() <= numBytes) {this.encodeAtom(outStream, tmpbuffer, j, this.bytesPerAtom()); } else {this.encodeAtom(outStream, tmpbuffer, j, numBytes - j); }}this.encodeLineSuffix(outStream); } while(numBytes >= this.bytesPerLine()); this.encodeBufferSuffix(outStream); }public void encodeBuffer(byte[] aBuffer, OutputStream aStream) throws IOException {ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer); this.encodeBuffer((InputStream)inStream, aStream); }public String encodeBuffer(byte[] aBuffer) {ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer); try {this.encodeBuffer((InputStream)inStream, outStream); } catch (Exception var5) {throw new Error("CharacterEncoder.encodeBuffer internal error"); }return outStream.toString(); }public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream) throws IOException {byte[] buf = this.getBytes(aBuffer); this.encodeBuffer(buf, aStream); }public String encodeBuffer(ByteBuffer aBuffer) {byte[] buf = this.getBytes(aBuffer); return this.encodeBuffer(buf); } }