gpt4 book ai didi

cryptography - 如何从生成的 ecdsa key 填充 jsonwebkey

转载 作者:行者123 更新时间:2023-12-03 09:20:58 24 4
gpt4 key购买 nike

我尝试生成一个公钥/私钥对,将其用于 jose4j 的 JWT 数字签名。我用Elliptic Curve Digital Signature Algorithm

我的问题是我不知道如何获取代表 edcsa 键含义的参数:

  • crv
  • x
  • d

     KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
    ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
    g.initialize(kpgparams);

    KeyPair pair = g.generateKeyPair();
    // Instance of signature class with SHA256withECDSA algorithm
    Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
    ecdsaSign.initSign(pair.getPrivate());

    System.out.println("Private Keys is::" + pair.getPrivate());
    System.out.println("Public Keys is::" + pair.getPublic());

    JsonWebKeySet jsonWebKeySet = new JsonWebKeySet();
    PrivateKey privateKey = pair.getPrivate();
    JsonWebKey webKey = new JsonWebKey(privateKey) {

    @Override
    public String getKeyType() {
    // TODO Auto-generated method stub
    return "EC";
    }

    @Override
    protected void fillTypeSpecificParams(Map<String, Object> params,
    OutputControlLevel outputLevel) {
    params.put("use", "sig");
    params.put("key_ops", "sign");
    params.put("alg", "ES256");
    params.put("kid", "kukuPrivateKey");

    }
    };
    jsonWebKeySet.addJsonWebKey(webKey);
    System.out.println("aaaa"+jsonWebKeySet.toJson());

最佳答案

您可以直接使用您生成的公钥创建 JsonWebKey,jose4j 将处理参数和编码。

    KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
g.initialize(kpgparams);

KeyPair keyPair = g.generateKeyPair();

PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(keyPair.getPublic());
jwk.setPrivateKey(keyPair.getPrivate());
jwk.setUse(Use.SIGNATURE);

System.out.println(jwk.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY));
System.out.println(jwk.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE)); // to include the private key 'd'

您还可以使用 jose4j 中的 EcJwkGenerator 实用程序生成 key 对并将其包装在 JsonWebKey 中,

    EllipticCurveJsonWebKey jwk = EcJwkGenerator.generateJwk(EllipticCurves.P256);
jwk.setUse(Use.SIGNATURE);

System.out.println(jwk.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY));
System.out.println(jwk.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE)); // to include the private key 'd'

关于cryptography - 如何从生成的 ecdsa key 填充 jsonwebkey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com