一、DES加密算法介绍
1、要求密钥必须是8个字节,即64bit长度。
2、因为密钥是byte[8] , 代表字符串也可以是非可见的字节,可以与Base64编码算法一起使用。
3、加密、解密都需要通过字节数组作为数据和密钥进行处理。
二、对称加密
DES加密算法属于对称加密。
即利用指定的密钥,按照密码的长度截取数据,分成数据块,和密钥进行复杂的移位、算数运算或者数据处理等操作,形成只有特
定的密码才能够解开的数据。 加密与解密用的是同一个密钥。
前些日子用过一次,在这里记录一下:
1、在spring-common中配置取的config,然后编辑变量名。
<bean id="propertyConfigurer"
class="com.tgb.fan.listener.EncryptablePropertyPlaceholderConfigurer">
<property name="location" value="classpath:config.properties" />
</bean>
2、添加EncryptablePropertyPlaceholderConfigurer类
package com.tgb.fan.listener;
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.tgb.fan.util.EncrypUtil;
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
private static final String c_username="jdbc.user";
private static final String c_jdbcUrl="jdbc.jdbcUrl";
private static final String c_driverClass="jdbc.driverClass";
private static final String c_password="jdbc.password";
private static final String c_dialect="jdbc.dialect";
@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
String username=props.getProperty(c_username);
if(null !=username && username.length()>6){
props.setProperty(c_username, EncrypUtil.decrypt(username));
}
String password=props.getProperty(c_password);
if(null !=password && password.length()>6){
props.setProperty(c_password, EncrypUtil.decrypt(password));
}
String jdbcUrl=props.getProperty(c_jdbcUrl);
//if(null != jdbcUrl && jdbcUrl.length() > 6){
// props.setProperty(c_jdbcUrl, EncrypUtil.decrypt(jdbcUrl));
//}
String driverClass=props.getProperty(c_driverClass);
//if(null !=driverClass && driverClass.length() > 6){
// props.setProperty(c_driverClass, EncrypUtil.decrypt(driverClass));
//}
String dialect=props.getProperty(c_dialect);
//if(null != dialect && dialect.length() > 6){
// props.setProperty(c_dialect, EncrypUtil.decrypt(dialect));
//}
super.processProperties(beanFactoryToProcess, props);
}
}
3、添加EncrypUtil类
package com.tgb.fan.util;
import org.apache.log4j.Logger;
public class EncrypUtil {
private static Logger logger = Logger.getLogger(EncrypUtil.class);
public static String encrypt(String src){
String res=src;
try {
res=DESUtil.desCrypto(src);
} catch (Exception e) {
logger.info("desCrypto error",e);
}
return res;
}
public static String decrypt(String src){
String res=src;
try {
res=DESUtil.decrypt(src);
} catch (Exception e) {
logger.info("decrypt error",e);
}
return res;
}
}
4、添加DESUtil类
package com.tgb.fan.util;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.binary.Hex;
import com.tgb.exception.WxyhException;
public class DESUtil {
private final static String DES_KEY = PropsUtil.getProperty("DES_KEY");
public static String desCrypto(String datasource) throws WxyhException {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(DES_KEY.getBytes());
//创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
//Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
//现在,获取数据并加密
//正式执行加密操作
return new String(Hex.encodeHex(cipher.doFinal(datasource.getBytes())));
} catch (Exception e) {
throw new WxyhException("加密失败");
}
}
public static String decrypt(String src) throws WxyhException{
try {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKey = new DESKeySpec(DES_KEY.getBytes());
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正开始解密操作
return new String(cipher.doFinal(Hex.decodeHex(src.toCharArray())));
} catch (Exception e) {
throw new WxyhException("解密失败");
}
}
/*public static void main(String[] args) throws WxyhException {
String src="";
System.out.println(desCrypto(src));
}*/
}
可以在DESUtil类中编译要加密的内容,获取密码(放开main方法注释,在src中填写)
5、在config.properties配置文件中配置密文内容和DES_KEY
jdbc.driverClass=数据库类型
jdbc.jdbcUrl=数据库地址名称
jdbc.user=用户名
jdbc.password=密码
jdbc.dialect=数据库语言
DES_KEY=azxHw3Jx0QqpeomrVROrMmmN
这样就大功告成啦~
望大家关注,批评指正~