Java – get ecpoint / ecpublickeyparameters from byte [] of bouncy castle
So far, I have managed to generate ecdhe pairs in the lightweight API of inflatable castle However, I encountered a problem trying to recreate the public key from byte []
Since the ecpublickeyparameters object has only one method getq (), I assume that everything needed to rebuild the key Other parameters such as the curve used (p-521) remain unchanged
I am doing the following:
AsymmetricCipherKeyPair kp = kpgen.generateKeyPair(); //ECDHE Key Generator EcpublicKeyParameters pubKey = (EcpublicKeyParameters)kp.getPublic(); byte[] aPubKeybytes = pubKey.getQ().getEncoded(false); //Should I set to true or false?
Unless there is another method to get the original bytes of the public key pubkey, I don't see a method to get the bytes without calling the method getq() that returns the ecpoint object
My question is how to use the lightweight API of bouncy castle to rebuild byte [] into an ecpoint object Or, better yet, how to reconstruct the entire ecpublickeyparameter object using a byte array derived from the original pubkey object
Solution
For anyone who may be involved, I solved this problem by using publickeyfactory and subjectpublickeyinfofactory to encode and decode the key
use:
byte[] key = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(pubKey).getEncoded();
I can get the original bytes of the key
And use:
EcpublicKeyParameters bpubKey = (EcpublicKeyParameters)PublicKeyFactory.createKey(key);
I can recreate the public key