重建私钥

Reconstruct private key

我正在使用下面的代码生成非对称 public 密钥和私钥。

public static KeyPair generateRSAKkeyPair()
                throws Exception {
        SecureRandom secureRandom = new SecureRandom();
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

        keyPairGenerator.initialize(
                    2048, secureRandom);
        return keyPairGenerator
                               .generateKeyPair();
    }

public static byte[] do_RSAEncryption(
                                          String plainText,
                                          PublicKey publicKey)
                throws Exception {
        Cipher cipher = Cipher.getInstance(RSA);
        cipher.init(
                    Cipher.ENCRYPT_MODE, publicKey);

        return cipher.doFinal(
                    plainText.getBytes());
    }

public static String do_RSADecryption(
                                          byte[] cipherText,
                                          PrivateKey privateKey)
                throws Exception {
        Cipher cipher = Cipher.getInstance(RSA);

        cipher.init(Cipher.DECRYPT_MODE,
                    privateKey);
        byte[] result = cipher.doFinal(cipherText);

        return new String(result);
    }

public static PublicKey getKey(String key) {
        try {
            byte[] byteKey = Base64.getDecoder().decode(key.getBytes());
            X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
            KeyFactory kf = KeyFactory.getInstance("RSA");

            return kf.generatePublic(X509publicKey);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    
    public static PrivateKey getPrivateKey(String key) {
        try {
            byte[] byteKey = Base64.getDecoder().decode(key.getBytes());
            X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
            KeyFactory kf = KeyFactory.getInstance("RSA");

            return kf.generatePrivate(X509publicKey);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

这按预期工作。

String plainText = "This is the PlainText. I want to Encrypt using RSA.";

byte[] cipherText = do_RSAEncryption(plainText, keypair.getPublic());
String decryptedText = do_RSADecryption(cipherText, keypair.getPrivate());

现在,我将生成的 public 密钥和私钥存储在一个字符串中并尝试加密和解密,但失败了。

String base64EncodedEncryptionKey =  DatatypeConverter.printBase64Binary(keypair.getPublic().getEncoded()));
String base64EccodedDecryptionKey =  DatatypeConverter.printBase64Binary(keypair.getPrivate().getEncoded()));

PublicKey pubKey = getKey(base64EncodedEncryptionKey);

byte[] cipherText1 = do_RSAEncryption(plainText, pubKey);
System.out.println("Encrypted Text ===> "+ DatatypeConverter.printHexBinary(cipherText1));
        
PrivateKey privateKey = getPrivateKey(base64EccodedDecryptionKey);
        
String decryptedText1 = do_RSADecryption(cipherText1,privateKey);
        
System.out.println("DecryptedText ====>>> "+decryptedText1);

错误:-

java.security.spec.InvalidKeySpecException: Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys
    at java.base/sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:389)
    at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:247)
    at java.base/java.security.KeyFactory.generatePrivate(KeyFactory.java:390)
    at com.apple.ist.alloy.memsqlloader.service.Asymmetric.getPrivateKey(Asymmetric.java:96)
    at com.apple.ist.alloy.memsqlloader.service.Asymmetric.main(Asymmetric.java:135)
Exception in thread "main" java.security.InvalidKeyException: No installed provider supports this key: (null)
    at java.base/javax.crypto.Cipher.chooseProvider(Cipher.java:930)
    at java.base/javax.crypto.Cipher.init(Cipher.java:1286)
    at java.base/javax.crypto.Cipher.init(Cipher.java:1223)
    at com.apple.ist.alloy.memsqlloader.service.Asymmetric.do_RSADecryption(Asymmetric.java:68)
    at com.apple.ist.alloy.memsqlloader.service.Asymmetric.main(Asymmetric.java:137)

私钥用PKCS8EncodedKeySpec编码。你应该用那个替换 X509EncodedKeySpec