Reputation: 145
I try to export an ECDSA PrivateKey from an existing (large) project and import it to a new one - after doing so the key differs. This is how the key is generated in the first place:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1");
keyGen.initialize(ecSpec, random);
KeyPair keyPair = keyGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
I then get the Byte array, convert it to hex and transfer it to another machine:
byte[] privateKeyBytes = privateKey.getEncoded();
String keyString = getHexString(privateKeyBytes);
And import it on the other side:
KeyFactory fact = KeyFactory.getInstance("ECDSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(hexStringToByteArray(msg));
PrivateKey key = fact.generatePrivate(keySpec);
Now after printing the key (System.out.println(key)), the first line differs:
Before:
EC Private Key [a7:3d:a5:d6:13:8f:42:d2:65:25:67:27:30:02:61:cb:1e:7a:1c:de]
After:
EC Private Key [29:8c:4c:22:0a:eb:b4:2b:c0:2d:15:d6:52:dc:e1:df:c5:1f:05:6b]
while X and Y stay the same. I confirmed the Byte array is the same after importing/exporting, this excludes getHexString/hexStringToByteArray.
I tried importing the key on the first machine again using the same code, this works, also generating a new key on the second and re-importing, also working. Just the combination causes problems.
System info: java -version: openjdk version "11.0.3" 2019-04-16 (both machines), both Ubuntu 18.04 64 Bit
Upvotes: 0
Views: 468
Reputation: 145
Just found the problem - The first machine was using a "custom" bcprov which behaves differently in some way. After using it on the second machine the keys match up.
Thank you for your time!
Upvotes: 0