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值。

  • 相关阅读:
    Java8之Optional用法举例
    Java多线程之ThreadPoolTaskExecutor用法
    Java多线程之ExecutorService使用说明
    CountDownLatch同步计数器使用说明
    读取excel文件内容 (hutool-poi)
    字符串工具-StrUtil(hutool)
    IDEA 常用插件
    在 Gerrit 仓库中创建空分支
    Linux idea 输入中文出现下划线乱码
    ArchLinux 修改 MariaDB 数据库路径后启动报错 Can't create test file /xxxxx/xxxxx-test
  • 原文地址:https://www.cnblogs.com/shenqz/p/7232544.html
Copyright © 2011-2022 走看看