Verifies whether the supplied key size is valid for the algorithm. - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:SymmetricAlgorithm

Description

Verifies whether the supplied key size is valid for the algorithm.

Demo Code


using System.Security.Cryptography;
using System;//from ww  w .  j ava2  s  .  c  o m

public class Main{
        /// <summary>
        /// Verifies whether the supplied key size is valid for the algorithm.
        /// </summary>
        /// <param name="keySize">Key size to verify</param>
        /// <returns><code>bool</code></returns>
        public static bool VerifySymmetricKeySize(this SymmetricAlgorithm alg, int keySize)
        {
            // Generate key size list
            var sizeList = new System.Collections.Generic.List<int>();
            for (var i = alg.LegalKeySizes[0].MinSize; i <= alg.LegalKeySizes[0].MaxSize; i += alg.LegalKeySizes[0].SkipSize)
            {
                sizeList.Add(i);
            }

            return sizeList.Contains(keySize);
        }
}

Related Tutorials