zoukankan      html  css  js  c++  java
  • password、文件MD5加密,passwordsha256、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;
    	}
    
    }
    

  • 相关阅读:
    史上最全的 Python 3 类型转换指南
    Python 3 进阶 —— print 打印和输出
    Go 标准库 —— sync.Mutex 互斥锁
    Python 判断文件/目录是否存在
    测试用例设计——如何提高测试覆盖率
    多语言的测试经验分享
    robotium教材(一):robotium环境搭建
    Android应用开发中出现appcompat-v7错误
    Android CPU使用率:top和dump cpuinfo的不同
    Kernel Time和User Time分别指什么
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7400364.html
Copyright © 2011-2022 走看看