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 {
}
}
}
}
@@ -1,11 +1,21 @@
package com.alibaba.druid.bvt.support.security;
import com.alibaba.druid.support.security.decryptor.AesDecrypterTest;
import com.alibaba.druid.support.security.decryptor.BlowfishDecrypterTest;
import com.alibaba.druid.support.security.decryptor.DesDecrypterTest;
import com.alibaba.druid.support.security.decryptor.RsaDecrypterTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Created with IntelliJ IDEA.
* User: yangkingsel
* Date: 12-8-27
* Time: 上午9:55
* To change this template use File | Settings | File Templates.
* @author Jonas Yang
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
AesDecrypterTest.class,
DesDecrypterTest.class,
RsaDecrypterTest.class,
BlowfishDecrypterTest.class
})
public class DecrypterTestSuite {
}
@@ -2,23 +2,63 @@ package com.alibaba.druid.support.security.decryptor;
import com.alibaba.druid.pool.DecryptException;
import com.alibaba.druid.pool.SensitiveParameters;
import com.alibaba.druid.util.Base64;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
/**
* @author Jonas Yang
*/
public class RsaDecrypterTest {
String publicKeyContent = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2YdTlkEw7JfRNItCosRf4QO8vOL54ZEdj0LZm" +
"tKhNFnNRTtHrLDFT44BPYqFSLVv3TOhLxJcEs31kyQhXEC5b9ozpZBvYrJDSt8QQMoYDAMLSojs+" +
"K4vTJavgbRKKads0di+DuoMmJ5g5dcbDNd1Kb4kgWhrZtUwhfSjQklaiIwIDAQAB";
String encryptedString = "Zaa9qpyB9snnBUz7UBvcaHcNfWMKEWvhnx0dbWmwxBOJ9vkeZNXjV9MEJezSzL+6yQh7aO29vD67" +
"qedtD9ublVwwf+jTPuO1NjzAKy0+yWVQ1KdR2KaQuvCxFWiM1n6nbpkUGB6OVpoQgFnFzdKlyC9D" +
"KU3Q6evGa1hlZ8jnHcA=";
String keyFilePath;
@Before
public void setUp() throws IOException {
File tmp = File.createTempFile("druid_public_key", Long.toString(System.currentTimeMillis()));
byte[] keyContent = Base64.base64ToByteArray(this.publicKeyContent);
FileOutputStream out = null;
try {
out = new FileOutputStream(tmp);
out.write(keyContent);
} finally {
if (out != null) {
out.close();
}
}
this.keyFilePath = tmp.getAbsolutePath();
}
@After
public void tearDown() {
File tmp = new File(this.keyFilePath);
if (tmp.exists()) {
tmp.delete();
}
this.keyFilePath = null;
}
@Test
public void testDecrypt() throws DecryptException {
RsaDecrypter decrypter = new RsaDecrypter();
decrypter.setPublicKeyFile("/Users/yangkingsel/IdeaProjects/HelloWorld/out/production/HelloWorld/a.public.key");
decrypter.setPublicKeyFile(this.keyFilePath);
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", encryptedString));
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
}
}