如何使用node-jose生成加密的JWE

时间:2018-02-07 09:14:04

标签: node.js encryption jwt jwe jwk

我正在使用node-jose v0.11.0(https://www.npmjs.com/package/node-jose)进行JWK和JWE操作。我有一个JWK格式的RSA密钥,我可以将其加载到JWK密钥库中并再次提取。但是,当我尝试加密任何东西时,我会进入“error2”,“不支持的算法”。 RSA如何成为不受支持的算法?

import * as jose from "node-jose";

const webkey = {
        "keys": [
            {
                "kty": "RSA",
                "e": "AQAB",
                "kid": "a024254d-0321-459f-9530-93020ce9d54a",
                "key_ops": [
                    "encrypt"
                ],
                "n": "jkHgYN98dlR2w7NX-gekCWaCdbxs7X4XXh52DVQrK--krwUYqRbBIUEw1bV8KX0ox6TLt-e6wpYsYYFUItSd5ySqohHRMq1IhyE2zpEC95BA9V7VrFUYnczf1bd5c-aR079aoz5JPXfqx01TzNfxWBb04SlRjsmJeY1v6JrDUI5U0FSOmnJTb3tSS6Szrvi_qOyViYp4v9V2_OVYy45kF_LQQy-pr-kP4gapXL235cieeTW6UvkhzaPT2D-JKyzVjjjgnfRXr8Ox9I9c4wpef2-5nPPeafB5EnOMpJE11KzO_8xxiTGUywPPLQagBvY35gkhQbYS2dv3NGIVSLZHFw"
            }
        ]
    };
    console.log("webkey", webkey);

    //generate key store from public JWK
    jose.JWK.asKeyStore(webkey)
        .then((result) => {
            console.log("Key Store", JSON.stringify(result.toJSON()));
            let keyStore = result;

            //get the key to encrypt
            const encryptionKey: jose.JWK.Key = keyStore.get(webkey.keys[0].kid);
            const output = jose.util.base64url.encode("Hello World");
            const output2 = jose.util.asBuffer(output);

            //encrypting content
            jose.JWE.createEncrypt(encryptionKey)
                .update(output2)
                .final()
                .then((jweInGeneralSerialization) => {
                    console.log("Encryption result", JSON.stringify(jweInGeneralSerialization));
                }, (error) => {
                    console.log("error2", error.message);
                });

        }, (error) => {
            console.log("error1", error.message);
        })

输出如下:

'webkey', Object{keys: [Object{kty: ..., e: ..., kid: ..., key_ops: ..., n: ...}]}
'Key Store', '{"keys":[{"kty":"RSA","kid":"a024254d-0321-459f-9530-93020ce9d54a","key_ops":["encrypt"],"e":"AQAB","n":"jkHgYN98dlR2w7NX-gekCWaCdbxs7X4XXh52DVQrK--krwUYqRbBIUEw1bV8KX0ox6TLt-e6wpYsYYFUItSd5ySqohHRMq1IhyE2zpEC95BA9V7VrFUYnczf1bd5c-aR079aoz5JPXfqx01TzNfxWBb04SlRjsmJeY1v6JrDUI5U0FSOmnJTb3tSS6Szrvi_qOyViYp4v9V2_OVYy45kF_LQQy-pr-kP4gapXL235cieeTW6UvkhzaPT2D-JKyzVjjjgnfRXr8Ox9I9c4wpef2-5nPPeafB5EnOMpJE11KzO_8xxiTGUywPPLQagBvY35gkhQbYS2dv3NGIVSLZHFw"}]}'
'error2', 'unsupported algorithm'

更新 我在实际代码中挖掘了一下,并在“basekey.js”中发现错误被抛出,因为库的算法是空的。

 Object.defineProperty(this, "encrypt", {
 value: function(alg, data, props) {
  // validate appropriateness
  if (this.algorithms("encrypt").indexOf(alg) === -1) {
      console.log("Algorithm USED", alg
      );
      console.log("All algorithms", this.algorithms("encrypt"))
    return Promise.reject(new Error("unsupported algorithm"));
  }

这里的输出是:

'Algorithm USED', 'A128CBC-HS256'
'All algorithms', []

1 个答案:

答案 0 :(得分:1)

我有一个示例,已添加到另一个问题: node-jose explanation / example?

我在研究证明中使用了node-jose,以反映我的C#代码,我只在服务器上以c#编写了签名和加密的令牌以进行解密和验证。

  

我需要使用对称密钥或不对称公共私钥对   ?

我将RSA密钥用于非对称签名,并且密钥包装了内容的对称加密详细信息。用于内容加密的加密算法是一种对称算法。 node-jose软件包生成了对称密钥。密钥包装算法对对称密钥进行了加密。

我拥有的C#代码解密并验证令牌签名。请注意:我使用软件包的功能来完成所有工作。

这是我进行检查的Runkit笔记本:

用于签名(JWS)https://runkit.com/archeon2/5bd66a8e7ee3b70012ec2e39

用于加密(JWE)https://runkit.com/archeon2/5bd6736ff36b39001313262a

在决赛中,我将两者结合在一起,创建了一个签名令牌,然后将输出用作加密的令牌(JWS + JWE)的有效负载。我在解密和验证创建的令牌中成功使用了c#服务器代码。

JWS + JWE:https://runkit.com/archeon2/jws-jwe-integration

  

我需要如何生成以及在服务器中需要存储密钥的位置   节点应用程序,然后允许我签名并验证令牌?

var store = jose.JWK.createKeyStore();
await store.generate("RSA",2048,{alg:"RS256", key_ops:["sign", "decrypt", "unwrap"]});
lkey = (await store.get());
var key = lkey.toJSON(); //get public key to exchange
key.use = "sig";
key.key_ops=["encrypt","verify", "wrap"];

var pubKey = await jose.JWK.asKey(key);
key = null;

密钥库可以序列化为JSON,因此我的概念是将其存储在会话存储或浏览器的本地存储中。然后检索JSON表示形式并读取Keystore。

var store= await jose.JWK.asKeyStore({"keys":[{"kty":"RSA","kid":"h9VHWShTfENF6xwjF3FR_b-9k1MvBvl3gnWnthV0Slk","alg":"RS256","key_ops":["sign","decrypt","unwrap"],"e":"AQAB","n":"l61fUp2hM3QxbFKk182yI5wTtiVS-g4ZxB4SXiY70sn23TalKT_01bgFElICexBXYVBwEndp6Gq60fCbaBeqTEyRvVbIlPlelCIhtYtL32iHvkkh2cXUgrQOscLGBm-8aWVtZE3HrtO-lu23qAoV7cGDU0UkX9z2QgQVmvT0JYxFsxHEYuWBOiWSGcBCgH10GWj40QBryhCPVtkqxBE3CCi9qjMFRaDqUg6kLqY8f0jtpY9ebgYWOmc1m_ujh7K6EDdsdn3D_QHfwtXtPi0ydEWu7pj1vq5AqacOd7AQzs4sWaTmMrpD9Ux43SVHbXK0UUkN5z3hcy6utysiBjqOwQ","d":"AVCHWvfyxbdkFkRBGX225Ygcw59fMLuejYyVLCu4qQMHGLO4irr7LD8EDDyZuOdTWoyP7BkM2e7S367uKeDKoQ6o1LND2cavgykokaI7bhxB0OxhVrnYNanJ1tCRVszxHRi78fqamHFNXZGB3fr4Za8frEEVJ5-KotfWOBmXZBvnoXbYbFXsKuaGo121AUCcEzFCGwuft75kPawzNjcdKhItfFrYh45OQLIO08W0fr_ByhxzWMU7yFUCEL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","wrap"],"e":"AQAB","n":"l61fUp2hM3QxbFKk182yI5wTtiVS-g4ZxB4SXiY70sn23TalKT_01bgFElICexBXYVBwEndp6Gq60fCbaBeqTEyRvVbIlPlelCIhtYtL32iHvkkh2cXUgrQOscLGBm-8aWVtZE3HrtO-lu23qAoV7cGDU0UkX9z2QgQVmvT0JYxFsxHEYuWBOiWSGcBCgH10GWj40QBryhCPVtkqxBE3CCi9qjMFRaDqUg6kLqY8f0jtpY9ebgYWOmc1m_ujh7K6EDdsdn3D_QHfwtXtPi0ydEWu7pj1vq5AqacOd7AQzs4sWaTmMrpD9Ux43SVHbXK0UUkN5z3hcy6utysiBjqOwQ","use":"sig"}]});
  

我怎么知道OCT,EC,RSA等中的哪一种?

为此,您的代币服务需要可能决定了这一点。我需要接收者是可以看到内容的接收者,因此我选择RSA作为非对称密钥。伪造有点难。

这些笔记本有些正在进行中。请仔细检查,因为这是我的解释以及我如何制定所需的内容。我希望他们能提供一些指导。