【文件属性】:
文件名称:js库RSA加密 .net Javascript 可用
文件大小:7KB
文件格式:RAR
更新时间:2019-11-25 11:37:14
js加密
用javascript进行RSA加密并用C#后台接收解码。
///
/// 产生一组RSA公钥、私钥
///
///
public static Dictionary CreateRsaKeyPair()
{
var keyPair = new Dictionary();
var rsaProvider = new RSACryptoServiceProvider(1024);
RSAParameters parameter = rsaProvider.ExportParameters(true);
keyPair.Add("PUBLIC", BytesToHexString(parameter.Exponent) + "," + BytesToHexString(parameter.Modulus));
keyPair.Add("PRIVATE", rsaProvider.ToXmlString(true));
keyPair.Add("PUBLICKEY", rsaProvider.ToXmlString(false));
return keyPair;
}
///
/// RSA解密字符串
///
/// 密文
/// 私钥
/// 明文
public static string DecryptRSA(string encryptData, string privateKey)
{
string decryptData = "";
try
{
var provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKey);
byte[] result = provider.Decrypt(HexStringToBytes(encryptData), false);
ASCIIEncoding enc = new ASCIIEncoding();
decryptData = enc.GetString(result);
}
catch (Exception e)
{
throw new Exception("RSA解密出错!", e);
}
return decryptData;
}
public static string BytesToHexString(byte[] input)
{
StringBuilder hexString = new StringBuilder(64);
for (int i = 0; i < input.Length; i++)
{
hexString.Append(String.Format("{0:X2}", input[i]));
}
return hexString.ToString();
}
public static byte[] HexStringToBytes(string hex)
{
if (hex.Length == 0)
{
return new byte[] { 0 };
}
if (hex.Length % 2 == 1)
{
hex = "0" + hex;
}
byte[] result = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length / 2; i++)
{
result[i] = byte.Parse(hex.Substring(2 * i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
return result;
}
【文件预览】:
NetRSA
----BigInt.js(15KB)
----RSA.js(4KB)
----Barrett.js(2KB)