关于配置文件内容加密处理的感想
2015-01-02 14:05
507 查看
前几天,公司提了一个需求,及让我们将数据库及服务器的路径做加密处理。这对于才接触工作的我来说,着实让我头疼半天。说实话,我一开始完全没想过加密问题,直到上次让我用3DES加密技术,对用户名和密码等做加密处理时稍微了解了一下。先说下3DES,这是对称加密算法,所谓对称加密,我的理解就是,加密解密通用一个密匙。
以下是3DES对字符串的处理代码:
public class ThreeDES
{
private static Key key;
/**
* 根据参数生成KEY
*
* @param strKey
*/
public static void getKey(String strKey)
{
try
{
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(strKey.getBytes()));
key = generator.generateKey();
generator = null;
} catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 加密String明文输入,String密文输出
*
* @param strMing
* @return
*/
public static String getEncString(String strMing)
{
String strMi = "";
try
{
return byte2hex(getEncCode(strMing.getBytes()));
} catch (Exception e)
{
e.printStackTrace();
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
*
* @param strMi
* @return
*/
public static String getDesString(String strMi)
{
String strMing = "";
try
{
return new String(getDesCode(hex2byte(strMi.getBytes())));
} catch (Exception e)
{
e.printStackTrace();
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS
* @return
*/
private static byte[] getEncCode(byte[] byteS)
{
byte[] byteFina = null;
Cipher cipher;
try
{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e)
{
e.printStackTrace();
} finally
{
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD
* @return
*/
private static byte[] getDesCode(byte[] byteD)
{
Cipher cipher;
byte[] byteFina = null;
try
{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e)
{
e.printStackTrace();
} finally
{
cipher = null;
}
return byteFina;
}
/**
* 二行制转字符串
*
* @param b
* @return
*/
public static String byte2hex(byte[] b)
{ // 一个字节的数,
// 转成16进制字符串
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
// 整数转成十六进制表示
stmp = (java.lang.Integer.toHexString(b
& 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase(); // 转成大写
}
public static byte[] hex2byte(byte[] b)
{
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2)
{
String item = new String(b, n, 2);
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
这是我查到的可以用于字符串之间的转换的方法。
起初我找到的是将其转换成字节数组然后根据16进制密匙处理得到的新的字节数组,但这个方法有一个缺点就是转换回原来的字符串必须得传字节数组,而我们将打乱后的字符串转成字节数组后,则一直报错,后来查原因发现是因为转码前后的字节数组长度不一样。因此和我们的需求相形甚远。
以下是3DES对字符串的处理代码:
public class ThreeDES
{
private static Key key;
/**
* 根据参数生成KEY
*
* @param strKey
*/
public static void getKey(String strKey)
{
try
{
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(strKey.getBytes()));
key = generator.generateKey();
generator = null;
} catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 加密String明文输入,String密文输出
*
* @param strMing
* @return
*/
public static String getEncString(String strMing)
{
String strMi = "";
try
{
return byte2hex(getEncCode(strMing.getBytes()));
} catch (Exception e)
{
e.printStackTrace();
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
*
* @param strMi
* @return
*/
public static String getDesString(String strMi)
{
String strMing = "";
try
{
return new String(getDesCode(hex2byte(strMi.getBytes())));
} catch (Exception e)
{
e.printStackTrace();
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS
* @return
*/
private static byte[] getEncCode(byte[] byteS)
{
byte[] byteFina = null;
Cipher cipher;
try
{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e)
{
e.printStackTrace();
} finally
{
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD
* @return
*/
private static byte[] getDesCode(byte[] byteD)
{
Cipher cipher;
byte[] byteFina = null;
try
{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e)
{
e.printStackTrace();
} finally
{
cipher = null;
}
return byteFina;
}
/**
* 二行制转字符串
*
* @param b
* @return
*/
public static String byte2hex(byte[] b)
{ // 一个字节的数,
// 转成16进制字符串
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
// 整数转成十六进制表示
stmp = (java.lang.Integer.toHexString(b
& 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase(); // 转成大写
}
public static byte[] hex2byte(byte[] b)
{
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2)
{
String item = new String(b, n, 2);
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
这是我查到的可以用于字符串之间的转换的方法。
起初我找到的是将其转换成字节数组然后根据16进制密匙处理得到的新的字节数组,但这个方法有一个缺点就是转换回原来的字符串必须得传字节数组,而我们将打乱后的字符串转成字节数组后,则一直报错,后来查原因发现是因为转码前后的字节数组长度不一样。因此和我们的需求相形甚远。
相关文章推荐
- 关于配置文件内容加密处理的感想(二)
- 在spring-config文件中对jdbc数据配置进行加密处理
- WCF 第一章 基础 更多关于配置文件的内容
- 采用DES加密方式对properties配置文件敏感信息加密处理
- IIS配置asp.net网站出现错误:请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
- Spring加载Properties配置文件的加密解密处理
- 关于Tomcat的点点滴滴(体系架构、处理http请求的过程、安装和配置、文件夹结构、设置压缩和对中文文件名称的支持、以及Catalina这个名字的由来……等)
- 关于ajax调用一般处理文件,传值内容过多后无法进入一般处理文件的问题
- IIS配置asp.net网站出现错误:请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
- 配置scut协议生成器的时候需要配置iis,出现常见错误——Win7中IIS出现“HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。
- 关于如何使用properties读取配置文件内容问题
- 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理解决方案 服务器配置错误
- 【脚本语言系列】关于Python结构化文本文件处理配置文件.ini,你需要知道的事
- 第一章 基础 (续 更多关于配置文件的内容)
- WCF基础 (续 更多关于配置文件的内容)
- WebConfig配置 文件加密处理
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- AP-关于付款界面中的’付款处理配置文件’的说明
- 关于SSH框架整合中,数据库配置文件加密解决思路记载
- 关于java读取和写入properties配置文件的内容