首页 > 代码库 > 密码、文件MD5加密,密码sha256、sha384、sha512Hex等加密

密码、文件MD5加密,密码sha256、sha384、sha512Hex等加密

package encryption;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Arrays;

import org.apache.commons.codec.binary.Base64OutputStream;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * 加密工具类
 * @author wangzg
 * @Date 2014-7-25 
 * @Need commons-codec-*.*.jar
 */
public class Encryption {

	/**
	 * @Title: main
	 * @Description:
	 * @param:
	 * @return void 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static void main(String[] args) {
        String strSource = "admin";
        
        String str = getMd5(strSource);
        System.out.println(strSource+",length:"+str.length()+",md5:"+str);
        str = getSha1(strSource);
        System.out.println(strSource+",length:"+str.length()+",Sha1:"+str);
        str = getSha256(strSource);
        System.out.println(strSource+",length:"+str.length()+",Sha256:"+str);
        str = getSha384(strSource);
        System.out.println(strSource+",length:"+str.length()+",Sha384:"+str);
        str = getSha512(strSource);
        System.out.println(strSource+",length:"+str.length()+",Sha512:"+str);
        
        digestFile("doc\\1.txt",MessageDigestAlgorithms.MD5);
         
        String target = getBase64(strSource);
        System.out.println(strSource);
        System.out.println(target);
        System.out.println(getFromBase64(target));
        
	}
	
	/**
	 * MD5加密,返回32位十六进制字符串
	 * @Title: getMD5
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static String getMd5(String source){
		return DigestUtils.md5Hex(source);
	}
	
	/**
	 * sha1加密,返回40位十六进制字符串
	 * @Title: getSha1
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static String getSha1(String source){
		return DigestUtils.sha1Hex(source);
	}
	
	/**
	 * sha256加密,返回64位十六进制字符串
	 * @Title: getSha256
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static String getSha256(String source){
		return DigestUtils.sha256Hex(source);
	}
	
	/**
	 * sha384加密,返回96位十六进制字符串
	 * @Title: getSha384
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static String getSha384(String source){
		return DigestUtils.sha384Hex(source);
	}
	
	/**
	 * sha512加密,返回128位十六进制字符串
	 * @Title: getSha512
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static String getSha512(String source){
		return DigestUtils.sha512Hex(source);
	}
	
	/**
	 * 文件加密
	 * @Title: digestFile
	 * @Description:
	 * @param:filename:加密文件,algorithm:加密算法
	 * @return void 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static void digestFile(String filename, String algorithm) {  
	        byte[] b = new byte[1024 * 4];  
	        int len = 0;  
	        FileInputStream fis = null;  
	        FileOutputStream fos = null;  
	        try {  
	            MessageDigest md = MessageDigest.getInstance(algorithm);  
	            fis = new FileInputStream(filename);  
	            while ((len = fis.read(b)) != -1) {  
	                md.update(b, 0, len);  
	            }  
	            byte[] digest = md.digest(); 
	            //System.out.println(Arrays.toString(digest));
	            StringBuffer fileNameBuffer = new StringBuffer(128).append(filename).append(".").append(algorithm);  
	            fos = new FileOutputStream(fileNameBuffer.toString());  
	            OutputStream encodedStream = new Base64OutputStream(fos);  
	            encodedStream.write(digest);  
	            encodedStream.flush();  
	            encodedStream.close(); 
	        } catch (Exception e) {  
	            System.out.println("Error computing Digest: " + e);  
	        } finally {  
	            try {  
	                if (fis != null)  
	                    fis.close();  
	                if (fos != null)  
	                    fos.close();  
	            } catch (Exception ignored) {  
	            }  
	        }  
	    }  

	/**
	 * Base64加密
	 * @Title: getBase64
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static String getBase64(String str) {
		byte[] b = null;
		String s = null;
		try {
			b = str.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (b != null) {
			s = new BASE64Encoder().encode(b);
		}
		return s;
	}

	/**
	 * Base64解密
	 * @Title: getFromBase64
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-25
	 * @throws
	 */
	public static String getFromBase64(String s) {
		byte[] b = null;
		String result = null;
		if (s != null) {
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				b = decoder.decodeBuffer(s);
				result = new String(b, "utf-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

}