Encrypt tool and Decrypter

This commit is contained in:
xiaoyu830411
2012-08-27 11:22:18 +08:00
parent 13fbf8b483
commit f755a614ac
7 changed files with 85 additions and 14 deletions
@@ -221,6 +221,16 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected AtomicLong createCount = new AtomicLong();
protected AtomicLong destroyCount = new AtomicLong();
protected Decrypter decrypter;
public Decrypter getDecrypter() {
return decrypter;
}
public void setDecrypter(Decrypter decrypter) {
this.decrypter = decrypter;
}
public boolean isOracle() {
return isOracle;
}
@@ -1237,6 +1247,18 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
}
Decrypter decrypter1 = this.getDecrypter();
if (decrypter1 != null) {
try {
SensitiveParameters parameters = decrypter1.decrypt(new SensitiveParameters(url, user, password));
url = parameters.getUrl();
user = parameters.getUsername();
password = parameters.getPassword();
} catch (DecryptException e) {
throw new SQLException("Failed to decrypt sensitive parameters.", e);
}
}
Properties physicalConnectProperties = new Properties();
if (connectProperties != null) {
physicalConnectProperties.putAll(connectProperties);
@@ -12,9 +12,9 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.spec.X509EncodedKeySpec;
import java.security.cert.Certificate;
/**
* RSA 解密器, 支持证书
@@ -1,6 +1,6 @@
package com.alibaba.druid.support.security.tool;
import sun.misc.BASE64Encoder;
import com.alibaba.druid.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@@ -27,6 +27,6 @@ public class AesAction extends AbstractAction {
byte[] encryptedBytes = cipher.doFinal(plainString.getBytes());
return new BASE64Encoder().encode(encryptedBytes);
return Base64.byteArrayToBase64(encryptedBytes);
}
}
@@ -1,6 +1,6 @@
package com.alibaba.druid.support.security.tool;
import sun.misc.BASE64Encoder;
import com.alibaba.druid.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
@@ -32,6 +32,6 @@ public class DesAction extends AbstractAction {
byte[] encryptedBytes = cipher.doFinal(plainString.getBytes());
return new BASE64Encoder().encode(encryptedBytes);
return Base64.byteArrayToBase64(encryptedBytes);
}
}
@@ -1,6 +1,6 @@
package com.alibaba.druid.support.security.tool;
import sun.misc.BASE64Encoder;
import com.alibaba.druid.util.Base64;
import javax.crypto.Cipher;
import java.io.*;
@@ -79,7 +79,7 @@ public class RsaAction implements Action {
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encryptedBytes = cipher.doFinal(plainString.getBytes());
String encryptedString = new BASE64Encoder().encode(encryptedBytes);
String encryptedString = Base64.byteArrayToBase64(encryptedBytes);;
System.out.println("请记住以下的密文, 长度为[" + encryptedString.length() + "].");
System.out.println();
@@ -128,5 +128,4 @@ public class RsaAction implements Action {
}
}
}
}