13Tem
ECDSA: Why do ssh-keygen and Java generated public keys have different sizes?
I am using ssh-keygen -t ecdsa -b 256
which generates this for the public part:
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNaHgNjXShzF/hmZOuhTCCsokgpSCyFohETHT4+OQMxW5g+d9nZCYxpDhwivuWbsoXTYpQWlLATXxjbQr2Y3KRY= t0132456@L541918
I need to do the same in Java. I am using BouncyCastle:
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(ecSpec, new SecureRandom());
KeyPair pair = g.generateKeyPair();
ECPublicKey publicKey = (ECPublicKey)pair.getPublic();
System.out.println("kpub=" + Base64.toBase64String(publicKey.getEncoded()));
but the public key is smaller and does not start with same header:
kpub=MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEvHW0nR1hAzy3BuQR8mMkHqgGvvdXLZWAXS29fkFbhshr8y6xybHh0LRDoaUciYgr3w7WGKpfxFSSFqSjdG8bww==
How to be generate the same way ssh-genkey does but in Java?