Spring 管理下的web项目,对Properties文件重要参数的加密解密处理(续)

之前有篇文章是对应properties文件加密的,但是上面的加密算法生成得是byte字节码。所以今天给大家重新写个类。

Endecrypt.java

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class Endecrypt
{
    /**
     * 进行MD5加密
     */
    private byte[] md5(String strSrc)
    {
        byte[] returnByte = null;
        try
        {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            returnByte = md5.digest(strSrc.getBytes("UTF-8"));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return returnByte;
    }

    /**
     * 得到3-DES的密钥匙 根据接口规范,密钥匙为24个字节,md5加密出来的是16个字节,因此后面补8个字节的0
     */
    private byte[] getEnKey(String spKey)
    {
        byte[] desKey = null;
        try
        {
            byte[] desKey1 = md5(spKey);
            desKey = new byte[24];
            int i = 0;
            while (i < desKey1.length && i < 24)
            {
                desKey[i] = desKey1[i];
                i++;
            }
            if(i < 24)
            {
                desKey[i] = 0;
                i++;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return desKey;
    }

    /**
     * 3-DES加密
     */
    public byte[] Encrypt(byte[] src, byte[] enKey)
    {
        byte[] encryptedData = null;
        try
        {
            DESedeKeySpec dks = new DESedeKeySpec(enKey);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey key = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DESede");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            encryptedData = cipher.doFinal(src);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return encryptedData;
    }

    /**
     * 对字符串进行Base64编码
     */
    public String getBase64Encode(byte[] src)
    {
        String requestValue = "";
        try
        {
            BASE64Encoder base64en = new BASE64Encoder();
            requestValue = base64en.encode(src);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return requestValue;
    }

    /**
     * 去掉字符串的换行符号 base64编码3-DES的数据时,得到的字符串有换行符号
     * ,一定要去掉,否则uni-wise平台解析票根不会成功,提示“sp验证失败”。
     */
    private String filter(String str)
    {
        String output = null;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++)
        {
            int asc = str.charAt(i);
            if(asc != 10 && asc != 13)
                sb.append(str.subSequence(i, i + 1));
        }
        output = new String(sb);
        return output;
    }

    /**
     * 对字符串进行URLDecoder.encode(strEncoding)编码
     */
    @SuppressWarnings("deprecation")
    public String getURLEncode(String src)
    {
        String requestValue = "";
        try
        {
            requestValue = URLEncoder.encode(src);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return requestValue;
    }

    /**
     * 3-DES加密
     */
    public String get3DESEncrypt(String src, String spkey)
    {
        String requestValue = "";
        try
        {
            // 得到3-DES的密钥匙
            byte[] enKey = getEnKey(spkey);
            // 要进行3-DES加密的内容在进行/"UTF-16LE/"取字节
            byte[] src2 = src.getBytes("UTF-16LE");
            // 进行3-DES加密后的内容的字节
            byte[] encryptedData = Encrypt(src2, enKey);

            // 进行3-DES加密后的内容进行BASE64编码
            String base64String = getBase64Encode(encryptedData);
            // BASE64编码去除换行符后
            String base64Encrypt = filter(base64String);

            // 对BASE64编码中的HTML控制码进行转义的过程
            requestValue = getURLEncode(base64Encrypt);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return requestValue;
    }

    /**
     * 对字符串进行URLDecoder.decode(strEncoding)解码
     */
    @SuppressWarnings("deprecation")
    public String getURLDecoderdecode(String src)
    {
        String requestValue = "";
        try
        {
            requestValue = URLDecoder.decode(src);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return requestValue;
    }

    /**
     * 进行3-DES解密(密钥匙等同于加密的密钥匙)。
     */
    public String deCrypt(byte[] debase64, String spKey)
    {
        String strDe = null;
        Cipher cipher = null;
        try
        {
            cipher = Cipher.getInstance("DESede");
            byte[] key = getEnKey(spKey);
            DESedeKeySpec dks = new DESedeKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey sKey = keyFactory.generateSecret(dks);
            cipher.init(Cipher.DECRYPT_MODE, sKey);
            byte ciphertext[] = cipher.doFinal(debase64);
            strDe = new String(ciphertext, "UTF-16LE");
        }
        catch (Exception ex)
        {
            strDe = "";
            ex.printStackTrace();
        }
        return strDe;
    }

    /**
     * 3-DES解密
     */
    public String get3DESDecrypt(String src, String spkey)
    {
        String requestValue = "";
        try
        {
            String URLValue = getURLDecoderdecode(src);
            BASE64Decoder base64Decode = new BASE64Decoder();
            byte[] base64DValue = base64Decode.decodeBuffer(URLValue);
            requestValue = deCrypt(base64DValue, spkey);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return requestValue;
    }

}

DecryptPropertyPlaceholderConfigurer.java

package com.rrcp.mgmt.config;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.rrcp.mgmt.util.Endecrypt;

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer
{
    static String[] propertiesArray =
    { "jdbc_url", "jdbc_username", "jdbc_password" };;

    static String key = "keystr";

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException
    {
        Endecrypt des = new Endecrypt();
        for (String str : propertiesArray)
        {
            if(props.getProperty(str) != null)
            {
                String strDes = des.get3DESDecrypt(props.getProperty(str), key);
                props.setProperty(str, strDes);
            }
        }
        super.processProperties(beanFactory, props);
    }

}

.xml文件配置参考之前的文章。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值