Skip to content

Instantly share code, notes, and snippets.

@AravindaM
Forked from destan/ParseRSAKeys.java
Created March 16, 2021 05:33
Show Gist options
  • Save AravindaM/eb4e70ff94e2c1106084b6f51b2a5f71 to your computer and use it in GitHub Desktop.
Save AravindaM/eb4e70ff94e2c1106084b6f51b2a5f71 to your computer and use it in GitHub Desktop.

Revisions

  1. @destan destan revised this gist Oct 1, 2017. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    ## Troubleshooting

    Error:

    ```
    unable to write 'random state'
    e is 65537 (0x10001)
    ```

    solution:

    `sudo rm ~/.rnd`

    source: https://stackoverflow.com/a/94458/878361
  2. @destan destan created this gist May 18, 2016.
    39 changes: 39 additions & 0 deletions ParseRSAKeys.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    import java.io.IOException;
    import java.net.URISyntaxException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.security.KeyFactory;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import java.util.Base64;

    /**
    * This file is intended to be used on a IDE for testing purposes.
    * ClassLoader.getSystemResource won't work in a JAR
    */
    public class Main {

    public static void main(String[] args) throws InvalidKeySpecException, NoSuchAlgorithmException, IOException, URISyntaxException {

    String privateKeyContent = new String(Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("private_key_pkcs8.pem").toURI())));
    String publicKeyContent = new String(Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("public_key.pem").toURI())));

    privateKeyContent = privateKeyContent.replaceAll("\\n", "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
    publicKeyContent = publicKeyContent.replaceAll("\\n", "").replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "");;

    KeyFactory kf = KeyFactory.getInstance("RSA");

    PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyContent));
    PrivateKey privKey = kf.generatePrivate(keySpecPKCS8);

    X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyContent));
    RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(keySpecX509);

    System.out.println(privKey);
    System.out.println(pubKey);
    }
    }
    7 changes: 7 additions & 0 deletions generateKeyPair.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/usr/bin/env bash

    openssl genrsa -out private_key.pem 4096
    openssl rsa -pubout -in private_key.pem -out public_key.pem

    # convert private key to pkcs8 format in order to import it from Java
    openssl pkcs8 -topk8 -in private_key.pem -inform pem -out private_key_pkcs8.pem -outform pem -nocrypt