在 Jose4J 中从 JSON 获取 EllipticCurveJsonWebKey
Get EllipticCurveJsonWebKey from JSON in Jose4J
我正在使用 jose4j 在我的应用程序中实现基于令牌的身份验证。我想知道,是否有可能从数据库中存储的 json 加载 JWK。
我做了以下步骤:
创建了一个密钥:EllipticCurveJsonWebKey key = EcJwkGenerator.generateJwk(EllipticCurves.P521);
从中创建 JSON:key.toJson()
将 json 值保存到数据库。
正在从数据库加载值。
在这一点上我卡住了。我不知道如何使用 json.
中提供的数据创建密钥
有什么解决办法吗?
好的,我从这个 link.
中找到了一个可行的解决方案
我 post 对我有用的答案:
// Generate a new RSA key pair wrapped in a JWK
PublicJsonWebKey rsaJwk = RsaJwkGenerator.generateJwk(2048);
// or an EC key, if you prefer
PublicJsonWebKey ecJwk = EcJwkGenerator.generateJwk(EllipticCurves.P256);
// A JSON string with only the public key info
String publicKeyJwkString = rsaJwk.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY);
System.out.println(publicKeyJwkString);
// A JSON string with both the public and private key info
String keyPairJwkString = rsaJwk.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE);
System.out.println(keyPairJwkString);
// parse and convert into PublicJsonWebKey/JsonWebKey objects
PublicJsonWebKey parsedPublicKeyJwk = PublicJsonWebKey.Factory.newPublicJwk(publicKeyJwkString);
PublicJsonWebKey parsedKeyPairJwk = PublicJsonWebKey.Factory.newPublicJwk(keyPairJwkString);
// the private key can be used to sign (JWS) or decrypt (JWE)
PrivateKey privateKey = parsedKeyPairJwk.getPrivateKey();
// the public key can be used to verify (JWS) or encrypt (JWE)
PublicKey publicKey = parsedPublicKeyJwk.getPublicKey();
我正在使用 jose4j 在我的应用程序中实现基于令牌的身份验证。我想知道,是否有可能从数据库中存储的 json 加载 JWK。 我做了以下步骤:
创建了一个密钥:
EllipticCurveJsonWebKey key = EcJwkGenerator.generateJwk(EllipticCurves.P521);
从中创建 JSON:
key.toJson()
将 json 值保存到数据库。
正在从数据库加载值。
在这一点上我卡住了。我不知道如何使用 json.
中提供的数据创建密钥
有什么解决办法吗?
好的,我从这个 link.
中找到了一个可行的解决方案我 post 对我有用的答案:
// Generate a new RSA key pair wrapped in a JWK
PublicJsonWebKey rsaJwk = RsaJwkGenerator.generateJwk(2048);
// or an EC key, if you prefer
PublicJsonWebKey ecJwk = EcJwkGenerator.generateJwk(EllipticCurves.P256);
// A JSON string with only the public key info
String publicKeyJwkString = rsaJwk.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY);
System.out.println(publicKeyJwkString);
// A JSON string with both the public and private key info
String keyPairJwkString = rsaJwk.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE);
System.out.println(keyPairJwkString);
// parse and convert into PublicJsonWebKey/JsonWebKey objects
PublicJsonWebKey parsedPublicKeyJwk = PublicJsonWebKey.Factory.newPublicJwk(publicKeyJwkString);
PublicJsonWebKey parsedKeyPairJwk = PublicJsonWebKey.Factory.newPublicJwk(keyPairJwkString);
// the private key can be used to sign (JWS) or decrypt (JWE)
PrivateKey privateKey = parsedKeyPairJwk.getPrivateKey();
// the public key can be used to verify (JWS) or encrypt (JWE)
PublicKey publicKey = parsedPublicKeyJwk.getPublicKey();