本文介绍了.NET私钥的RSA加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用RSA 1.5算法加密字符串。我一直在提供私人密钥。但是,我不能为我的生活弄清楚如何这个键添加到类。它似乎寿的关键必须是类型RSAParameter stuct的。然而,这需要一套价值观我没有得到,如模量,指数,P,Q等。所有我已经是私钥。任何人都可以帮忙吗?

I need to encrypt a string using an RSA 1.5 algorithm. I have been provided with a private key. However, I cannot for the life of me figure out how to add this key to the class. It seems as tho the key needs to be of type RSAParameter stuct. However this requires a set of values I have not been given such as Modulus, Exponent, P, Q, etc.. All I have is the private key. Can anyone help?

推荐答案

您应该知道的 BouncyCastle的C#库的。有特别两个非常有用的类: Org.BouncyCastle.OpenSsl.PemReader 这将从OpenSSL的风格键,你必须一个BouncyCastle的重点对象转换,以及 Org.BouncyCastle.Security.DotNetUtilities ,将一个BouncyCastle的关键所在。NET转换 RSAParameters 对象。

You should be aware of the Bouncycastle C# library. There are in particular two very useful classes: Org.BouncyCastle.OpenSsl.PemReader which will convert from the openssl style key you have to a bouncycastle key object, and Org.BouncyCastle.Security.DotNetUtilities, which will convert a bouncycastle key to a .NET RSAParameters object.

下面是未经考验code一点点,演示如何使用它

Here is a tiny bit of untested code that shows how to use it

using System;
using System.IO;
using System.Security.Cryptography;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;

namespace RSAOpensslToDotNet
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader("../../privatekey.pem");
            PemReader pr = new PemReader(sr);
            AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject();
            RSAParameters rsa = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private);
        }
    }
}

这篇关于.NET私钥的RSA加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 22:11