title: Specified padding mode is not valid for this algorithm. PS256
body: <p>I use provided code in this <a href="https://cloud.yandex.com/en-ru/docs/iam/operations/iam-token/create-for-sa#via-jwt" rel="nofollow noreferrer">document</a>. The difference here is only values of some account variables. Algorithm used for generate private key (that stored in private.pem file) is RSA_2048.</p>
  <p>The main goal is achive JWT with PS256 encryption algorithm.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Security.Cryptography;
  using Jose;
  using Org.BouncyCastle.Crypto.Parameters;
  using Org.BouncyCastle.OpenSsl;
  using Org.BouncyCastle.Security;
  
  class Program
  {
      static void Main(string[] args)
      {
          var serviceAccountId = &quot;myServiceAccountId&quot;;
          var keyId = &quot;myKeyId
          var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
  
          var headers = new Dictionary&lt;string, object&gt;()
          {
              { &quot;kid&quot;, keyId }
          };
  
          var payload = new Dictionary&lt;string, object&gt;()
          {
              { &quot;aud&quot;, &quot;someAudience&quot; },
              { &quot;iss&quot;, serviceAccountId },
              { &quot;iat&quot;, now },
              { &quot;exp&quot;, now + 3600 }
          };
  
          RsaPrivateCrtKeyParameters privateKeyParams;
          using (var pemStream = File.OpenText(&quot;private.pem&quot;))
          {
              privateKeyParams = new PemReader(pemStream).ReadObject() as RsaPrivateCrtKeyParameters;
          }
  
          using (var rsa = new RSACryptoServiceProvider())
          {
              rsa.ImportParameters(DotNetUtilities.ToRSAParameters(privateKeyParams));
              string encodedToken = Jose.JWT.Encode(payload, rsa, JwsAlgorithm.PS256, headers);
          }
      }
  }
  </code></pre>
  <p>On line:</p>
  <pre><code>string encodedToken = Jose.JWT.Encode(payload, rsa, JwsAlgorithm.PS256, headers);
  </code></pre>
  <p>I receive an exception:</p>
  <pre><code>System.AggregateException: One or more errors occurred. (Specified padding mode is not valid for this algorithm.)
   ---&gt; System.Security.Cryptography.CryptographicException: Specified padding mode is not valid for this algorithm.
     at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
     at System.Security.Cryptography.RSA.SignData(Byte[] data, Int32 offset, Int32 count, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
     at System.Security.Cryptography.RSA.SignData(Byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
     at Jose.RsaPssUsingSha.Sign(Byte[] securedInput, Object key)
     at Jose.JWT.EncodeBytes(Byte[] payload, Object key, JwsAlgorithm algorithm, IDictionary`2 extraHeaders, JwtSettings settings)
     at Jose.JWT.Encode(String payload, Object key, JwsAlgorithm algorithm, IDictionary`2 extraHeaders, JwtSettings settings)
     at Jose.JWT.Encode(Object payload, Object key, JwsAlgorithm algorithm, IDictionary`2 extraHeaders, JwtSettings settings)
  </code></pre>
  <p>I use jwt-jose package for encoding.</p>