title: Create a instance of IDataProtectionProvider asp.net core
body: <p>I am trying to create a static class and methods to encrypt and decrypt data in asp.net core.</p>
  
  <p>But the problem is that I have to get the "IDataProtectionProvider provider" in constructor with DI and then pass it to the methods so that a CreateProtector be used.</p>
  
  <p>I donot want that and directly want to instanciate the IDataProtectionProvider provider in the method it self.</p>
  
  <p>The controller code:</p>
  
  <pre><code>private readonly IDataProtectionProvider _provider;
  public addMDL(IDataProtectionProvider provider)
  {
      _provider = provider;
  }
  
  public IActionResult OnGet()
  {
      DataProProvider.decData(0, "ABC", _provider)
  }
  </code></pre>
  
  <p>and the static class is :</p>
  
  <pre><code>public static class DataProProvider
  {
  
      public static string encData(int intData, string strData, IDataProtectionProvider provider)
      {
          string str;
          IDataProtector dataProtector;
          dataProtector = provider.CreateProtector("AA");
          if (!string.IsNullOrEmpty(strData))
          {
              str = dataProtector.Protect(strData);
          }
          else
          {
              str = dataProtector.Protect(intData.ToString());
          }
          return str;
      }
  
      public static string decData(int intData, string strData, IDataProtectionProvider provider)
      {
          string str;
          IDataProtector dataProtector;
          dataProtector = provider.CreateProtector("A3");
          if (!string.IsNullOrEmpty(strData))
          {
              str = dataProtector.Unprotect(strData);
          }
          else
          {
              str = dataProtector.Unprotect(intData.ToString());
          }
          return str;
      }
  }
  </code></pre>
  
  <p>[UPDATE]</p>
  
  <p>As per suggestion I have moved to a smpler approch using <a href="https://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp">Encrypting &amp; Decrypting a String in C#</a>  <a href="https://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp">enter link description here</a></p>