0

I have tried to create a public key in two ways,

  1. I created Self Signed Certificate and export it to .pfx file and used Openssl to get temp.cer file which contains the public key

  2. used openssl to generate a public key as follows.

    openssl genrsa -out myjira.pem 1024
    
    openssl rsa -in temp.pem -pubout -out temp.pub
    

I am using an application and trying to implement OAuth on it. when I enter the public key from temp.cer (which is longer) it does not accepts it and says Must be a valid public key. java.security.InvalidKeyException:. But when I enter the public key from temp.pub (which is much shorter than the other one) it accepts it. now my question is what is differences between those public keys?

user217648
  • 2,772
  • 7
  • 32
  • 53

2 Answers2

1

A certificate file contains a public key as well as identity information that is bound to that key via a signature. That's why your certificate file is longer than your public key file.

If you load the certificate into a Java X509Certificate object, you can obtain the PublicKey by using the getPublicKey() method of the X509Certificate object.

gtrig
  • 10,294
  • 5
  • 25
  • 33
  • Thank you, you are right, I loaded the .cer file into X509Certificate2. It has a public key as you said, but it does not has any private key. Is it possible to create a .cer file from my .pub and .pem file so that it contains both private and public key? – user217648 Sep 27 '13 at 08:09
  • My prolem is Jira, I am trying to implement OAut. Jira accepts public key and it accepts ONLY a public key that is generated by openssl rsa -in temp.pem -pubout -out temp.pub. and my oauth client must use the private key. I have to create a .pfx file from a public key and a private key to load it by using X509Certificate which loads a .pfx file – user217648 Sep 27 '13 at 09:23
  • @user217648, Take a look at this [SO question](http://stackoverflow.com/questions/808669/convert-a-cert-pem-certificate-to-a-pfx-certificate) on creating pfx files from a key and cert. – gtrig Sep 27 '13 at 19:33
0

It sounds like the longer of your keys is in fact a private key. Generally you have a key pair, a private and public key.

more info: http://en.wikipedia.org/wiki/Public-key_cryptography

floodpants
  • 123
  • 7
  • 1
    usually .cer files containing public keys, isn't it? .cer file cotains -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- – user217648 Sep 26 '13 at 11:23