I want apply the RSA encryption to my project, but encountered some troubles:
- First, I have download the JavaScripts library from http://www.ohdave.com/rsa/ ,and add reference to my project;
Second, I have define the RSA object and code to initialize that:
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);
UserInfo user = new UserInfo(); user.Birthday = DateTime.Now.Date; return View(user); } private RSACryptoServiceProvider GetRsa() { RSACryptoServiceProvider Rsa = new RSACryptoServiceProvider(1024); return Rsa; }
3.then, on JavaScript side , I have code, it encrypt the password user input and the bind it control:
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();
4.when user click submit, my C# code get the encrypted pwd string and try to decrypt it but failed with exception: bad data:
[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
who do you know how to fix it? thank you in advance.