使用RSA兼容.NET的Javascript加密使用充气城堡库 [英] Javascript encryption using RSA compatible with .NET using bouncy castle library
问题描述
我需要通过javascript对用户登录密码进行RSA加密,该密码从Web应用程序的登录页面发送到服务器,由http://www.bouncycastle.org/csharp提供的C#库解密。 />
使用以下代码:
I need to make RSA encryption by javascript for the user login password that sent from the login page in web application to the server to be decrypted by C# library provided by http://www.bouncycastle.org/csharp.
using the following code:
// Generate public key using RSA encryption
protected void Session_Start(object sender, EventArgs e)
{
if (System.Runtime.Caching.MemoryCache.Default.Get("privateKey") == null)
{
RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator();
rsaKeyPairGnr.Init(new Org.BouncyCastle.Crypto.KeyGenerationParameters(new SecureRandom(), 1024));
Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair();
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);
var pubKeyPEM = FormatPem(publicKeyInfo.GetEncoded().ToBase64(), "PUBLIC KEY");
RsaKeyParameters publicKey = (RsaKeyParameters)keyPair.Public;
RsaKeyParameters privateKey = (RsaKeyParameters)keyPair.Private;
System.Runtime.Caching.MemoryCache.Default.Add("publicKey", pubKeyPEM, DateTimeOffset.MaxValue);
System.Runtime.Caching.MemoryCache.Default.Add("publicKeyX", publicKey, DateTimeOffset.MaxValue);
System.Runtime.Caching.MemoryCache.Default.Add("privateKey", privateKey, DateTimeOffset.MaxValue);
}
}
// Convert SubjectPublicKeyInfo object to PEM format
private static string FormatPem(string pem, string keyType)
{
var sb = new StringBuilder();
sb.AppendFormat("-----BEGIN {0}-----\n", keyType);
int line = 1, width = 64;
while ((line - 1) * width < pem.Length)
{
int startIndex = (line - 1) * width;
int len = line * width > pem.Length
? pem.Length - startIndex
: width;
sb.AppendFormat("{0}\n", pem.Substring(startIndex, len));
line++;
}
sb.AppendFormat("-----END {0}-----\n", keyType);
return sb.ToString();
}
//Login aspx : javascript code for the login button get public key and password and encrypt using JSEncrypt library
<asp:Button ID="LoginButton" runat="server" OnClientClick="onLoginButtonClicked()" CommandName="Login" Text="Log In" ValidationGroup="Login1" />
function onLoginButtonClicked() {
var key = $("#<%= hdnkey.ClientID%>").val();
var password = $("#Login1_Password").val();
var encrypt = new JSEncrypt();
encrypt.setPublicKey(key);
var encrypted = encrypt.encrypt(password);
$("#<%= hdnEncryptedPass.ClientID%>").val(encrypted);
}
//Login aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
if (System.Runtime.Caching.MemoryCache.Default.Get("publicKey") != null)
{
var publicKey = System.Runtime.Caching.MemoryCache.Default.Get("publicKey");
this.hdnkey.Value = publicKey.ToString();
}
}
// servre side code that decrypt the encrypted password using the private key
// it gives error while decrypt in the follwoing line
// byte[] deciphered = cipher.ProcessBlock(ciphered, 0, ciphered.Length);
//the error message is: input too large for RSA cipher.
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
try
{
RsaKeyParameters privateKey = (RsaKeyParameters)System.Runtime.Caching.MemoryCache.Default.Get("privateKey");
string encrypted = this.hdnEncryptedPass.Value;
SHA512Managed hash = new SHA512Managed();
SecureRandom randomNumber = new SecureRandom();
byte[] encodingParam = hash.ComputeHash(Encoding.UTF8.GetBytes(randomNumber.ToString()));
IAsymmetricBlockCipher cipher = new RsaEngine();
UTF8Encoding utf8enc = new UTF8Encoding();
cipher.Init(false, privateKey);
byte[] ciphered = Encoding.UTF8.GetBytes(encrypted);
byte[] deciphered = cipher.ProcessBlock(ciphered, 0, ciphered.Length);
var pass = utf8enc.GetString(deciphered);
}
catch (Exception ee)
{
throw;
}
e.Authenticated = Membership.ValidateUser(Login1.UserName, Login1.Password);
}
我的尝试:
i在www.bouncycastle.org/csharp上尝试了.NET库,但它只是服务器端代码。我需要使用RSA加密进行加密并与使用C#和.NET框架实现的RSA加密兼容的javascript代码由充气城堡图书馆。
i尝试了javascript库Jscript并且失败了。
** Javascript代码加密密码并将其发送到长度大于服务器的服务器decrypt方法接受。
What I have tried:
i tried the .NET library on www.bouncycastle.org/csharp but it is server side code only.i need the javascript code that made encryption with RSA encryption and compatible with RSA encryption implemented with C# and .NET framework by the bouncy castle library.
i tried the javascript library Jscript and it failed.
**the Javascript code encrypt the password and send it to server with larger length than the size that the decrypt method accept.
推荐答案
( # & lt;%= hdnkey.ClientID%>)。val();
var password =
("#<%= hdnkey.ClientID%>").val(); var password =
( #Login1_Password跨度>)VAL();
var encrypt = new JSEncrypt();
encrypt.setPublicKey(key);
var encrypted = encrypt.encrypt(password);
("#Login1_Password").val(); var encrypt = new JSEncrypt(); encrypt.setPublicKey(key); var encrypted = encrypt.encrypt(password);
( #& lt;%= hdnEncryptedPass.ClientID%>)。val(加密);
}
// 登录aspx.cs
protected void Page_Load( object sender,EventArgs e)
{
if (IsPostBack) return ;
if (System.Runtime.Caching.MemoryCache.Default.Get( publicKey)!= null )
{
var publicKey = System.Runtime.Caching.MemoryCache.Default.Get( publicKey);
this .hdnkey.Value = publicKey.ToString();
}
}
// 解密密码的servre端代码使用私钥的加密密码
// 它在下一行解密时出错/ span>
// byte [] deciphered = cipher.ProcessBlock(ciphered,0,ciphered.Length) ;
// 错误消息是:输入对于RSA密码而言太大。
protected void Login1_Authenticate( object sender,AuthenticateEventArgs e)
{
try
{
RsaKeyParameters privateKey =(RsaKeyParameters)System.Runtime。 Caching.MemoryCache.Default.Get( privateKey);
string encrypted = 此 .hdnEncryptedPass.Value;
SHA512Managed hash = new SHA512Managed();
SecureRandom randomNumber = new SecureRandom();
byte [] encodingParam = hash.ComputeHash(Encoding.UTF8.GetBytes(randomNumber.ToString()));
IAsymmetricBlockCipher cipher = new RsaEngine();
UTF8Encoding utf8enc = new UTF8Encoding();
cipher.Init( false ,privateKey);
byte [] ciphered = Encoding.UTF8.GetBytes(加密);
byte [] deciphered = cipher.ProcessBlock(ciphered, 0 ,ciphered.Length);
var pass = utf8enc.GetString(deciphered);
}
catch (例外ee)
{
掷跨度>;
}
e.Authenticated = Membership.ValidateUser(Login1.UserName,Login1.Password);
}
("#<%= hdnEncryptedPass.ClientID%>").val(encrypted); } //Login aspx.cs protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; if (System.Runtime.Caching.MemoryCache.Default.Get("publicKey") != null) { var publicKey = System.Runtime.Caching.MemoryCache.Default.Get("publicKey"); this.hdnkey.Value = publicKey.ToString(); } } // servre side code that decrypt the encrypted password using the private key // it gives error while decrypt in the follwoing line // byte[] deciphered = cipher.ProcessBlock(ciphered, 0, ciphered.Length); //the error message is: input too large for RSA cipher. protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { try { RsaKeyParameters privateKey = (RsaKeyParameters)System.Runtime.Caching.MemoryCache.Default.Get("privateKey"); string encrypted = this.hdnEncryptedPass.Value; SHA512Managed hash = new SHA512Managed(); SecureRandom randomNumber = new SecureRandom(); byte[] encodingParam = hash.ComputeHash(Encoding.UTF8.GetBytes(randomNumber.ToString())); IAsymmetricBlockCipher cipher = new RsaEngine(); UTF8Encoding utf8enc = new UTF8Encoding(); cipher.Init(false, privateKey); byte[] ciphered = Encoding.UTF8.GetBytes(encrypted); byte[] deciphered = cipher.ProcessBlock(ciphered, 0, ciphered.Length); var pass = utf8enc.GetString(deciphered); } catch (Exception ee) { throw; } e.Authenticated = Membership.ValidateUser(Login1.UserName, Login1.Password); }
我的尝试:
i在www.bouncycastle.org/csharp上尝试了.NET库,但它只是服务器端代码。我需要使用RSA加密进行加密并与使用C#和.NET框架实现的RSA加密兼容的javascript代码由充气城堡图书馆。
i尝试了javascript库Jscript并且失败了。
** Javascript代码加密密码并将其发送到长度大于服务器的服务器解密方法接受。
What I have tried:
i tried the .NET library on www.bouncycastle.org/csharp but it is server side code only.i need the javascript code that made encryption with RSA encryption and compatible with RSA encryption implemented with C# and .NET framework by the bouncy castle library.
i tried the javascript library Jscript and it failed.
**the Javascript code encrypt the password and send it to server with larger length than the size that the decrypt method accept.
这篇关于使用RSA兼容.NET的Javascript加密使用充气城堡库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!