mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Encrypt tool and Decrypter
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user