转自:http://www.javaeye.com/topic/70663如何给工程中的配置文件加密 解密
之前有人问过hibernate密码问题,大家都没有给出一个具体的解决方案,所以我就看了一下代码,把我的想法和实现拿出来和大家讨论一下。 我现在的环境是spring+hibernate,但是这并不影响这个加密解密的问题,其他环境应该是略有不同,但是思路肯定是一样的。 总体思路:在工程的配置文件中填写数据库密码的密文,在应用程序使用datasource的时候解密成明文以创建连接。
步骤1
使用java的中cipher类并使用DES(对称加密算法)算法对明文进行加密
````````````````这里如何使用cipher类和DES算法的原理可以上网查找,我懒得写了,如果大家真的也怕麻烦自己去找的话我再写一个贴出来吧 修改:我随便写了一个类,大家看着改吧,我没有测试过Java代码
public class DESUtil {
public static void main(String[] args){
try {
if(args[0].equals("-genKey")){
generateKey(args[1]);
}else{
//if (args[0].equals("-encrypt"))encrypt();
//else decrypt();
}
}catch (Exception e) {
// TODO: handle exception
}
}
public static String encrypt(String plainText, String encryptString, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile));
int mode = Cipher.ENCRYPT_MODE;
Key key = (Key)keyIn.readObject();
keyIn.close();
InputStream in = new FileInputStream(plainText);
OutputStream out = new FileOutputStream(encryptString);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
doEncryptAndDecrypt(in, out, cipher);
String result = out.toString();
System.out.print(result);
in.close();
out.close();
return result;
}
public static String decrypt(String encryptString, String plainText, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile));
int mode = Cipher.DECRYPT_MODE;
Key key = (Key)keyIn.readObject();
keyIn.close();
InputStream in = new FileInputStream(encryptString);
OutputStream out = new FileOutputStream(plainText);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
doEncryptAndDecrypt(in, out, cipher);
String result = out.toString();
System.out.print(result);
in.close();
out.close();
return result;
}
public static void doEncryptAndDecrypt(InputStream in, OutputStream out, Cipher cipher)throws IOException, GeneralSecurityException{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while(more){
inLength = in.read(inBytes);
if(inLength == blockSize){
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes,0,outLength);
}
else more = false;
}
if(inLength>0) outBytes = cipher.doFinal(inBytes, 0, inLength);
else outBytes = cipher.doFinal();
out.write(outBytes);
}
public static void generateKey(String path) throws Exception{
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(path));
out.writeObject(key);
out.close();
}
} public class DESUtil {
public static void main(String[] args){
try {
if(args[0].equals("-genKey")){
generateKey(args[1]);
}else{
//if (args[0].equals("-encrypt"))encrypt();
//else decrypt();
}
}catch (Exception e) {
// TODO: handle exception
}
}
public static String encrypt(String plainText, String encryptString, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile));
int mode = Cipher.ENCRYPT_MODE;
Key key = (Key)keyIn.readObject();
keyIn.close();
InputStream in = new FileInputStream(plainText);
OutputStream out = new FileOutputStream(encryptString);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
doEncryptAndDecrypt(in, out, cipher);
String result = out.toString();
System.out.print(result);
in.close();
out.close();
return result;
}
public static String decrypt(String encryptString, String plainText, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile));
int mode = Cipher.DECRYPT_MODE;
Key key = (Key)keyIn.readObject();
keyIn.close();
InputStream in = new FileInputStream(encryptString);
OutputStream out = new FileOutputStream(plainText);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
doEncryptAndDecrypt(in, out, cipher);
String result = out.toString();
System.out.print(result);
in.close();
out.close();
return result;
}
public static void doEncryptAndDecrypt(InputStream in, OutputStream out, Cipher cipher)throws IOException, GeneralSecurityException{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while(more){
inLength = in.read(inBytes);
if(inLength == blockSize){
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes,0,outLength);
}
else more = false;
}
if(inLength>0) outBytes = cipher.doFinal(inBytes, 0, inLength);
else outBytes = cipher.doFinal();
out.write(outBytes);
}
public static void generateKey(String path) throws Exception{
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(path));
out.writeObject(key);
out.close();
}}
通过以上的encrypt方法得到一个密码的密文(一般的密码是明文,作为参数传进去可以得到对应的密文),比如说21sadf25
之前有人问过hibernate密码问题,大家都没有给出一个具体的解决方案,所以我就看了一下代码,把我的想法和实现拿出来和大家讨论一下。 我现在的环境是spring+hibernate,但是这并不影响这个加密解密的问题,其他环境应该是略有不同,但是思路肯定是一样的。 总体思路:在工程的配置文件中填写数据库密码的密文,在应用程序使用datasource的时候解密成明文以创建连接。
步骤1
使用java的中cipher类并使用DES(对称加密算法)算法对明文进行加密
````````````````这里如何使用cipher类和DES算法的原理可以上网查找,我懒得写了,如果大家真的也怕麻烦自己去找的话我再写一个贴出来吧 修改:我随便写了一个类,大家看着改吧,我没有测试过Java代码
public class DESUtil {
public static void main(String[] args){
try {
if(args[0].equals("-genKey")){
generateKey(args[1]);
}else{
//if (args[0].equals("-encrypt"))encrypt();
//else decrypt();
}
}catch (Exception e) {
// TODO: handle exception
}
}
public static String encrypt(String plainText, String encryptString, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile));
int mode = Cipher.ENCRYPT_MODE;
Key key = (Key)keyIn.readObject();
keyIn.close();
InputStream in = new FileInputStream(plainText);
OutputStream out = new FileOutputStream(encryptString);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
doEncryptAndDecrypt(in, out, cipher);
String result = out.toString();
System.out.print(result);
in.close();
out.close();
return result;
}
public static String decrypt(String encryptString, String plainText, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile));
int mode = Cipher.DECRYPT_MODE;
Key key = (Key)keyIn.readObject();
keyIn.close();
InputStream in = new FileInputStream(encryptString);
OutputStream out = new FileOutputStream(plainText);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
doEncryptAndDecrypt(in, out, cipher);
String result = out.toString();
System.out.print(result);
in.close();
out.close();
return result;
}
public static void doEncryptAndDecrypt(InputStream in, OutputStream out, Cipher cipher)throws IOException, GeneralSecurityException{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while(more){
inLength = in.read(inBytes);
if(inLength == blockSize){
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes,0,outLength);
}
else more = false;
}
if(inLength>0) outBytes = cipher.doFinal(inBytes, 0, inLength);
else outBytes = cipher.doFinal();
out.write(outBytes);
}
public static void generateKey(String path) throws Exception{
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(path));
out.writeObject(key);
out.close();
}
} public class DESUtil {
public static void main(String[] args){
try {
if(args[0].equals("-genKey")){
generateKey(args[1]);
}else{
//if (args[0].equals("-encrypt"))encrypt();
//else decrypt();
}
}catch (Exception e) {
// TODO: handle exception
}
}
public static String encrypt(String plainText, String encryptString, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile));
int mode = Cipher.ENCRYPT_MODE;
Key key = (Key)keyIn.readObject();
keyIn.close();
InputStream in = new FileInputStream(plainText);
OutputStream out = new FileOutputStream(encryptString);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
doEncryptAndDecrypt(in, out, cipher);
String result = out.toString();
System.out.print(result);
in.close();
out.close();
return result;
}
public static String decrypt(String encryptString, String plainText, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException{
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile));
int mode = Cipher.DECRYPT_MODE;
Key key = (Key)keyIn.readObject();
keyIn.close();
InputStream in = new FileInputStream(encryptString);
OutputStream out = new FileOutputStream(plainText);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
doEncryptAndDecrypt(in, out, cipher);
String result = out.toString();
System.out.print(result);
in.close();
out.close();
return result;
}
public static void doEncryptAndDecrypt(InputStream in, OutputStream out, Cipher cipher)throws IOException, GeneralSecurityException{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while(more){
inLength = in.read(inBytes);
if(inLength == blockSize){
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes,0,outLength);
}
else more = false;
}
if(inLength>0) outBytes = cipher.doFinal(inBytes, 0, inLength);
else outBytes = cipher.doFinal();
out.write(outBytes);
}
public static void generateKey(String path) throws Exception{
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(path));
out.writeObject(key);
out.close();
}}
通过以上的encrypt方法得到一个密码的密文(一般的密码是明文,作为参数传进去可以得到对应的密文),比如说21sadf25
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货