对信息的加密是用的DES(具体加密方法的选用,可自行选择)
 

  1. public class DESUtil {      
  2.      public static final String KEY_STRING = "farawayfrom";//生成密钥的字符串  
  3.      static Key key;       
  4.        
  5.      /**     
  6.       * 根据参数生成KEY     
  7.       *      
  8.       * @param strKey     
  9.       */      
  10.      public static void getKey(String strKey) {       
  11.          try {       
  12.              KeyGenerator _generator = KeyGenerator.getInstance("DES");       
  13.              _generator.init(new SecureRandom(strKey.getBytes()));       
  14.              key = _generator.generateKey();     
  15.              _generator = null;       
  16.          } catch (Exception e) {       
  17.              e.printStackTrace();       
  18.          }       
  19.      }       
  20.        
  21.      /**     
  22.       * 加密String明文输入,String密文输出     
  23.       *      
  24.       * @param strMing     
  25.       * @return     
  26.       */      
  27.      public static String getEncString(String strMing) {    
  28.          DESUtil.getKey(KEY_STRING);// 生成密匙       
  29.          byte[] byteMi = null;       
  30.          byte[] byteMing = null;       
  31.          String strMi = "";       
  32.          BASE64Encoder base64en = new BASE64Encoder();       
  33.          try {       
  34.              byteMing = strMing.getBytes("UTF8");       
  35.              byteMi = getEncCode(byteMing);       
  36.              strMi = base64en.encode(byteMi);       
  37.          } catch (Exception e) {       
  38.              e.printStackTrace();       
  39.          } finally {       
  40.              base64en = null;       
  41.              byteMing = null;       
  42.              byteMi = null;       
  43.          }       
  44.          return strMi;       
  45.      }       
  46.        
  47.      /**     
  48.       * 解密 以String密文输入,String明文输出     
  49.       *      
  50.       * @param strMi     
  51.       * @return     
  52.       */      
  53.      public static String getDesString(String strMi) {   
  54.          DESUtil.getKey(KEY_STRING);// 生成密匙       
  55.          BASE64Decoder base64De = new BASE64Decoder();       
  56.          byte[] byteMing = null;       
  57.          byte[] byteMi = null;       
  58.          String strMing = "";       
  59.          try {       
  60.              byteMi = base64De.decodeBuffer(strMi);       
  61.              byteMing = getDesCode(byteMi);       
  62.              strMing = new String(byteMing, "UTF8");       
  63.          } catch (Exception e) {       
  64.              e.printStackTrace();       
  65.          } finally {       
  66.              base64De = null;       
  67.              byteMing = null;       
  68.              byteMi = null;       
  69.          }       
  70.          return strMing;       
  71.      }       
  72.        
  73.      /**     
  74.       * 加密以byte[]明文输入,byte[]密文输出     
  75.       *      
  76.       * @param byteS     
  77.       * @return     
  78.       */      
  79.      private static byte[] getEncCode(byte[] byteS) {       
  80.          byte[] byteFina = null;       
  81.          Cipher cipher;       
  82.          try {       
  83.              cipher = Cipher.getInstance("DES");       
  84.              cipher.init(Cipher.ENCRYPT_MODE, key);       
  85.              byteFina = cipher.doFinal(byteS);       
  86.          } catch (Exception e) {       
  87.              e.printStackTrace();       
  88.          } finally {       
  89.              cipher = null;       
  90.          }       
  91.          return byteFina;       
  92.      }       
  93.        
  94.      /**     
  95.       * 解密以byte[]密文输入,以byte[]明文输出     
  96.       *      
  97.       * @param byteD     
  98.       * @return     
  99.       */      
  100.      private static byte[] getDesCode(byte[] byteD) {       
  101.          Cipher cipher;       
  102.          byte[] byteFina = null;       
  103.          try {       
  104.              cipher = Cipher.getInstance("DES");       
  105.              cipher.init(Cipher.DECRYPT_MODE, key);       
  106.              byteFina = cipher.doFinal(byteD);       
  107.          } catch (Exception e) {       
  108.              e.printStackTrace();       
  109.          } finally {       
  110.              cipher = null;       
  111.          }       
  112.          return byteFina;       
  113.        
  114.      }       
  115.        
  116.      public static void main(String[] args) {       
  117.           
  118.          String strEnc = DESUtil.getEncString("yanfa");// 加密字符串,返回String的密文       
  119.          System.out.println(strEnc);       
  120.        
  121.          String strDes = DESUtil.getDesString("kSElf8soueY=");// 把String 类型的密文解密        
  122.          System.out.println(strDes);       
  123.      }       
  124.        
  125.  }      
  126.  

这里的密钥字符串已经在类中写死,如果觉得不安全可以自己将密钥用MD5加密后写到文件中使用。


spring配置文件部分内容如下:(注意红色的地方正是解密的类)

 

  1. <!-- 属性文件读入 --> 
  2.     <bean id="propertyConfigurer" 
  3.         [color=red]class="com.dt.util.MyConfigurer"[/color]> 
  4.         <property name="locations"> 
  5.             <list> 
  6.                 <value>classpath:conf/jdbc.properties</value> 
  7.             </list> 
  8.         </property> 
  9.     </bean> 

解密类如下: 

  1. public class MyConfigurer extends PropertyPlaceholderConfigurer  
  2.  
  3. {  
  4.  
  5.     @Override  
  6.     protected void processProperties(  
  7.             ConfigurableListableBeanFactory beanFactory, Properties props)  
  8.             throws BeansException {  
  9.  
  10.         System.out.println("MyConfigurer!");  
  11.         String UserName = props.getProperty("jdbc.UserName");  
  12.         if (UserName != null ) {  
  13.             props.setProperty("jdbc.UserName", DESUtil.getDesString(UserName));  
  14.         }  
  15.         String password = props.getProperty("jdbc.PassWord");  
  16.         if (password != null ) {  
  17.             props.setProperty("jdbc.PassWord", DESUtil.getDesString(password));  
  18.         }  
  19.         super.processProperties(beanFactory, props);  
  20.  
  21.     }  
  22. }  
  23.