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
@@ -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());
}
}