MD5.SHA.BASE64算法加解密

package com.wpf.wz;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

public class Base64Demo {

	private static byte[] decryptBASE64(String key) throws IOException {
		return (new BASE64Decoder()).decodeBuffer(key);   
	}
	
	private static String encryptBASE64(byte[] key){
		return (new BASE64Encoder()).encodeBuffer(key);
	}
	
	private static byte[] encryptSHA(byte[] key) throws NoSuchAlgorithmException{
		MessageDigest sha = MessageDigest.getInstance("SHA");
		sha.update(key);
		
		return sha.digest();
	}
	
	private static byte[] encryptMD5(byte[] key) throws NoSuchAlgorithmException{
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		md5.update(key);
		return md5.digest();
	}
	public static void main(String[] args) throws NoSuchAlgorithmException {
		try {
			byte[] bytes = "简单加密".getBytes();

			String en = encryptBASE64(bytes);
			System.out.println("BASE64 加密" + en);
			byte[] out = decryptBASE64(en);
			System.out.println("BASE64解密" + new String(out));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(new BigInteger(encryptSHA("SHA简单加密".getBytes())).toString(32));
		System.out.println(new BigInteger(encryptMD5("MD5简单加密".getBytes())).toString(16));
	}
}


测试结果:

BASE64 加密vPK1pbzTw9w=

BASE64解密简单加密
eah5asjrlct2dp9l1mk9qeabi8ut9kbp
-1c32af8f7100e533911c105cfba7840e


 

你可能感兴趣的:(加密,解密,md5,算法,base64,java)