diff --git a/src/main/java/com/alibaba/druid/filter/config/ConfigTools.java b/src/main/java/com/alibaba/druid/filter/config/ConfigTools.java index daa16a0da8..c61fa33900 100644 --- a/src/main/java/com/alibaba/druid/filter/config/ConfigTools.java +++ b/src/main/java/com/alibaba/druid/filter/config/ConfigTools.java @@ -17,6 +17,8 @@ package com.alibaba.druid.filter.config; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; +import java.security.InvalidKeyException; +import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; @@ -26,7 +28,10 @@ import java.security.PublicKey; import java.security.SecureRandom; import java.security.cert.Certificate; import java.security.cert.CertificateFactory; +import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.RSAPrivateCrtKeySpec; +import java.security.spec.RSAPrivateKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; @@ -36,146 +41,161 @@ import com.alibaba.druid.util.JdbcUtils; public class ConfigTools { - private static final String DEFAULT_PRIVATE_KEY_STRING = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAocbCrurZGbC5GArEHKlAfDSZi7gFBnd4yxOt0rwTqKBFzGyhtQLu5PRKjEiOXVa95aeIIBJ6OhC2f8FjqFUpawIDAQABAkAPejKaBYHrwUqUEEOe8lpnB6lBAsQIUFnQI/vXU4MV+MhIzW0BLVZCiarIQqUXeOhThVWXKFt8GxCykrrUsQ6BAiEA4vMVxEHBovz1di3aozzFvSMdsjTcYRRo82hS5Ru2/OECIQC2fAPoXixVTVY7bNMeuxCP4954ZkXp7fEPDINCjcQDywIgcc8XLkkPcs3Jxk7uYofaXaPbg39wuJpEmzPIxi3k0OECIGubmdpOnin3HuCP/bbjbJLNNoUdGiEmFL5hDI4UdwAdAiEAtcAwbm08bKN7pwwvyqaCBC//VnEWaq39DCzxr+Z2EIk="; - public static final String DEFAULT_PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKHGwq7q2RmwuRgKxBypQHw0mYu4BQZ3eMsTrdK8E6igRcxsobUC7uT0SoxIjl1WveWniCASejoQtn/BY6hVKWsCAwEAAQ=="; + private static final String DEFAULT_PRIVATE_KEY_STRING = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAocbCrurZGbC5GArEHKlAfDSZi7gFBnd4yxOt0rwTqKBFzGyhtQLu5PRKjEiOXVa95aeIIBJ6OhC2f8FjqFUpawIDAQABAkAPejKaBYHrwUqUEEOe8lpnB6lBAsQIUFnQI/vXU4MV+MhIzW0BLVZCiarIQqUXeOhThVWXKFt8GxCykrrUsQ6BAiEA4vMVxEHBovz1di3aozzFvSMdsjTcYRRo82hS5Ru2/OECIQC2fAPoXixVTVY7bNMeuxCP4954ZkXp7fEPDINCjcQDywIgcc8XLkkPcs3Jxk7uYofaXaPbg39wuJpEmzPIxi3k0OECIGubmdpOnin3HuCP/bbjbJLNNoUdGiEmFL5hDI4UdwAdAiEAtcAwbm08bKN7pwwvyqaCBC//VnEWaq39DCzxr+Z2EIk="; + public static final String DEFAULT_PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKHGwq7q2RmwuRgKxBypQHw0mYu4BQZ3eMsTrdK8E6igRcxsobUC7uT0SoxIjl1WveWniCASejoQtn/BY6hVKWsCAwEAAQ=="; - public static void main(String[] args) throws Exception { - String password = args[0]; - System.out.println(encrypt(password)); - } + public static void main(String[] args) throws Exception { + String password = args[0]; + System.out.println(encrypt(password)); + } - public static String decrypt(String cipherText) throws Exception { - return decrypt((String) null, cipherText); - } + public static String decrypt(String cipherText) throws Exception { + return decrypt((String) null, cipherText); + } - public static String decrypt(String publicKeyText, String cipherText) throws Exception { - PublicKey publicKey = getPublicKey(publicKeyText); + public static String decrypt(String publicKeyText, String cipherText) + throws Exception { + PublicKey publicKey = getPublicKey(publicKeyText); - return decrypt(publicKey, cipherText); - } + return decrypt(publicKey, cipherText); + } - public static PublicKey getPublicKeyByX509(String x509File) { - if (x509File == null || x509File.length() == 0) { - return ConfigTools.getPublicKey(null); - } + public static PublicKey getPublicKeyByX509(String x509File) { + if (x509File == null || x509File.length() == 0) { + return ConfigTools.getPublicKey(null); + } - FileInputStream in = null; - try { - in = new FileInputStream(x509File); + FileInputStream in = null; + try { + in = new FileInputStream(x509File); - CertificateFactory factory = CertificateFactory.getInstance("X.509"); - Certificate cer = factory.generateCertificate(in); - return cer.getPublicKey(); - } catch (Exception e) { - throw new IllegalArgumentException("Failed to get public key", e); - } finally { - JdbcUtils.close(in); - } - } + CertificateFactory factory = CertificateFactory + .getInstance("X.509"); + Certificate cer = factory.generateCertificate(in); + return cer.getPublicKey(); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to get public key", e); + } finally { + JdbcUtils.close(in); + } + } - public static PublicKey getPublicKey(String publicKeyText) { - if (publicKeyText == null || publicKeyText.length() == 0) { - publicKeyText = ConfigTools.DEFAULT_PUBLIC_KEY_STRING; - } + public static PublicKey getPublicKey(String publicKeyText) { + if (publicKeyText == null || publicKeyText.length() == 0) { + publicKeyText = ConfigTools.DEFAULT_PUBLIC_KEY_STRING; + } - try { - byte[] publicKeyBytes = Base64.base64ToByteArray(publicKeyText); - X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes); + try { + byte[] publicKeyBytes = Base64.base64ToByteArray(publicKeyText); + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec( + publicKeyBytes); - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - return keyFactory.generatePublic(x509KeySpec); - } catch (Exception e) { - throw new IllegalArgumentException("Failed to get public key", e); - } - } + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + return keyFactory.generatePublic(x509KeySpec); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to get public key", e); + } + } - public static PublicKey getPublicKeyByPublicKeyFile(String publicKeyFile) { - if (publicKeyFile == null || publicKeyFile.length() == 0) { - return ConfigTools.getPublicKey(null); - } + public static PublicKey getPublicKeyByPublicKeyFile(String publicKeyFile) { + if (publicKeyFile == null || publicKeyFile.length() == 0) { + return ConfigTools.getPublicKey(null); + } - FileInputStream in = null; - try { - in = new FileInputStream(publicKeyFile); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - int len = 0; - byte[] b = new byte[512 / 8]; - while ((len = in.read(b)) != -1) { - out.write(b, 0, len); - } + FileInputStream in = null; + try { + in = new FileInputStream(publicKeyFile); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int len = 0; + byte[] b = new byte[512 / 8]; + while ((len = in.read(b)) != -1) { + out.write(b, 0, len); + } - byte[] publicKeyBytes = out.toByteArray(); - X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes); - KeyFactory factory = KeyFactory.getInstance("RSA"); - return factory.generatePublic(spec); - } catch (Exception e) { - throw new IllegalArgumentException("Failed to get public key", e); - } finally { - JdbcUtils.close(in); - } - } + byte[] publicKeyBytes = out.toByteArray(); + X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes); + KeyFactory factory = KeyFactory.getInstance("RSA"); + return factory.generatePublic(spec); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to get public key", e); + } finally { + JdbcUtils.close(in); + } + } - public static String decrypt(PublicKey publicKey, String cipherText) throws Exception { - Cipher cipher = Cipher.getInstance("RSA"); - cipher.init(Cipher.DECRYPT_MODE, publicKey); + public static String decrypt(PublicKey publicKey, String cipherText) + throws Exception { + Cipher cipher = Cipher.getInstance("RSA"); + try { + cipher.init(Cipher.DECRYPT_MODE, publicKey); + } catch (InvalidKeyException e) { + // for ibm jdk + RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; + RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent()); + Key fakePublicKey = KeyFactory.getInstance("RSA").generatePrivate(spec); + cipher.init(Cipher.DECRYPT_MODE, fakePublicKey); + } + + if (cipherText == null || cipherText.length() == 0) { + return cipherText; + } - if (cipherText == null || cipherText.length() == 0) { - return cipherText; - } + byte[] cipherBytes = Base64.base64ToByteArray(cipherText); + byte[] plainBytes = cipher.doFinal(cipherBytes); - byte[] cipherBytes = Base64.base64ToByteArray(cipherText); - byte[] plainBytes = cipher.doFinal(cipherBytes); + return new String(plainBytes); + } - return new String(plainBytes); - } + public static String encrypt(String plainText) throws Exception { + return encrypt((String) null, plainText); + } - public static String encrypt(String plainText) throws Exception { - return encrypt((String) null, plainText); - } + public static String encrypt(String key, String plainText) throws Exception { + if (key == null) { + key = DEFAULT_PRIVATE_KEY_STRING; + } - public static String encrypt(String key, String plainText) throws Exception { - if (key == null) { - key = DEFAULT_PRIVATE_KEY_STRING; - } + byte[] keyBytes = Base64.base64ToByteArray(key); + return encrypt(keyBytes, plainText); + } - byte[] keyBytes = Base64.base64ToByteArray(key); - return encrypt(keyBytes, plainText); - } + public static String encrypt(byte[] keyBytes, String plainText) + throws Exception { + PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); + KeyFactory factory = KeyFactory.getInstance("RSA"); + PrivateKey privateKey = factory.generatePrivate(spec); + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, privateKey); - public static String encrypt(byte[] keyBytes, String plainText) throws Exception { - PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); - KeyFactory factory = KeyFactory.getInstance("RSA"); - PrivateKey privateKey = factory.generatePrivate(spec); - Cipher cipher = Cipher.getInstance("RSA"); - cipher.init(Cipher.ENCRYPT_MODE, privateKey); + byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8")); + String encryptedString = Base64.byteArrayToBase64(encryptedBytes); - byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8")); - String encryptedString = Base64.byteArrayToBase64(encryptedBytes); + return encryptedString; + } - return encryptedString; - } + public static byte[][] genKeyPairBytes(int keySize) + throws NoSuchAlgorithmException { + byte[][] keyPairBytes = new byte[2][]; - public static byte[][] genKeyPairBytes(int keySize) throws NoSuchAlgorithmException { - byte[][] keyPairBytes = new byte[2][]; + KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); + gen.initialize(keySize, new SecureRandom()); + KeyPair pair = gen.generateKeyPair(); - KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); - gen.initialize(keySize, new SecureRandom()); - KeyPair pair = gen.generateKeyPair(); + keyPairBytes[0] = pair.getPrivate().getEncoded(); + keyPairBytes[1] = pair.getPublic().getEncoded(); - keyPairBytes[0] = pair.getPrivate().getEncoded(); - keyPairBytes[1] = pair.getPublic().getEncoded(); + return keyPairBytes; + } - return keyPairBytes; - } + public static String[] genKeyPair(int keySize) + throws NoSuchAlgorithmException { + byte[][] keyPairBytes = genKeyPairBytes(keySize); + String[] keyPairs = new String[2]; - public static String[] genKeyPair(int keySize) throws NoSuchAlgorithmException { - byte[][] keyPairBytes = genKeyPairBytes(keySize); - String[] keyPairs = new String[2]; + keyPairs[0] = Base64.byteArrayToBase64(keyPairBytes[0]); + keyPairs[1] = Base64.byteArrayToBase64(keyPairBytes[1]); - keyPairs[0] = Base64.byteArrayToBase64(keyPairBytes[0]); - keyPairs[1] = Base64.byteArrayToBase64(keyPairBytes[1]); - - return keyPairs; - } + return keyPairs; + } }