// // System.Security.Cryptography AsymmetricAlgorithm Class implementation // // Authors: // Thomas Neidhart (tome@sbox.tugraz.at) // using System; namespace System.Security.Cryptography { /// /// Abstract base class for all cryptographic asymmetric algorithms. /// Available algorithms include: /// RSA, DSA /// public abstract class AsymmetricAlgorithm { protected int KeySizeValue; // The size of the secret key used by the symmetric algorithm in bits. protected KeySizes[] LegalKeySizesValue; // Specifies the key sizes that are supported by the symmetric algorithm. /// /// Called from constructor of derived class. /// protected AsymmetricAlgorithm () { throw new CryptographicException(); } /// /// Gets the key exchange algorithm /// public abstract string KeyExchangeAlgorithm {get;} /// /// Gets or sets the actual key size /// public virtual int KeySize { get { return this.KeySizeValue; } set { if (!IsLegalKeySize(this.LegalKeySizesValue, value)) throw new CryptographicException("key size not supported by algorithm"); this.KeySizeValue = value; } } /// /// Gets all legal key sizes /// public virtual KeySizes[] LegalKeySizes { get { return this.LegalKeySizesValue; } } /// /// Gets the signature algorithm /// public abstract string SignatureAlgorithm {get;} /// /// Reconstructs the AsymmetricAlgorithm Object from an XML-string /// public abstract void FromXmlString(string xmlString); /// /// Returns an XML string representation the current AsymmetricAlgorithm object /// public abstract string ToXmlString(bool includePrivateParameters); private bool IsLegalKeySize(KeySizes[] LegalKeys, int Size) { foreach (KeySizes LegalKeySize in LegalKeys) { for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) { if (i == Size) return true; } } return false; } /// /// Checks wether the given keyLength is valid for the current algorithm /// /// the given keyLength public bool ValidKeySize(int bitLength) { return IsLegalKeySize(LegalKeySizesValue, bitLength); } /// /// Creates the default implementation of the default asymmetric algorithm (RSA). /// public static AsymmetricAlgorithm Create () { return RSA.Create();; } /// /// Creates a specific implementation of the given asymmetric algorithm. /// /// the given algorithm public static AsymmetricAlgorithm Create (string algName) { // TODO: use reflection to create a new instance of the given algorithm return null; } } }