Google Gruppi non supporta più i nuovi post o le nuove iscrizioni Usenet. I contenuti storici continuano a essere visibili.

Having trouble loading Public Key

9 visualizzazioni
Passa al primo messaggio da leggere

Eternally

da leggere,
28 apr 2003, 17:48:0128/04/03
a
Hey folks,

I've seen many examples of this on the internet. It's done like this
everytime, but for me it's not working. I'm trying to store a public key
and then load it up. But, when I encode the key, I can't get it back. I
get the exception: "java.security.spec.InvalidKeySpecException: Unknown
KeySpec type."
Anyone know why this might happen, or have an alternative way of doing this?
It's been driving me nuts all day. Thanks a lot!
As a note, the keys do work before it's encoded and stored. I can encrypt
and decrypt messages no problem. Also, I outputted the key format and it
says it is in fact PKCS#8.
Here's the code:

/*Generating the keys*/
Security.addProvider(new
org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
SecureRandom randVal = new SecureRandom();
kpGen.initialize(1024, randVal);
KeyPair kPair = kpGen.generateKeyPair();
PublicKey publicKey = kPair.getPublic();
PrivateKey privateKey = kPair.getPrivate();

/*Storing the key*/
byte[] bEncdPubKey = publicKey.getEncoded();

/*Loading above Stored Key*/
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bEncdPubKey);
KeyFactory kFact = KeyFactory.getInstance("RSA", "BC");
PublicKey partnerPublicKey = kFact.generatePublic(keySpec); /*This last line
gives exception*/


The exact output is:
java.security.spec.InvalidKeySpecException: Unknown KeySpec type.
at
org.bouncycastle.jce.provider.JDKKeyFactory$RSA.engineGeneratePublic(JDKKeyF
actory.java:282)
at java.security.KeyFactory.generatePublic(KeyFactory.java:221)
at MyClass.MyFunc(MyClass.java:131)


Eternally

da leggere,
28 apr 2003, 18:55:3728/04/03
a
Nothing worse than answering your own question.
Solution...and I have no idea why...but, even though the format is PKCS#8,
the exception doesn't occur if I replace the line:

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bEncdPubKey);
with
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bEncdPubKey);
Haven't been fully able to test if it properly decrypts yet, but the code
doesn't produce the exception anymore. Hopefully all is good now.


"Eternally" <m...@r.com> wrote in message
news:lkhra.59977$5f4....@twister.nyroc.rr.com...

Eternally

da leggere,
28 apr 2003, 21:19:0728/04/03
a
Ahhh....PKCS#8 is the format for private key. X509 is the public. That's
why!

"Eternally" <m...@r.com> wrote in message

news:Jjira.60007$5f4....@twister.nyroc.rr.com...

0 nuovi messaggi