Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:11:25+00:00 2026-05-27T13:11:25+00:00

I have my Private and Public keys in a String in base64 which where

  • 0

I have my Private and Public keys in a String in base64 which where encoded using ANS1 DER. I tried creating the instance of a java PrivateKey and PublicKey:

byte [] llave2 = DatatypeConverter.parseBase64Binary(key);
PKCS8Key pkcs8 = new PKCS8Key( llave2, password.toCharArray()); //line 2
llave2 = pkcs8.getDecryptedBytes();                             //line 3
certificado = DatatypeConverter.parseBase64Binary(cer);

KeyFactory kf = KeyFactory.getInstance("RSA");  
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(llave2);
PrivateKey privateKey = kf.generatePrivate(ks);
X509EncodedKeySpec x = new X509EncodedKeySpec(certificado);
PublicKey publicKey = kf.generatePublic(x);

I get the following error in PublicKey publicKey = kf.generatePublic(x).

    java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException:     IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
    at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(Unknown Source)
    at java.security.KeyFactory.generatePublic(Unknown Source)
    at vital.cancelaciones.GeneraXMLCancelacion.main(GeneraXMLCancelacion.java:118)
Caused by: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
    at sun.security.x509.X509Key.decode(Unknown Source)
    at sun.security.x509.X509Key.decode(Unknown Source)
    at sun.security.rsa.RSAPublicKeyImpl.<init>(Unknown Source)
    at sun.security.rsa.RSAKeyFactory.generatePublic(Unknown Source)
    ... 3 more

I guess I should do something similar with the public key as done with the private key in lines 2 and 3. Because the certificate is also encrypted. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:11:25+00:00Added an answer on May 27, 2026 at 1:11 pm

    To test your scenario, I’ve created an RSA private key with openssl.

    openssl genrsa -out private.pem 1024
    

    Then I’ve converted this key to PKCS#8 DER format.

    openssl pkcs8 -topk8 -inform PEM -in private.pem -outform DER -out private.der -nocrypt
    

    The manual of openssl refers to PKCS#8 and DER both as formats, so as far as I’m concerned the following happens:

    • pkcs8 tells openssl that I want to work with private keys in PKCS#8 format.
    • -topk8 tells it that the private key I’m going to specify with -in is not in PKCS#8 (otherwise it’ll assume it is).
    • -inform and -in specify that I want to convert the (PEM) private key to PKCS#8 (without -topk8 it’ll try to convert a key already in PKCS#8 format to a standard key format).
    • -outform and -out tells it I want a DER formatted key as output.
    • -nocrypt tells it that I don’t want to encrypt the key.

    Then, with my RSA key (in standard format) I’ve created a certificate.

    openssl req -new -x509 -keyform PEM -key private.pem -outform DER -out public.der
    

    The certificate contains the public key corresponding to my private key.

    After all of these, I’ve encoded both the private key and the certificate with Base64.

    base64 private.der > private.der.b64
    base64 public.der > public.der.b64
    

    The following files were generated.

    private.pem      # standard
    private.der      # pkcs8/DER
    private.der.b64 
    public.der       # x509/DER
    public.der.b64   
    
    public static void main(String[] args) throws IOException, GeneralSecurityException {
      // get a handle on the base64 encoded key and certificate
      File privateKeyFile = new File("private.der.b64");
      File publicKeyFile = new File("public.der.b64");
    
      // pull them into arrays
      byte[] privateKeyBytes = toByteArray(privateKeyFile);
      byte[] publicKeyBytes = toByteArray(publicKeyFile);
    
      // decode them
      privateKeyBytes = toDecodedBase64ByteArray(privateKeyBytes);
      publicKeyBytes = toDecodedBase64ByteArray(publicKeyBytes);
    
      // get the private key
      KeyFactory keyFactory = KeyFactory.getInstance("RSA");
      KeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
      PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
    
      // get the public key
      CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
      Certificate certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(publicKeyBytes));
      PublicKey publicKey = certificate.getPublicKey();
    }
    
    private static byte[] toByteArray(File file) throws IOException {
      // java 7's try-with-resources statement
      try (FileInputStream in = new FileInputStream(file);
          FileChannel channel = in.getChannel()) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        channel.transferTo(0, channel.size(), Channels.newChannel(out));
        return out.toByteArray();
      }
    }
    
    private static byte[] toDecodedBase64ByteArray(byte[] base64EncodedByteArray) {
      return DatatypeConverter.parseBase64Binary(
          new String(base64EncodedByteArray, Charset.forName("UTF-8")));
    }
    

    The main problem was that you had a certificate instead of a public key. The certificate contains the public key, but it cannot be loaded with X509EncodedKeySpec(...), this is why the CertificateFactory has to be used instead.

    (By the way here is a great article/tutorial on openssl and Java cryptography usage. I’ve got my info partly from there.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have public and private keys in separate .pem files that I would need
Suppose I have a class public class MyClass { private Set<String> set = new
I use openssl. First off I created private/public keys, then I encrypt a string
I have a public/private key pair set up so I can ssh to a
So I have the following: public class Singleton { private Singleton(){} public static readonly
I have a List<MyClass> The class is like this: private class MyClass { public
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have the following Repository: Public Class PageRepository Private Shared _pages As BLL.PageCollection Shared
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.