Tuesday, May 13, 2014

Legal Key Sizes for Symmetric Cryptographic Algorithms like DES and 3DES

Following code block prints the Legal Key and IV sizes for any Symmetric Algorithms in .Net


public static void PrintLegalKeySizes(string algorithm) {
var des = SymmetricAlgorithm.Create(algorithm);
var lbs = des.LegalBlockSizes;
var lks = des.LegalKeySizes;

Console.WriteLine(new String('-', algorithm.Length));

Console.WriteLine("Legal Block Sizes in bytes: ");
foreach (var item in lbs)

Console.WriteLine("Legal Key Sizes in bytes: ");
foreach (var item in lks)

public static void PrintKeySizes(KeySizes item) {
if (item.SkipSize == 0)
  Console.WriteLine("\t" + (item.MinSize / 8).ToString());
  for (var n = item.MinSize; n <= item.MaxSize; n += item.SkipSize)
   Console.WriteLine("\t" + (n / 8).ToString());

No comments:

Post a Comment