博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
password、文件MD5加密,passwordsha256、sha384、sha512Hex等加密
阅读量:4485 次
发布时间:2019-06-08

本文共 5068 字,大约阅读时间需要 16 分钟。

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;	}}

转载于:https://www.cnblogs.com/brucemengbm/p/7400364.html

你可能感兴趣的文章
Apache 下载+安装
查看>>
第七周总结CoreIDRAW
查看>>
Java ScriptEngine 解析js
查看>>
dubbo启动消费者报错:No provider available for the service
查看>>
IntelliJ IDEA使用教程(很全)
查看>>
4、指针
查看>>
One English Sentence
查看>>
在 Ubuntu 16.04 上安装 LEMP 环境之图文向导
查看>>
AOP面向切面编程
查看>>
android利用adb shell查看activity的栈
查看>>
【LeetCode 229】Majority Element II
查看>>
第2章 列表和元组
查看>>
14.18 InnoDB Backup and Recovery 备份和恢复:
查看>>
文件操作
查看>>
Python 文章汇总
查看>>
老男孩Python全栈开发(92天全)视频教程 自学笔记21
查看>>
ASP.NET页面传值之Server.Transfer 和Response.Direct
查看>>
git随笔
查看>>
codeforces 985C. Liebig's Barrels
查看>>
获取URL参数
查看>>