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" <m...@r.com> wrote in message
news:lkhra.59977$5f4....@twister.nyroc.rr.com...
"Eternally" <m...@r.com> wrote in message
news:Jjira.60007$5f4....@twister.nyroc.rr.com...