zoukankan      html  css  js  c++  java
  • DES加密(jdbc.properties)

    加密工具类

    package com.action;
    
    import java.security.Key;
    import java.security.SecureRandom;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    public class DESUtils {
            private static Key key;
            private static String KEY_STR = "htsc-pos-pbos";// 密钥
            private static String CHARSETNAME = "UTF-8";// 编码
            private static String ALGORITHM = "AES";// 加密类型
         
            static {
                try {
                    KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
                    SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
                    random.setSeed(KEY_STR.getBytes());
                    kgen.init(128, random);
                    
                    key = kgen.generateKey();
                    kgen = null;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
         
            /**
             * 对str进行DES加密
             * 
             * @param str
             * @return
             */
            public static String getEncryptString(String str) {
                BASE64Encoder base64encoder = new BASE64Encoder();
                try {
                    byte[] bytes = str.getBytes(CHARSETNAME);
                    Cipher cipher = Cipher.getInstance(ALGORITHM);
                    cipher.init(Cipher.ENCRYPT_MODE, key);
                    byte[] doFinal = cipher.doFinal(bytes);
                    return base64encoder.encode(doFinal);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
         
            /**
             * 对str进行DES解密
             * 
             * @param str
             * @return
             */
            public static String getDecryptString(String str) {
                BASE64Decoder base64decoder = new BASE64Decoder();
                try {
                    byte[] bytes = base64decoder.decodeBuffer(str);
                    Cipher cipher = Cipher.getInstance(ALGORITHM);
                    cipher.init(Cipher.DECRYPT_MODE, key);
                    byte[] doFinal = cipher.doFinal(bytes);
                    return new String(doFinal, CHARSETNAME);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            
            public static void main(String[] args) {
                System.out.println(getEncryptString("oracle.jdbc.driver.OracleDriver"));
            }
        }

    读取jdbc.properties的value值并解密

    package com.action;
    
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    
    import com.action.DESUtils;
    
    public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
        private String[] encryptPropNames = { "jdbc.driverClassName" ,"jdbc.url" , "jdbc.username", "jdbc.password"};
         
        protected String convertProperty(String propertyName, String propertyValue) {
            if (isEncryptProp(propertyName)) {
                String decryptValue = DESUtils.getDecryptString(propertyValue);
                return decryptValue;
            } else {
                return propertyValue;
            }
        }
     
        
        private boolean isEncryptProp(String propertyName) {
            for (String encryptpropertyName : encryptPropNames) {
                if (encryptpropertyName.equals(propertyName))
                    return true;
            }
            return false;
        }
    }
    encryptPropNames中的值对应jdbc.properties中的name。

    在spring配置文件中添加如下配置
    <bean class="com.openeap.common.web.EncryptPropertyPlaceholderConfigurer"  >
             <property name="ignoreUnresolvablePlaceholders" value="true"></property>
            <property name="locations">    
               <list>    
                   <value>classpath:jdbc.properties</value>    
               </list>    
           </property>  
    </bean>

     也可以在加密工具类中添加二进制加密

    注:要先将jdbc.properties文件中value值先加密替换原来value值。

  • 相关阅读:
    黑胶100
    界面滑动+ztree
    Linux下Java获取本机IP地址
    垂直对齐vertical-align:top
    针对jquery获取表单数据并且迭代方式
    js传递参数中文乱码
    前端开发资源库
    webpack加载器(Loaders)
    webpack基础知识点
    webpack操作基础
  • 原文地址:https://www.cnblogs.com/shenqz/p/7232544.html
Copyright © 2011-2022 走看看