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

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

使用时请先导入commons-codec-1.5.jar包

[java]  view plain copy
  1. package com.pdsu.crm.utils;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.OutputStream;  
  6. import java.security.MessageDigest;  
  7. import java.security.NoSuchAlgorithmException;  
  8.   
  9. import org.apache.commons.codec.binary.Base64OutputStream;  
  10. import org.apache.commons.codec.digest.DigestUtils;  
  11.   
  12. /** 
  13.  * 类说明:密码的加密与解密 
  14.  *  
  15.  * @author 作者: LiuJunGuang 
  16.  * @version 创建时间:2011-5-19 下午11:07:09 
  17.  */  
  18. public class PasswordEncode {  
  19.     public final static String MD5 = "MD5";  
  20.     public final static String NONE = "NONE";  
  21.     public final static String SHA_256 = "SHA-256";  
  22.     public final static String SHA_512 = "SHA-512";  
  23.     public final static String SHA_384 = "SHA-384";  
  24.   
  25.     /** 
  26.      * 加密文件算法 
  27.      *  
  28.      * @param filename 
  29.      *            需要加密的文件名 
  30.      * @param algorithm 
  31.      *            加密算法名 
  32.      */  
  33.     public static void digestFile(String filename, String algorithm) {  
  34.         byte[] b = new byte[1024 * 4];  
  35.         int len = 0;  
  36.         FileInputStream fis = null;  
  37.         FileOutputStream fos = null;  
  38.         try {  
  39.             MessageDigest md = MessageDigest.getInstance(algorithm);  
  40.             fis = new FileInputStream(filename);  
  41.             while ((len = fis.read(b)) != -1) {  
  42.                 md.update(b, 0, len);  
  43.             }  
  44.             byte[] digest = md.digest();  
  45.             StringBuffer fileNameBuffer = new StringBuffer(128).append(filename).append(".").append(algorithm);  
  46.             fos = new FileOutputStream(fileNameBuffer.toString());  
  47.             OutputStream encodedStream = new Base64OutputStream(fos);  
  48.             encodedStream.write(digest);  
  49.             encodedStream.flush();  
  50.             encodedStream.close();  
  51.         } catch (Exception e) {  
  52.             System.out.println("Error computing Digest: " + e);  
  53.         } finally {  
  54.             try {  
  55.                 if (fis != null)  
  56.                     fis.close();  
  57.             } catch (Exception ignored) {  
  58.             }  
  59.             try {  
  60.                 if (fos != null)  
  61.                     fos.close();  
  62.             } catch (Exception ignored) {  
  63.             }  
  64.         }  
  65.     }  
  66.   
  67.     /** 
  68.      * 加密密码算法 
  69.      *  
  70.      * @param pass 
  71.      *            需要加密的原始密码 
  72.      * @param algorithm 
  73.      *            加密算法名称 
  74.      * @return 加密后的密码 
  75.      * @throws NoSuchAlgorithmException 
  76.      *             当加密算法不可用时抛出此异常 
  77.      */  
  78.     public static String digestString(String password, String alg) throws NoSuchAlgorithmException {  
  79.         String newPass;  
  80.         if (alg == null || MD5.equals(alg)) {  
  81.             newPass = DigestUtils.md5Hex(password);  
  82.         } else if (NONE.equals(alg)) {  
  83.             newPass = password;  
  84.         } else if (SHA_256.equals(alg)) {  
  85.             newPass = DigestUtils.sha256Hex(password);  
  86.         } else if (SHA_384.equals(alg)) {  
  87.             newPass = DigestUtils.sha384Hex(password);  
  88.         } else if (SHA_512.equals(alg)) {  
  89.             newPass = DigestUtils.sha512Hex(password);  
  90.         } else {  
  91.             newPass = DigestUtils.shaHex(password);  
  92.         }  
  93.         return newPass;  
  94.     }  
  95.   
  96.     /** 
  97.      * 加密密码算法,默认的加密算法是MD5 
  98.      *  
  99.      * @param password 
  100.      *            未加密的密码 
  101.      * @return String 加密后的密码 
  102.      */  
  103.     public static String digestPassword(String password) {  
  104.         try {  
  105.             if (password != null && !"".equals(password)) {  
  106.                 return digestString(password, MD5);  
  107.             } else  
  108.                 return null;  
  109.         } catch (NoSuchAlgorithmException nsae) {  
  110.             throw new RuntimeException("Security error: " + nsae);  
  111.         }  
  112.     }  
  113.   
  114.     /** 
  115.      * 判断密码是不是相等,默认的加密算法是MD5 
  116.      *  
  117.      * @param beforePwd 
  118.      *            要判断的密码 
  119.      * @param afterPwd 
  120.      *            加密后的数据库密码 
  121.      * @return Boolean true 密码相等 
  122.      */  
  123.     public static boolean isPasswordEnable(String beforePwd, String afterPwd) {  
  124.         if (beforePwd != null && !"".equals(beforePwd)) {  
  125.             String password = digestPassword(beforePwd);  
  126.             return afterPwd.equals(password);  
  127.         } else  
  128.             return false;  
  129.     }  
  130.   
  131.     public static void main(String[] args) throws NoSuchAlgorithmException {  
  132.         System.out.println(PasswordEncode.digestPassword("123456"));  
  133.         System.out.println(PasswordEncode.digestString("123456", PasswordEncode.MD5));  
  134.         PasswordEncode.digestFile("C:\\Users\\user\\Desktop\\PasswordEncode.java", PasswordEncode.SHA_512);  
  135.         System.out.println(PasswordEncode.isPasswordEnable("123456", PasswordEncode.digestPassword("123456")));  
  136.     }  
  137. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值