baiy / Ctool

程序开发常用工具 chrome / edge / firefox / utools / windows / linux / mac
https://ctool.dev
MIT License
1.44k stars 202 forks source link

提供对Druid 数据库密码的加解密支持 #305

Open poc9999 opened 7 months ago

poc9999 commented 7 months ago

ConfigTools

public static String decrypt(String publicKeyText, String cipherText) throws Exception { PublicKey publicKey = getPublicKey(publicKeyText);

    return decrypt(publicKey, cipherText);

}


```java
public static String decrypt(PublicKey publicKey, String cipherText)
            throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        try {
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
        } catch (InvalidKeyException e) {
            // 因为 IBM JDK 不支持私钥加密, 公钥解密, 所以要反转公私钥
            // 也就是说对于解密, 可以通过公钥的参数伪造一个私钥对象欺骗 IBM JDK
            RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
            RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
            Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
            cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one.
            cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
        }
poc9999 commented 7 months ago

这个是RSA 加解密。但是解密的时候使用的公钥 而不是私钥