1.什么是公钥加密私钥解密
简单一点来说一般加密解密都用的是同一个秘钥或者根本不用,而这里采用的是加密用一个秘钥,解密用另一个秘钥且能解密成功.这就属于不对称加密解密算法的一种了.
2.公钥秘钥的生成(可以git中生成)
由于这种加密方案,公钥秘钥是成对的,所以需要一些工具生成
利用 openssl 生成公钥私钥
生成公钥: openssl genrsa -out rsa_private_key.pem 1024
生成私钥: openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
RSA密钥生成命令
生成RSA私钥
openssl genrsa -out rsa_private_key.pem 1024
生成RSA公钥
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
将RSA私钥转换成PKCS8格式
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out pkcs8.pem
注意:“>”符号后面的才是需要输入的命令。
https://blog.csdn.net/cuiran/article/details/104393677
public partial class DebugRsaController : BaseController
{
private readonly IRsaProvider _rsaProvider;
/// <summary>
///
/// </summary>
public DebugRsaController(IRsaProvider rsaProvider)
{
_rsaProvider = rsaProvider;
}
/// <summary>
///
/// </summary>
/// <param name="body"></param>
/// <returns></returns>
[HttpPost()]
public async Task<IActionResult> Post([FromBody] JToken body)
{
//PKCS8格式私钥
string strPriPkcs8 = "MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAMz0Czg6QUtTISa2pUkloeQB/TEpHdqrfyroWpKLW9B/LWFSOGH9nyTk1pPZaeadyEZQ6gay/C0pUAetLraq9bMA/Luxq68b87uG7WX7dKytEO2/87qGpGMRs97H+GlkzWil2QO2KK4cHnAcVicPsmi5aZ72U0BWJFyPhtd+qdmrAgMBAAECgYEAvW67iAbgHt0BASVD9C3iSjpEaVHVlC165o/IVzaTcEx8Bz3Ve0zN8W3JnvIO3ebsG4HiLLr2Nk++9rltOc0eNeGMv7F1e/OFot1wN0ON6s1g4bYh1z5Uz8FcYiMWcqHHICrx+oSFeK9x+I2Zge7enQXcsVnqEhm77ZE5YczSryECQQD9nB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N7YjzldufHrG4zJWHklDaaWS+C4FZ1zAkEAuy77PUJfhjwwKqO06BDNZZiAkfdOkDyAR7l/tbCd8KWDETlrpsD2u2UR9+o+pbPukeTsHIJqVnrhNYGE6wTbAQJBAJ9qRLQJY19X6EH3+gi6M2IPyeifUZlRXCeqs9C199OdqjRQUGMPAjBwe1ZFTc7AyAwvC1+bj9qc063Ty3sPqTw=";
//公钥
string strPub = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDM9As4OkFLUyEmtqVJJaHkAf0xKR3aq38q6FqSi1vQfy1hUjhh/Z8k5NaT2WnmnchGUOoGsvwtKVAHrS62qvWzAPy7sauvG/O7hu1l+3SsrRDtv/O6hqRjEbPex/hpZM1opdkDtiiuHB5wHFYnD7JouWme9lNAViRcj4bXfqnZqwIDAQAB";
string strDJM = "泰酷拉!123ABC";//待加密字符串
strDJM = "泰酷拉";//待加密字符串,超过117字符的测试
Console.WriteLine("待加密字符串:" + strDJM);
string charSet = "UTF-8";
string strJMH = _rsaProvider.Encrypt(strDJM, charSet, strPub);//密文
Console.WriteLine("密文:" + strJMH);
string strDecryptedByPkcs8 = _rsaProvider.Decrypt(strJMH, charSet, strPriPkcs8, "PKCS8");//使用PKCS8格式私钥解密
Console.WriteLine("PKCS8 解密后:" + strDecryptedByPkcs8);
string strDecryptedByPkcs1 = _rsaProvider.Decrypt(strJMH, charSet, strPriPkcs1, "PKCS1");//使用PKCS1格式私钥解密
Console.WriteLine("PKCS1 解密后:" + strDecryptedByPkcs1);
return await Task.Run<IActionResult>(() => new JsonResult(new ResultDetail<object> { Data = new { message = "生成成功" }, Success = true }));
}
}
文档更新时间: 2024-03-23 17:10 作者:admin