1

我认为这个问题不是骗人的,所以我将尝试解释我的情况。

我正在测试 JWT,更具体地说是来自 Github的 JOSE-JWT 库,好吧,我遇到了麻烦。

我正在使用 PHP 和phpseclib生成一个私钥-公钥对并将公钥发送给客户端。如您所见一切都是正确的。我的客户正在接收 JSON 并将其转换为对象并使用JSON.NET将其提取为字符串。

我正在使用BouncyCastle和 Stackoverflow 的答案,稍作修改即可直接从字符串中读取,而不是从文件中读取。

        public static RSACryptoServiceProvider GetRSAProviderFromPemFile(string pemfile)
        {
            return GetRSAProviderFromPemString(File.ReadAllText(pemfile).Trim());
        }

        public static RSACryptoServiceProvider GetRSAProviderFromPemString(string pemstr)
        {
            bool isPrivateKeyFile = true;

            if (pemstr.StartsWith(pempubheader) && pemstr.EndsWith(pempubfooter))
                isPrivateKeyFile = false;

            byte[] pemkey;
            if (isPrivateKeyFile)
                pemkey = DecodeOpenSSLPrivateKey(pemstr);
            else
                pemkey = DecodeOpenSSLPublicKey(pemstr);

            if (pemkey == null)
                return null;

            if (isPrivateKeyFile)
                return DecodeRSAPrivateKey(pemkey);
            else
                return DecodeX509PublicKey(pemkey);
        }

他们都给我带来了问题,答案和使用来自 Jose repo 的文档:

            var payload1 = new Dictionary<string, object>()
            {
                { "sub", "mr.x@contoso.com" },
                { "exp", 1300819380 }
            };

            Console.WriteLine("Jose says: {0}", JWT.Encode(payload1, pubkey, JwsAlgorithm.RS256));

例外:

...

等效英文: http: //unlocalize.com/es/74799_Keyset-does-not-exist.html

还有充气城堡:

            var claims = new List<Claim>();
            claims.Add(new Claim("claim1", "value1"));
            claims.Add(new Claim("claim2", "value2"));
            claims.Add(new Claim("claim3", "value3"));

            Console.WriteLine("Bouncy Castle says: {0}", Helpers.CreateToken(claims, pubkeyStr));

例外:

...

从这里提取的CreateToken方法:https ://stackoverflow.com/a/44857593/3286975

我对这个方法做了一点修改:

    public static string CreateToken(List<Claim> claims, string privateRsaKey)
    {
        RSAParameters rsaParams;
        using (var tr = new StringReader(privateRsaKey))
        {
            var pemReader = new PemReader(tr);
            var keyPair = pemReader.ReadObject() as AsymmetricCipherKeyPair;
            if (keyPair == null)
            {
                throw new Exception("Could not read RSA private key");
            }
            //var privateRsaParams = keyPair.Private as RsaPrivateCrtKeyParameters;
            rsaParams = DotNetUtilities.ToRSAParameters(keyPair.Public as RsaKeyParameters); //DotNetUtilities.ToRSAParameters(privateRsaParams);
        }
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            rsa.ImportParameters(rsaParams);
            Dictionary<string, object> payload = claims.ToDictionary(k => k.Type, v => (object)v.Value);
            return Jose.JWT.Encode(payload, rsa, Jose.JwsAlgorithm.RS256);
        }
    }

在这两种情况下,就像加密器方法正在寻找私钥(在客户端客户端????)......所以,我的问题是为什么这个例子在客户端使用私钥,如果维基百科这么说:

...

来源: https ://en.wikipedia.org/wiki/Public-key_cryptography

在几种情况下,我发现我认为是正确的:

https://connect2id.com/products/nimbus-jose-jwt/examples/jwt-with-rsa-encryption

在这个 Java 示例中,这使用公钥来加密数据,而不是私钥。

我不知道为什么 C# 示例在客户端使用私钥,这是不合逻辑的,有人可以解释我为什么,我该如何解决这个问题?

4

1 回答 1

1

我已经为我遇到的两个问题之一找到了答案,而且我还没有完全阅读 JOSE-JWT 存储库,它说:

var payload = new Dictionary<string, object>()
{
    { "sub", "mr.x@contoso.com" },
    { "exp", 1300819380 }
};

var publicKey=... //Load it from there you need

string token = Jose.JWT.Encode(payload, publicKey, JweAlgorithm.RSA_OAEP, JweEncryption.A256GCM);

我已经意识到 Bouncy Castle 只是一个操作公私钥的 API,加密解密工作由 JOSE-JWT 完成。所以,我的问题解决了。

于 2018-05-10T00:14:23.073 回答