RSACryptoServiceProvider.FromXmlString

public sealed class RSACryptoServiceProvider : RSA, ICspAsymmetricAlgorithm

// System.Security.Cryptography.RSA
/// <summary>Initializes an <see cref="T:System.Security.Cryptography.RSA" /> object from the key information from an XML string.</summary>
/// <param name="xmlString">The XML string containing <see cref="T:System.Security.Cryptography.RSA" /> key information.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="xmlString" /> parameter is <see langword="null" />.</exception>
/// <exception cref="T:System.Security.Cryptography.CryptographicException">The format of the <paramref name="xmlString" /> parameter is not valid.</exception>
/// <exception cref="T:System.PlatformNotSupportedException">.NET Core only: This member is not supported.</exception>
// Token: 0x06002293 RID: 8851 RVA: 0x0007C3A4 File Offset: 0x0007A5A4
public override void FromXmlString(string xmlString)
{
    if (xmlString == null)
    {
        throw new ArgumentNullException("xmlString");
    }
    RSAParameters parameters = default(RSAParameters);
    Parser parser = new Parser(xmlString);
    SecurityElement topElement = parser.GetTopElement();
    string text = topElement.SearchForTextOfLocalName("Modulus");
    if (text == null)
    {
        throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[]
        {
            "RSA",
            "Modulus"
        }));
    }
    parameters.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text));
    string text2 = topElement.SearchForTextOfLocalName("Exponent");
    if (text2 == null)
    {
        throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[]
        {
            "RSA",
            "Exponent"
        }));
    }
    parameters.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text2));
    string text3 = topElement.SearchForTextOfLocalName("P");
    if (text3 != null)
    {
        parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text3));
    }
    string text4 = topElement.SearchForTextOfLocalName("Q");
    if (text4 != null)
    {
        parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text4));
    }
    string text5 = topElement.SearchForTextOfLocalName("DP");
    if (text5 != null)
    {
        parameters.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text5));
    }
    string text6 = topElement.SearchForTextOfLocalName("DQ");
    if (text6 != null)
    {
        parameters.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text6));
    }
    string text7 = topElement.SearchForTextOfLocalName("InverseQ");
    if (text7 != null)
    {
        parameters.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text7));
    }
    string text8 = topElement.SearchForTextOfLocalName("D");
    if (text8 != null)
    {
        parameters.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text8));
    }
    this.ImportParameters(parameters);
}
// System.Security.Cryptography.RSACryptoServiceProvider
/// <summary>Imports the specified <see cref="T:System.Security.Cryptography.RSAParameters" />.</summary>
/// <param name="parameters">The parameters for <see cref="T:System.Security.Cryptography.RSA" />.</param>
/// <exception cref="T:System.Security.Cryptography.CryptographicException">The cryptographic service provider (CSP) cannot be acquired.  
///  -or-  
///  The <paramref name="parameters" /> parameter has missing fields.</exception>
// Token: 0x060022B7 RID: 8887 RVA: 0x0007CB10 File Offset: 0x0007AD10
[SecuritySafeCritical]
public override void ImportParameters(RSAParameters parameters)
{
    if (this._safeKeyHandle != null && !this._safeKeyHandle.IsClosed)
    {
        this._safeKeyHandle.Dispose();
        this._safeKeyHandle = null;
    }
    RSACspObject cspObject = RSACryptoServiceProvider.RSAStructToObject(parameters);
    this._safeKeyHandle = SafeKeyHandle.InvalidHandle;
    if (RSACryptoServiceProvider.IsPublic(parameters))
    {
        Utils._ImportKey(Utils.StaticProvHandle, 41984, CspProviderFlags.NoFlags, cspObject, ref this._safeKeyHandle);
        return;
    }
    if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
    {
        KeyContainerPermission keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
        KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(this._parameters, KeyContainerPermissionFlags.Import);
        keyContainerPermission.AccessEntries.Add(accessEntry);
        keyContainerPermission.Demand();
    }
    if (this._safeProvHandle == null)
    {
        this._safeProvHandle = Utils.CreateProvHandle(this._parameters, this._randomKeyContainer);
    }
    Utils._ImportKey(this._safeProvHandle, 41984, this._parameters.Flags, cspObject, ref this._safeKeyHandle);
}
原文地址:https://www.cnblogs.com/chucklu/p/14004622.html