其他分享
首页 > 其他分享> > .MET Core RSA加密解密

.MET Core RSA加密解密

作者:互联网

RSA加密解密

Nuget 引入 XC.RSAUtil

博客园参考文章1

生成公私钥对

// 生成公私钥对
// XML
Console.WriteLine(RsaKeyGenerator.XmlKey(2048)[0]);
Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Console.WriteLine(RsaKeyGenerator.XmlKey(2048)[1]);
Console.WriteLine("================================================================================");
// Pkcs1
Console.WriteLine(RsaKeyGenerator.Pkcs1Key(2048, true)[0]);
Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Console.WriteLine(RsaKeyGenerator.Pkcs1Key(2048, true)[1]);
Console.WriteLine("================================================================================");
// Pkcs8
Console.WriteLine(RsaKeyGenerator.Pkcs8Key(2048, true)[0]);
Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Console.WriteLine(RsaKeyGenerator.Pkcs8Key(2048, true)[1]);

RSA 加密,解密,签名和验签

/// <summary>
/// RSA 加密,解密,签名和验签
/// </summary>
public static void RSASignFunc(string privateKey, string publicKey, EnumUserType enumUserType)
{
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    switch (enumUserType)
    {
        case EnumUserType.xml:
            RsaXmlUtil rsaXmlUtil = new RsaXmlUtil(Encoding.UTF8, publicKey, privateKey);
            var encrypt = rsaXmlUtil.Encrypt("123456789", RSAEncryptionPadding.Pkcs1);
            var encryptInput = rsaXmlUtil.Decrypt(encrypt, RSAEncryptionPadding.Pkcs1);
            Console.Write("XML加解密是否成功:");
            Console.WriteLine(encryptInput is "123456789");
            var sign = rsaXmlUtil.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            var verifyData = rsaXmlUtil.VerifyData("987654321", sign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            Console.Write("XML签名是否成功");
            Console.WriteLine(verifyData);
            break;
        case EnumUserType.pkcs1:
            RsaPkcs1Util rsaPkcs1Util = new RsaPkcs1Util(Encoding.UTF8, publicKey, privateKey);
            var encrypt2 = rsaPkcs1Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1);
            var encryptInput2 = rsaPkcs1Util.Decrypt(encrypt2, RSAEncryptionPadding.Pkcs1);
            Console.Write("PKCS1加解密是否成功:");
            Console.WriteLine(encryptInput2 is "123456789");
            var sign2 = rsaPkcs1Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            var verifyData2 = rsaPkcs1Util.VerifyData("987654321", sign2, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            Console.Write("PKCS1签名是否成功");
            Console.WriteLine(verifyData2);
            break;
        default:
            RsaPkcs8Util rsaPkcs8Util = new RsaPkcs8Util(Encoding.UTF8, publicKey, privateKey);
            var encrypt3 = rsaPkcs8Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1);
            var encryptInput3 = rsaPkcs8Util.Decrypt(encrypt3, RSAEncryptionPadding.Pkcs1);
            Console.Write("PKCS8加解密是否成功:");
            Console.WriteLine(encryptInput3 is "123456789");
            var sign3 = rsaPkcs8Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            var verifyData3 = rsaPkcs8Util.VerifyData("987654321", sign3, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            Console.Write("PKCS8签名是否成功");
            Console.WriteLine(verifyData3);
            break;
    }
}

RSA密钥转换

XML-> Pkcs1:
私钥:RsaKeyConvert.PrivateKeyXmlToPkcs1()
公钥:RsaKeyConvert.PublicKeyXmlToPem()

XML-> Pkcs8:
私钥:RsaKeyConvert.PrivateKeyXmlToPkcs8()
公钥:RsaKeyConvert.PublicKeyXmlToPem()

Pkcs1-> XML:
私钥:RsaKeyConvert.PrivateKeyPkcs1ToXml()
公钥:RsaKeyConvert.PublicKeyPemToXml()

Pkcs1-> Pkcs8:
私钥:RsaKeyConvert.PrivateKeyPkcs1ToPkcs8()
公钥:不需要转换

Pkcs8-> XML:
私钥:RsaKeyConvert.PrivateKeyPkcs8ToXml()
公钥:RsaKeyConvert.PublicKeyPemToXml()

Pkcs8-> Pkcs1:
私钥:RsaKeyConvert.PrivateKeyPkcs8ToPkcs1()
公钥:不需要转换

标签:XML,Core,Console,RsaKeyConvert,RSA,MET,WriteLine,var,Pkcs1
来源: https://www.cnblogs.com/ainghub/p/15796392.html