title: RSA: Encrypt password in javascript but failed to decrypt that in C#
body: <p>I want apply the RSA encryption to my project, but encountered some troubles:</p>
  
  <ol>
  <li>First, I have download the JavaScripts library from
  <a href="http://www.ohdave.com/rsa/" rel="nofollow">http://www.ohdave.com/rsa/</a> ,and add reference to my project;</li>
  <li><p>Second, I have define the RSA object and code to initialize that:</p>
  
  <pre><code>internal RSACryptoServiceProvider Rsa
      {
          get
          {
              if (HttpContext.Cache["Rsa"] != null)
              {
                  RSACryptoServiceProvider encryptKeys = (RSACryptoServiceProvider)HttpContext.Cache["Rsa"];
                  return encryptKeys;
              }
              else
              {
                  return new RSACryptoServiceProvider(1024);
              }
          }
          set
          {
              HttpContext.Cache.Remove("Rsa");
              HttpContext.Cache.Insert("Rsa", value);
          }
      }
  public ActionResult SignUp()
      {
          this.Rsa = Security.GetRsa();
          RSAParameters param= this.Rsa.ExportParameters(true);
          //this will bind to view
          TempData["exponent"] = Util.BytesToHexString(param.Exponent);
          TempData["key"] = Util.BytesToHexString(param.Modulus);
  
  <pre><code>    UserInfo user = new UserInfo();
      user.Birthday = DateTime.Now.Date;
      return View(user);
  }
  private RSACryptoServiceProvider GetRsa()
      {
          RSACryptoServiceProvider Rsa = new RSACryptoServiceProvider(1024);
          return Rsa;
      }
  </code></pre>
  
  </code></pre></li>
  </ol>
  
  <p>3.then, on JavaScript side , I have code, it encrypt the password user input and the bind it control:</p>
  
  <pre><code>var hash = document.getElementById("Pwd").value;
  var exponent = document.getElementById("exponent").innerHTML;
  var rsa_n = document.getElementById("key").innerHTML;
  setMaxDigits(131);
  var key = new RSAKeyPair(exponent, "", rsa_n);
  hash = encryptedString(key, "111");
  
  document.getElementById("Pwd").value = hash;
  document.getElementById("Pwd2").value = hash;
  
  document.getElementById("error").innerHTML = "";
  document.getElementById("submit").click();
  </code></pre>
  
  <p>4.when user click submit, my C# code get the encrypted pwd string and try to decrypt it but failed with exception: bad data:</p>
  
  <pre><code>[HttpPost]
      public ActionResult SignUp(UserInfo user)
      {
          user.UserId = user.UserId.ToLower(); //ignore case
          user.UserGUID = Guid.NewGuid();
          user.CreatedDate = DateTime.Now;
          user.IsEnabled = false;
  
          user.Pwd = Convert.ToBase64String(Rsa.Decrypt(Util.HexStringToBytes(user.Pwd), false));//Exception:Rsa.Decrypt throw bad data exception
  </code></pre>
  
  <p>who do you know how to fix it? thank you in advance.</p>