I'm trying to generate a "Secp256k1" keypair with java.
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256k1");
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(ecSpec, new SecureRandom());
KeyPair keyPair = g.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
System.out.println(Base64.getEncoder().encodeToString(privateKey.getEncoded()));
System.out.println(Base64.getEncoder().encodeToString(publicKey.getEncoded()));
But if i look at the Outputs, my keypair always looks simmular.
Try 0: Pubkey: MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEx2WR3g1ytBdx8VJ+N121FFjn/YFbZ77ZumqVteTXAHnzN9fR+3NRD0EQ8kb+TnHvDMCtRR6a7GE8ckVVpajCrA==
Try 1: Pubkey: MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEpzA5+Zd8xtD7UH0IxlIyIGIFCjctvzGMqBKEpqDf09PLTGcp9UrDHOB/uWH9VGA+nJAUjnPtxSR+njuDZy4JZw==
Try 2: Pubkey: MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEbXclGh0T7jScfGfJNhAw6MnFI4AcpAytdd7TsrEykbOVGsT8xBla7x7uYdlBp3KNVQPG7E9X5Ajftd1dOyTjeA==
Try 3: Pubkey: MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEACJKf861/P4yhsJnPMitSWiLNrbvBEYdB/pndY0ScUWdKfIPhA3qbHLTzYPROA7wiGbj2oS7joxYzhrrWb0rwA==
"MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE" the beginning is always the same, and it ends with "=="
Im sure there is something im missing. Does anybody knows why it always beginns with the same 32 characters?
Since you are using BouncyCastle
as the provider, your public key will be instance of BCECPublicKey
. The getEncoded
method of this class reuturns encoded SubjectPublicKeyInfo
structure. This is defined in RFC5480
:
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier,
subjectPublicKey BIT STRING
}
The first field of this structure is algorithm of type AlgorithmIdentifier
. Since you do not change the algorithm for your keys - the bytes at the begining are the same (they designate the same algorithm) and this is expected behaviour.