title: Encryption with public key in Jose-JWT
body: <p>I think this question is not a dupe, so I will try to explain my situation.</p>
  
  <p>I'm testing JWT, more specifically <a href="https://github.com/dvsekhvalnov/jose-jwt" rel="nofollow noreferrer">JOSE-JWT lib</a> from Github, and well, I'm having troubles.</p>
  
  <p>I'm generating a private-public key pair and sending to the client the public key, using PHP and <a href="https://github.com/phpseclib/phpseclib" rel="nofollow noreferrer">phpseclib</a>. Everything is correct as you can see <a href="https://qbz28b-user.freehosting.host/api.php?action=pubkey" rel="nofollow noreferrer">there</a>. My client is receiving the JSON and converting it to a object and extracting it to a string using <a href="https://www.newtonsoft.com/json" rel="nofollow noreferrer">JSON.NET</a>.</p>
  
  <p>I'm using <a href="http://bouncycastle.org/csharp/index.html" rel="nofollow noreferrer">BouncyCastle</a> and an <a href="https://stackoverflow.com/a/32243171/3286975">answer from Stackoverflow</a> with a little modifications to read directly from a string instead from a File.</p>
  
  <pre><code>        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) &amp;&amp; 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);
          }
  </code></pre>
  
  <p>And both of them are giving me problems, with the answer and using docs from Jose repo:</p>
  
  <pre><code>            var payload1 = new Dictionary&lt;string, object&gt;()
              {
                  { "sub", "mr.x@contoso.com" },
                  { "exp", 1300819380 }
              };
  
              Console.WriteLine("Jose says: {0}", JWT.Encode(payload1, pubkey, JwsAlgorithm.RS256));
  </code></pre>
  
  <p><strong>Exception:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/1bzHJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1bzHJ.png" alt="..."></a></p>
  
  <p><em>English equivalent:</em> <a href="http://unlocalize.com/es/74799_Keyset-does-not-exist.html" rel="nofollow noreferrer">http://unlocalize.com/es/74799_Keyset-does-not-exist.html</a></p>
  
  <p>And with Bouncy Castle:</p>
  
  <pre><code>            var claims = new List&lt;Claim&gt;();
              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));
  </code></pre>
  
  <p><strong>Exception:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/JKK1C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JKK1C.png" alt="..."></a></p>
  
  <p><strong>CreateToken</strong> method extracted from here: <a href="https://stackoverflow.com/a/44857593/3286975">https://stackoverflow.com/a/44857593/3286975</a></p>
  
  <p>I did a little modification to this method:</p>
  
  <pre><code>    public static string CreateToken(List&lt;Claim&gt; 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&lt;string, object&gt; payload = claims.ToDictionary(k =&gt; k.Type, v =&gt; (object)v.Value);
              return Jose.JWT.Encode(payload, rsa, Jose.JwsAlgorithm.RS256);
          }
      }
  </code></pre>
  
  <p>In both cases is like the encrypter methods are looking for the private key (in the client client????)... So, My question is why this examples are using private keys in the client-side, if the Wikipedia says this:</p>
  
  <p><a href="https://i.stack.imgur.com/RAVDf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RAVDf.png" alt="..."></a></p>
  
  <p><strong>Source:</strong> <a href="https://en.wikipedia.org/wiki/Public-key_cryptography" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Public-key_cryptography</a></p>
  
  <p>And in several cases I found what I think is right:</p>
  
  <p><a href="https://connect2id.com/products/nimbus-jose-jwt/examples/jwt-with-rsa-encryption" rel="nofollow noreferrer">https://connect2id.com/products/nimbus-jose-jwt/examples/jwt-with-rsa-encryption</a></p>
  
  <p>In this Java example, this uses public key to encrypt data, not private one.</p>
  
  <p>I don't know why C# examples are using private keys in the client-side, this is ilogical, can somebody explain me why, and how can I solve this problems?</p>