zoukankan      html  css  js  c++  java
  • DES加密解密实现

     1 package com.liuhq.o2o.utils;
     2 
     3 import java.security.Key;
     4 import java.security.SecureRandom;
     5 
     6 import javax.crypto.Cipher;
     7 import javax.crypto.KeyGenerator;
     8 
     9 import sun.misc.BASE64Decoder;
    10 import sun.misc.BASE64Encoder;
    11 
    12 @SuppressWarnings("restriction")
    13 public class DESUtil {
    14 
    15     private static Key key;
    16     private static String KEY_STR = "myKey";
    17     private static String CHARSETNAME = "UTF-8";
    18     private static String ALGORITHM = "DES";
    19 
    20     static {
    21         try {
    22             // 生成DES算法对象
    23             KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
    24             // 运行SHA1安全策略
    25             SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    26             //设置上秘钥种子
    27             secureRandom.setSeed(KEY_STR.getBytes());
    28             //初始化基于SHA1的算法对象
    29             generator.init(secureRandom);
    30             // 生成秘钥对象
    31             key = generator.generateKey();
    32             generator = null;
    33         } catch (Exception e) {
    34             throw new RuntimeException(e);
    35         }
    36     }
    37     
    38     /**
    39      * 加密
    40      * @param str
    41      * @return
    42      */
    43     public static String getEncryptString(String str) {
    44         // 基于BADE64编码  接受byte[]并转化为String
    45         BASE64Encoder base64encoder = new BASE64Encoder();
    46         try {
    47             // 按UTF8编码
    48             byte[] bytes = str.getBytes(CHARSETNAME);
    49             // 获取加密对象
    50             Cipher cipher = Cipher.getInstance(ALGORITHM);
    51             // 初始化密码信息
    52             cipher.init(Cipher.ENCRYPT_MODE, key);
    53             // 加密
    54             byte[] doFinal = cipher.doFinal(bytes);
    55             // byte[] to encode好的string返回
    56             return base64encoder.encode(doFinal);
    57         } catch (Exception e) {
    58             // TODO: handle exception
    59             throw new RuntimeException(e);
    60         }
    61     }
    62 
    63     /**
    64      * 解密
    65      * @param str
    66      * @return
    67      */
    68     public static String getDecryptString(String str) {
    69         // 基于BADE64编码  接受byte[]并转化为String
    70         
    71         BASE64Decoder base64decoder = new BASE64Decoder();
    72         try {
    73             //将字符串decode成byte[]
    74             byte[] bytes = base64decoder.decodeBuffer(str);
    75             // 获取加密对象
    76             Cipher cipher = Cipher.getInstance(ALGORITHM);
    77             // 初始化密码信息
    78             cipher.init(Cipher.DECRYPT_MODE, key);
    79             //解密
    80             byte[] doFinal = cipher.doFinal(bytes);
    81             //返回解密之后的信息
    82             return new String(doFinal, CHARSETNAME);
    83         } catch (Exception e) {
    84             // TODO: handle exception
    85             throw new RuntimeException(e);
    86         }
    87     }
    88     
    89     public static void main(String[] args) {
    90         System.out.println(getEncryptString("root"));
    91         System.out.println(getEncryptString("123456"));
    92        
    93       
    94     }
    95 
    96 }

     解密jdbc.properties配置文件

    创建EncryptPropertyPlaceholderConfigurer类

    package com.liuhq.o2o.utils;
    
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    
    public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
        
         // 需要解密的数组
        private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };
    
        @Override
        protected String convertProperty(String propertyName, String propertyValue) {
            if (isEncryptProp(propertyName)) {
                String decryptValue = DESUtil.getDecryptString(propertyValue);
                return decryptValue;
            } else {
                return propertyValue;
            }
        }
    
        private boolean isEncryptProp(String propertyName) {
            for (String encryptpropertyName : encryptPropNames) {
                if (encryptpropertyName.equals(propertyName))
                    return true;
            }
            return false;
        }
    
    }

    替换<context:property-placeholder location="classpath:jdbc.properties"/>配置

    新的为

    <bean class="com.liuhq.o2o.utils.EncryptPropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
            <property name="fileEncoding" value="UTF-8" />
        </bean>
  • 相关阅读:
    Educational Codeforces Round 10 C. Foe Pairs 水题
    Educational Codeforces Round 10 B. z-sort 构造
    CDOJ 1048 Bob's vector 三分
    Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟
    第14届电子科大初赛民间盗版部分题目题解
    HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
    HDU 5653 Bomber Man wants to bomb an Array. dp
    HDU 5652 India and China Origins 二分+并查集
    HDU 5651 xiaoxin juju needs help 数学
    HDU 5650 so easy 数学
  • 原文地址:https://www.cnblogs.com/lhq1996/p/11982491.html
Copyright © 2011-2022 走看看