.NET Core RSA PKCS8 格式 签名/验签

RSA 信息

PKCS密钥格式15个标准
公钥/私钥生成工具
支付宝密钥工具

RSAHelp

Nuget包:Install-Package BouncyCastle.NetCore -Version 1.8.8

public static class RsaHelp
    {
        private static AsymmetricKeyParameter GetPublicKeyParameter(string s)
        {
            s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            byte[] publicInfoByte = Convert.FromBase64String(s);
            Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);
            AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
            return pubKey;
        }
        private static AsymmetricKeyParameter GetPrivateKeyParameter(string s)
        {
            s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            byte[] privateInfoByte = Convert.FromBase64String(s);
            AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
            return priKey;
        }

        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="key">密匙</param>
        /// <returns></returns>
        public static string SignByPrivateKey(string data, string key)
        {
            AsymmetricKeyParameter priKey = GetPrivateKeyParameter(key);
            byte[] byteData = System.Text.Encoding.UTF8.GetBytes(data);

            ISigner normalSig = SignerUtilities.GetSigner("SHA1WithRSA");
            normalSig.Init(true, priKey);
            normalSig.BlockUpdate(byteData, 0, data.Length);
            byte[] normalResult = normalSig.GenerateSignature(); //签名结果
            return Convert.ToBase64String(normalResult);
        }

        /// <summary>
        /// 验签
        /// </summary>
        /// <param name="plainData">验证数据</param>
        /// <param name="sign">签名</param>
        /// <param name="key">公匙</param>
        /// <returns></returns>
        public static bool ValidationPublicKey(string plainData, string sign, string key)
        {
            AsymmetricKeyParameter priKey = GetPublicKeyParameter(key);

            byte[] signBytes = Convert.FromBase64String(sign);
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainData);


            ISigner verifier = SignerUtilities.GetSigner("SHA1WithRSA");
            verifier.Init(false, priKey);
            verifier.BlockUpdate(plainBytes, 0, plainBytes.Length);

            return verifier.VerifySignature(signBytes); //验签结果
        }
    }

使用示例

RSA 密钥长度:2048位
RSA 密钥格式:PKCS#8

using System;
using System.Text;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;

namespace TestSingleFile
{
    class Program
    {
        static void Main(string[] args)
        {
            var key =
@"MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQDdBtJb7at1DG7u
eeihR++VKU5KYlmaHH6mWBCucrEEF0DhDPnjn6tVkY4M83 + HX1oOjaa6m7jDRjVZ
v24wCizK7K0kRCnlPP2K2wu1hj9c7 + mgTpU7ugIZwqIz7mqvyO9QC26QbOVGL / WR
mgIAcWo2 / 4Y0 + n + xVDS7i + hVvqQdMSEU9McyM4 + CqIaPXuTYI48Md0 / dfX3r + hLf
HGPBEG40OmrruiuJRMmXiaJulC4fBPsw6arYqnupnMcDTZkW0d32Nv / kb98q46B5
//oYv1QQXzJzPjT8oV8JtasXO9URtmi0Jgb2JUcxz2xsc9JCnceDQKqoDcLHSoMx
zzokj / utAgMBAAECggEBAKxY6Gk0b7EKq + ma9 / HjJQebuOe5vwKz23IcU42j8zIs
a7PRKX38AF7t6 + Vfbf8CNDgK7oquaKkkMu + znNkq5fiqCuyvljehklbXLU8zym7O
yM7u1 + FFzuOFaHO0Ik7GEQ3KjEFkGHfdZ9BMVelAhsEyCNWAy1dcQO5JTzalPFVh
iciuuZTO60Lpea7nrGoONLmMYWvzFDS6X + Z5wlwuCC8aMSAfnKMSpEZag5Ggk83j
gh9VjK1GnIENYR8xtO8fU6RWnRP27BMkp8 + ppsK598U1G / 0GMh4ZZAI0iTw6 / i6C
PtcU10jIJlzkUMRvhhQkQhOnKE2OeKKtP6rqDNS7mUECgYEA81Bkc26T9 / qPsCwE
PawL5ek6 / kpLt6CsBlfM3o1uIgQIYfR6qmQ73FYyod + vhvvgYuTQFac43q5mcHy1
iI9TO + SKWZCt6G7QTw12pjoi4VtmdmXdpKlNjER3FRR9ae + gd8EocT + AqZN2CrDM
sTXe8yqruCjtRm2IGRhPlJZG9tECgYEA6IzzbHLAsHRmqXN5DdP8xp6mHobQP8FX
MXBxqnPb0yStP0 / sILGKJ6VYak2EwlECHpo9VylXjhCRfBvtgsQ9Cjzx8uyltxBP
t7x4dqsqPoWteJQpYOYwKQQSHpnE0ERp4iyvvEnVsB0Gl3S / P0nrbYlkpBWfhoTm
2MS5c9ZzJh0CgYEA3LgR17D449xVGnBjjHqU9IRlsqY9F5 / WTY66Vy0fOhZEcG + y
Ev4IW3GKTtO2zBGTVMLSjMH8Ue0NGn / WwcGatNs2MdeQGyjjKMNC3bohSCN8yGzR
6nx2z4aQK / xBZKcu / 1Om10zO74XIcBxAI4WF8TrFObNOwzO7YWBvWJAWYfECgYEA
kn3YCgTvpThuQ2heZXuYzHW8J5a9MKxWhOpUcA + z9nowOKXcFWxBn1 / lJnHWo6Hs
N + LpqTJ92RxOyWO + ZpW / 7ececuzoJz27Of8b04VXgzMqZ77NoiDfTbcmwugSty + h
k7yap33LgrmGIXqFzxR9ne7i2wpUB6VI1lx2hqYcu3ECgYEAu921JkIfbDaF4l4I
a2i5xQRy5EX5CTBVHzZS + 4TGZCLN9rygm5Yk / 8WHZuLNhdkEEr6t / Q9NA6SpMkPu
z0D2LPVdvqeWYQJQhrBn3P / +zbmjUVWcVZaFAAhZuMQuTnvKXg6l8jSgiy1frAHo
cwGiXqN3pneg87M / tGPLArNQPto = ";
            var publicKey = @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3QbSW+2rdQxu7nnooUfv
lSlOSmJZmhx + plgQrnKxBBdA4Qz545 + rVZGODPN / h19aDo2mupu4w0Y1Wb9uMAos
yuytJEQp5Tz9itsLtYY / XO / poE6VO7oCGcKiM + 5qr8jvUAtukGzlRi / 1kZoCAHFq
Nv + GNPp / sVQ0u4voVb6kHTEhFPTHMjOPgqiGj17k2COPDHdP3X196 / oS3xxjwRBu
NDpq67oriUTJl4mibpQuHwT7MOmq2Kp7qZzHA02ZFtHd9jb / 5G / fKuOgef / 6GL9U
EF8ycz40 / KFfCbWrFzvVEbZotCYG9iVHMc9sbHPSQp3Hg0CqqA3Cx0qDMc86JI / 7
rQIDAQAB";
            var data = "123321124567";

            var sign = RsaHelp.SignByPrivateKey(data, key);
            Console.WriteLine(RsaHelp.ValidationPublicKey(data, sign, publicKey));
        }
    }
}

测试截图
测试截图

posted @ 2021-09-28 23:03  刘大大。  阅读(1254)  评论(0编辑  收藏  举报