Creating ECPublicKey Using Bouncy Castle
This generates the EU public key used in the JCE / JCA. The Bouncy Castle provider can directly use these software keys. Otherwise, Bouncy is simply used to generate the parameters needed to generate the public key.
package nl.owlstead.stackoverflow; import static java.nio.charset.StandardCharsets.US_ASCII; import java.math.BigInteger; import java.security.KeyFactory; import java.security.KeyPairGenerator; import java.security.Security; import java.security.interfaces.ECPublicKey; import java.security.spec.ECGenParameterSpec; import java.security.spec.ECParameterSpec; import java.security.spec.ECPoint; import java.security.spec.ECPublicKeySpec; import javax.crypto.Cipher; import org.bouncycastle.jce.ECNamedCurveTable; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec; import org.bouncycastle.jce.spec.ECNamedCurveSpec; import org.bouncycastle.util.encoders.Hex; public class ECPublicKeyFactory { public static void main(String[] args) throws Exception { String name = "secp256r1"; Security.addProvider(new BouncyCastleProvider());
Creating Bouncy Castle ECPublicKeyParameters
Initially, I thought that a special Bouncy Castle key was required, so the following code generates the EC public key used in the Bouncy Castle lightweight API.
package nl.owlstead.stackoverflow; import java.math.BigInteger; import java.security.KeyPairGenerator; import java.security.Security; import java.security.interfaces.ECPublicKey; import java.security.spec.ECGenParameterSpec; import org.bouncycastle.asn1.ASN1ObjectIdentifier; import org.bouncycastle.asn1.x9.ECNamedCurveTable; import org.bouncycastle.asn1.x9.X9ECParameters; import org.bouncycastle.crypto.params.ECNamedDomainParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.math.ec.ECCurve; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.util.encoders.Hex; public class BC_EC_KeyCreator { public static void main(String[] args) throws Exception { String name = "secp256r1";
This was mostly complicated because I did not want to include any JCE code, and X9ECParameters not a subclass of ECDomainParameters . Therefore, I used the conversion to ECNamedDomainParameters , copied from another place in the Bouncy Castle code base.
source share