. NET Private Key RSA Encryption

Muszę zaszyfrować łańcuch za pomocą algorytmu RSA 1.5. Otrzymałem klucz prywatny. Jednak nie mogę na całe życie wymyślić, jak dodać ten klucz do klasy. Wydaje się, że klucz musi być typu rsaparametr stuct. Wymaga to jednak zestawu wartości, które nie zostały podane, takich jak moduł, wykładnik, P, Q, itp.. Mam tylko klucz prywatny. Czy ktoś może pomóc?

Author: John Saunders, 2011-05-17

2 answers

Powinieneś być świadomy Bouncycastle C# library. Istnieją w szczególności dwie bardzo użyteczne klasy: Org.BouncyCastle.OpenSsl.PemReader, które przekonwertują z klucza w stylu OpenSSL do obiektu klucza bouncycastle oraz Org.BouncyCastle.Security.DotNetUtilities, które przekonwertują klucz bouncycastle do obiektu.NET RSAParameters.

Oto mały kawałek nietestowanego kodu, który pokazuje, jak go używać

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);
        }
    }
}
 26
Author: James K Polk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-18 01:28:57

Chyba tego szukasz:

    // Import ASymmetric RSA Key from a system file.
    public static RSAParameters ImportRSAKey(String fileName)
    {

        // Create a stream to a the specified system file.
        Stream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

        // Extract/Deserialize the key from the file.
        IFormatter soapFormatter = new SoapFormatter();            
        RSAParameters rsaParameter = 
           (RSAParameters) soapFormatter.Deserialize(fileStream);

        // Close the file stream.
        fileStream.Close();

        return rsaParameter;

    }

Aby wygenerować nowy klucz, możesz użyć RSACryptoServiceProvider.ExportParameters method.


Zobacz:

Struktura Rsaparametrów

 5
Author: Akram Shahda,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-17 11:42:51