177 lines
6.2 KiB
C#
177 lines
6.2 KiB
C#
using Org.BouncyCastle.Crypto;
|
||
using Org.BouncyCastle.Crypto.Parameters;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WX.CRM.Common;
|
||
|
||
namespace WX.CRM.BLL.Util
|
||
{
|
||
public class MSecurityHelper
|
||
{
|
||
public MSecurityHelper()
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 内容根据公钥加密
|
||
/// </summary>
|
||
/// <param name="plainText"></param>
|
||
/// <returns></returns>
|
||
public string content(string plainText, string publicKeyXml)
|
||
{
|
||
//string publicKeyXml = PemToXml(publicKey, false);
|
||
|
||
// 创建 RSA 实例
|
||
using (var rsa = new RSACryptoServiceProvider())
|
||
{
|
||
rsa.FromXmlString(publicKeyXml);
|
||
// 将明文转换为字节数组
|
||
var plainBytes = Encoding.UTF8.GetBytes(plainText);
|
||
|
||
// 使用公钥加密
|
||
var encryptedBytes = rsa.Encrypt(plainBytes, false);
|
||
|
||
// 将加密后的字节数组转换为 Base64 字符串
|
||
var encryptedText = Convert.ToBase64String(encryptedBytes);
|
||
return encryptedText;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据加密内容 私钥 生成签名
|
||
/// </summary>
|
||
/// <param name="content"></param>
|
||
/// <returns></returns>
|
||
public string sign(string content, string privateKeyXml)
|
||
{
|
||
//string privateKeyXml = PemToXml(privateKey, true);
|
||
|
||
var rsa = new RSACryptoServiceProvider();
|
||
rsa.FromXmlString(privateKeyXml);
|
||
//签名返回
|
||
using (var sha256 = new SHA256CryptoServiceProvider())
|
||
{
|
||
var signData = rsa.SignData(Encoding.UTF8.GetBytes(content), sha256);
|
||
return Convert.ToBase64String(signData);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据私钥解密出key
|
||
/// </summary>
|
||
/// <param name="plainText"></param>
|
||
/// <returns></returns>
|
||
public string decyptContent(string plainText, string privateKeyXml)
|
||
{
|
||
//string privateKeyXml = PemToXml(privateKey, true);
|
||
|
||
// 创建 RSA 实例
|
||
using (var rsa = new RSACryptoServiceProvider())
|
||
{
|
||
rsa.FromXmlString(privateKeyXml);
|
||
// 将明文转换为字节数组
|
||
var plainBytes = Convert.FromBase64String(plainText);
|
||
|
||
// 使用公钥加密
|
||
var encryptedBytes = rsa.Decrypt(plainBytes, false);
|
||
Encoding encoding = Encoding.UTF8; // 使用UTF-8编码方式
|
||
string str = encoding.GetString(encryptedBytes);
|
||
return str.ToString();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据公钥验证签名
|
||
/// </summary>
|
||
/// <param name="sign"></param>
|
||
/// <param name="content"></param>
|
||
/// <returns></returns>
|
||
public string decyptSign(string sign, string content, string publicKeyXml, string privateKeyXml)
|
||
{
|
||
//string publicKeyXml = PemToXml(publicKey, false);
|
||
|
||
var rsa = new RSACryptoServiceProvider();
|
||
rsa.FromXmlString(publicKeyXml);
|
||
//签名返回
|
||
using (var sha256 = new SHA256CryptoServiceProvider())
|
||
{
|
||
var cipherbytes = rsa.VerifyData(Encoding.UTF8.GetBytes(content), sha256, Convert.FromBase64String(sign));
|
||
if (cipherbytes)
|
||
{
|
||
return decyptContent(content, privateKeyXml);
|
||
}
|
||
throw new Exception("验证参数失败");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Pem格式密钥转换成Xml格式密钥
|
||
/// </summary>
|
||
/// <param name="pemKey">Pem格式密钥(公钥或私钥)</param>
|
||
/// <param name="isPrivateKey">是否是私钥</param>
|
||
/// <returns>转换后的Xml格式密钥(公钥或私钥)</returns>
|
||
public string PemToXml(string pemKey, bool isPrivateKey)
|
||
{
|
||
string rsaKey = string.Empty;
|
||
object pemObject = null;
|
||
RSAParameters rsaPara = new RSAParameters();
|
||
using (StringReader sReader = new StringReader(pemKey))
|
||
{
|
||
var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(sReader);
|
||
pemObject = pemReader.ReadObject();
|
||
}
|
||
//私钥
|
||
if (isPrivateKey)
|
||
{
|
||
RsaPrivateCrtKeyParameters key = (RsaPrivateCrtKeyParameters)((AsymmetricCipherKeyPair)pemObject).Private;
|
||
rsaPara = new RSAParameters
|
||
{
|
||
Modulus = key.Modulus.ToByteArrayUnsigned(),
|
||
Exponent = key.PublicExponent.ToByteArrayUnsigned(),
|
||
D = key.Exponent.ToByteArrayUnsigned(),
|
||
P = key.P.ToByteArrayUnsigned(),
|
||
Q = key.Q.ToByteArrayUnsigned(),
|
||
DP = key.DP.ToByteArrayUnsigned(),
|
||
DQ = key.DQ.ToByteArrayUnsigned(),
|
||
InverseQ = key.QInv.ToByteArrayUnsigned(),
|
||
};
|
||
}
|
||
//公钥
|
||
else
|
||
{
|
||
RsaKeyParameters key = (RsaKeyParameters)pemObject;
|
||
rsaPara = new RSAParameters
|
||
{
|
||
Modulus = key.Modulus.ToByteArrayUnsigned(),
|
||
Exponent = key.Exponent.ToByteArrayUnsigned(),
|
||
};
|
||
}
|
||
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
|
||
rsa.ImportParameters(rsaPara);
|
||
using (StringWriter sw = new StringWriter())
|
||
{
|
||
sw.Write(rsa.ToXmlString(isPrivateKey ? true : false));
|
||
rsaKey = sw.ToString();
|
||
}
|
||
return rsaKey;
|
||
}
|
||
}
|
||
|
||
public class SignReturnData
|
||
{
|
||
public string content { get; set; }
|
||
public string sign { get; set; }
|
||
}
|
||
|
||
public class MidData
|
||
{
|
||
public int? code { get; set; }
|
||
public string data { get; set; }
|
||
public string message { get; set; }
|
||
}
|
||
} |