title: Using Key by Azure Key Vault to Encrypt and Decrypt text
body: <p>I'm trying use a key from Azure Key Vault to Encrypt and Decrypt the cookies of a web API.</p>
  
  <p>To encryption proccess I'm using the RSA, in that class:</p>
  
  <pre><code>    public class SimpleRSA
      {
          private RSA _rsa;
  
          public SimpleRSA(RSA rsa)
          {
              _rsa = rsa;
          }
  
          public string EncryptAsync(string value)
          {
              var byteData = Encoding.Unicode.GetBytes(value);
              var encryptedText = _rsa.Encrypt(byteData, RSAEncryptionPadding.OaepSHA1);
              var encodedText = Convert.ToBase64String(encryptedText);
              return encodedText;
          }
  
  
          public string DecryptAsync(string encryptedText)
          {
              var encryptedBytes = Convert.FromBase64String(encryptedText);
              var decryptionResult = _rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA1);
              var decryptedText = Encoding.Unicode.GetString(decryptionResult);
              return decryptedText;
          }
      }
  </code></pre>
  
  <p>And I'm getting my RSA from the Key, using that code:</p>
  
  <pre><code>  public RSA GetRSA(string appId, string appSecret)
          {
              AuthenticationCallback callback = async (authority, resource, scope) =&gt;
              {
                  var authContext = new AuthenticationContext(authority);
  
                  var credential = new ClientCredential(appId, appSecret);
                  var authResult = await authContext.AcquireTokenAsync(resource, credential);
                  return authResult.AccessToken;
              };
  
              var client = new KeyVaultClient(callback);
  
              var result = client.GetKeyAsync(_vaultBaseUrl, _keyId).Result;
              var key = result.Key;
  
              return key.ToRSA();
          }
  </code></pre>
  
  <p>I got the RSA from my Azure Key Vault and I managed encrypt my string. The problem is when I'm trying Decrypt the value. In that process I got that error:</p>
  
  <pre><code>System.Security.Cryptography.CryptographicException: 'Error decoding OAEP padding.'
  </code></pre>
  
  <p>I think that can be happening because I'm without the private keys in RSA, but I've tried use this method to get the RSA with private key::</p>
  
  <pre><code>key.ToRSA(true);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/2cD6W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2cD6W.png" alt="enter image description here"></a></p>
  
  <p>But a got that error:</p>
  
  <p><a href="https://i.stack.imgur.com/Bxkks.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bxkks.png" alt="enter image description here"></a></p>
  
  <p>So, I don't know how I can complete this process. Are there other way to do that? Or what's wrong?</p>