重建私钥

发布于 2025-01-24 10:57:42 字数 4244 浏览 3 评论 0原文

我使用以下代码生成不对称的公钥和私有密钥。

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

现在,我将生成的生成的公共密钥和私钥存储在字符串中,并试图加密和解密,并且失败了。

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)

I am generating Asymmetric public key and private using the code below.

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;
    }

This works as expected.

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

Now, I am storing the generated the generated public key and private key in a string and trying to encrypt and decrypt and it is failing.

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);

Error:-

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2025-01-31 10:57:43

私钥用pkcs8encodedkeyspec编码。您应该用它替换x509encodedkeyspec

Private keys are encoded with PKCS8EncodedKeySpec. You should replace X509EncodedKeySpec with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文