-1

I am trying to use Python to work with peer public key generated from Java (uses BouncyCastle.)

Here is the snippet from the Java Code. It uses ECDH and Curve25519.

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
        Constants.ALGORITHM,
        Constants.PROVIDER
    );
X9ECParameters ecParams = CustomNamedCurves.getByName(Constants.CURVE);
ECParameterSpec ecSpec = new ECParameterSpec(
    ecParams.getCurve(),
    ecParams.getG(),
    ecParams.getN(),
    ecParams.getH(),
    ecParams.getSeed()
);

keyPairGenerator.initialize(ecSpec, new SecureRandom());
keyPairGenerator.generateKeyPair();

Additionally, I would like to create the public key in the same format as in Java (uncompressed). Python's cryptography module only supports X25519 (which has x coordinate only.)

Is there any library in python that can help achieve this ?

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Ajeet2040
  • 99
  • 1
  • 6
  • 1
    It's not clear what you are asking for. Your snippet does *not* show the use of ECDH and Curve25519, it's just a generic snippet of code to generate a key pair. The [cryptography](https://cryptography.io/en/latest/) module supports many more curves beyond X25519. – President James K. Polk Aug 27 '23 at 18:39
  • 1
    Did you call `getEncoded()` after generating the key pair? I'm not going to answer a question if the format isn't even clear... Please create an [mcve] instead of just displaying part of the code. – Maarten Bodewes Aug 27 '23 at 19:28
  • 1
    Use for X25519 instead of your approach: `KeyPairGenerator.getInstance("X25519").generateKeyPair()`. `ECDH` is for elliptic curves of the short Weierstrassform, while `X25519` is for Montgomery curves (like `curve25519`). The algorithms differ (e.g. clamping) and even if they did not, the key pairs would not be readily interchangeable (but it is possible to convert because of the birational equivalence, [here](https://datatracker.ietf.org/doc/html/draft-ietf-lwig-curve-representations-01#appendix-D.2)). – Topaco Aug 28 '23 at 15:06

0 Answers0