/// <summary> /// Returns <c>true</c> if the key string passed is a valid XML key /// and is NOT a key container name. /// </summary> /// <param name="algorithm">The asymmetric algorithm.</param> /// <param name="key">The key string to be tested.</param> /// <returns><c>true</c> if the key is a valid XML key.</returns> /// <remarks> /// The current implementation supports only the "RSA" provider. /// </remarks> public static bool IsXmlKey(string algorithm, string key) { AsymmetricAlgorithm asymmetric = null; try { if (ParseKeyContainer(key) != null) { return(false); } asymmetric = EncryptionConfig.CreateAsymmetric(algorithm, 0); asymmetric.FromXmlString(key); return(true); } catch { return(false); } finally { if (asymmetric != null) { asymmetric.Clear(); } } }
/// <summary> /// Generates an asymmetric private key and returns the result as XML. /// </summary> /// <param name="algorithm">The asymmetric algorithm name.</param> /// <param name="keySize">The key size in bits.</param> /// <returns>The private key encoded as XML.</returns> /// <remarks> /// The current implementation supports only the "RSA" provider. /// </remarks> public static string CreatePrivateKey(string algorithm, int keySize) { var asymmetric = EncryptionConfig.CreateAsymmetric(algorithm, keySize); try { return(asymmetric.ToXmlString(true)); } finally { asymmetric.Clear(); } }
/// <summary> /// Returns the public key for a private key. /// </summary> /// <param name="algorithm">The asymmetric algorithm.</param> /// <param name="privateKeyXml">The private key encoded as XML.</param> /// <returns>The public key encoded as XML.</returns> /// <remarks> /// The current implementation supports only the "RSA" provider. /// </remarks> public static string GetPublicKey(string algorithm, string privateKeyXml) { var asymmetric = EncryptionConfig.CreateAsymmetric(algorithm, 0); try { asymmetric.FromXmlString(privateKeyXml); return(asymmetric.ToXmlString(false)); } finally { asymmetric.Clear(); } }