Java對字符串加密並返回星號※

 If you don't look back, you'll never know I waiting for you behind you.前端

 

Java對字符串加密並返回星號※java

PasswordUtils這個加密工具類是在Ranger項目的源碼中發現的,它是一個安全管理框架,普通的加密需求應該用它的加密工具類就OK了;spring

首先,用戶輸入密碼,前端先用type爲password把密碼顯示爲※,可是這時經過F12查看,瀏覽器仍然能夠看到密碼信息,可是這是用戶本身輸入的,第一把看見也ok;一旦請求提交馬上返回經加密後的密碼,此處並不是返回加密後的密碼,而是直接返回一個※密碼如「******」,並把轉換加密後的密碼存入數據庫,以後每次請求也都返回「******」;而後在後臺須要用到密碼的地方就本身解密咯。數據庫

加密工具類PasswordUtils:apache

 1 package org.apache.ranger.plugin.util;  2 
 3 import java.io.IOException;  4 import java.util.Map;  5 
 6 import javax.crypto.Cipher;  7 import javax.crypto.SecretKey;  8 import javax.crypto.SecretKeyFactory;  9 import javax.crypto.spec.PBEKeySpec;  10 import javax.crypto.spec.PBEParameterSpec;  11 
 12 import org.apache.commons.lang.StringUtils;  13 import org.slf4j.Logger;  14 import org.slf4j.LoggerFactory;  15 
 16 import com.sun.jersey.core.util.Base64;  17 public class PasswordUtils {  18 
 19     private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class);  20 
 21     private final String CRYPT_ALGO;  22     private String password;  23     private final char[] ENCRYPT_KEY;  24     private final byte[] SALT;  25     private final int ITERATION_COUNT;  26     private final char[] encryptKey;  27     private final byte[] salt;  28     private static final String LEN_SEPARATOR_STR = ":";  29 
 30     public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES";  31     public static final String DEFAULT_ENCRYPT_KEY = "tzL1AKl5uc4NKYaoQ4P3WLGIBFPXWPWdu1fRm9004jtQiV";  32     public static final String DEFAULT_SALT = "f77aLYLo";  33     public static final int DEFAULT_ITERATION_COUNT = 17;  34 
 35     public static String encryptPassword(String aPassword) throws IOException {  36         return new PasswordUtils(aPassword).encrypt();  37  }  38 
 39     private String encrypt() throws IOException {  40         String ret = null;  41         String strToEncrypt = null;  42         if (password == null) {  43             strToEncrypt = "";  44         } else {  45             strToEncrypt = password.length() + LEN_SEPARATOR_STR + password;  46  }  47         try {  48             Cipher engine = Cipher.getInstance(CRYPT_ALGO);  49             PBEKeySpec keySpec = new PBEKeySpec(encryptKey);  50             SecretKeyFactory skf = SecretKeyFactory.getInstance(CRYPT_ALGO);  51             SecretKey key = skf.generateSecret(keySpec);  52             engine.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, ITERATION_COUNT));  53             byte[] encryptedStr = engine.doFinal(strToEncrypt.getBytes());  54             ret = new String(Base64.encode(encryptedStr));  55  }  56         catch(Throwable t) {  57             LOG.error("Unable to encrypt password due to error", t);  58             throw new IOException("Unable to encrypt password due to error", t);  59  }  60         return ret;  61  }  62 
 63  PasswordUtils(String aPassword) {  64             String[] crypt_algo_array = null;  65             int count = 0;  66             if (aPassword != null && aPassword.contains(",")) {  67                 count = StringUtils.countMatches(aPassword, ",");  68                 crypt_algo_array = aPassword.split(",");  69  }  70             if (crypt_algo_array != null && crypt_algo_array.length > 4) {  71                 CRYPT_ALGO = crypt_algo_array[0];  72                 ENCRYPT_KEY = crypt_algo_array[1].toCharArray();  73                 SALT = crypt_algo_array[2].getBytes();  74                 ITERATION_COUNT = Integer.parseInt(crypt_algo_array[3]);  75                 password = crypt_algo_array[4];  76                 if (count > 4) {  77                     for (int i = 5 ; i<=count ; i++){  78                         password = password + "," + crypt_algo_array[i];  79  }  80  }  81             } else {  82                     CRYPT_ALGO = DEFAULT_CRYPT_ALGO;  83                     ENCRYPT_KEY = DEFAULT_ENCRYPT_KEY.toCharArray();  84                     SALT = DEFAULT_SALT.getBytes();  85                     ITERATION_COUNT = DEFAULT_ITERATION_COUNT;  86                     password = aPassword;  87  }  88             Map<String, String> env = System.getenv();  89             String encryptKeyStr = env.get("ENCRYPT_KEY");  90             if (encryptKeyStr == null) {  91                 encryptKey=ENCRYPT_KEY;  92             }else{  93                 encryptKey=encryptKeyStr.toCharArray();  94  }  95             String saltStr = env.get("ENCRYPT_SALT");  96             if (saltStr == null) {  97                 salt = SALT;  98             }else{  99                 salt=saltStr.getBytes(); 100  } 101  } 102 
103     public static String decryptPassword(String aPassword) throws IOException { 104         return new PasswordUtils(aPassword).decrypt(); 105  } 106 
107     private String decrypt() throws IOException { 108         String ret = null; 109         try { 110             byte[] decodedPassword = Base64.decode(password); 111             Cipher engine = Cipher.getInstance(CRYPT_ALGO); 112             PBEKeySpec keySpec = new PBEKeySpec(encryptKey); 113             SecretKeyFactory skf = SecretKeyFactory.getInstance(CRYPT_ALGO); 114             SecretKey key = skf.generateSecret(keySpec); 115             engine.init(Cipher.DECRYPT_MODE, key,new PBEParameterSpec(salt, ITERATION_COUNT)); 116             String decrypted = new String(engine.doFinal(decodedPassword)); 117             int foundAt = decrypted.indexOf(LEN_SEPARATOR_STR); 118             if (foundAt > -1) { 119                 if (decrypted.length() > foundAt) { 120                     ret = decrypted.substring(foundAt+1); 121  } 122                 else { 123                     ret = ""; 124  } 125  } 126             else { 127                 ret = null; 128  } 129  } 130         catch(Throwable t) { 131             LOG.error("Unable to decrypt password due to error", t); 132             throw new IOException("Unable to decrypt password due to error", t); 133  } 134         return ret; 135  } 136 
137     public static String getDecryptPassword(String password) { 138         String decryptedPwd = null; 139         try { 140             decryptedPwd = decryptPassword(password); 141         } catch (Exception ex) { 142             LOG.warn("Password decryption failed, trying original password string."); 143             decryptedPwd = null; 144         } finally { 145             if (decryptedPwd == null) { 146                 decryptedPwd = password; 147  } 148  } 149         return decryptedPwd; 150  } 151 }

測試加密/解密執行結果:瀏覽器

 1 package com.xinyan.springcloud.tjt;  2 
 3 public class TestDecryptEncrypt {  4     
 5     public static void main(String[] args) throws Exception {  6         String password = "taojietaoge";  7         //加密:
 8         String encryptPassword = PasswordUtils.encryptPassword(password);  9         System.out.println("加密後:"+ encryptPassword); 10         //解密:
11         String decryptPassword = PasswordUtils.decryptPassword(encryptPassword); 12         System.out.println("解密後:"+ decryptPassword); 13  } 14 
15 }

執行結果以下:安全

相關文章
相關標籤/搜索