zoukankan      html  css  js  c++  java
  • SymmetricAlgorithmEvaluator.cs

    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    namespace APress.DotNetSecurity.Chapter2.SymmetricAlgorithmEvaluator
    {
     class SymmetricAlgorithmEvaluatorTester
     {
      static void Main(string[] args)
      {
       try
       {
        Console.WriteLine("Creating a RijndaelManaged instance...");
        SymmetricAlgorithm symAlg = new RijndaelManaged();
        
        symAlg.GenerateIV();
        Console.WriteLine("Generated IV value is " +
         ArrayToHexString(symAlg.IV));

        Console.WriteLine("Block size is " + symAlg.BlockSize.ToString());
        Console.WriteLine("Key size is " + symAlg.KeySize.ToString());
        Console.WriteLine("Key value is " +
         ArrayToHexString(symAlg.Key));

        foreach(KeySizes ks in symAlg.LegalKeySizes)
        {
         int currentSize = ks.MinSize;

         do
         {
          Console.WriteLine("Legal key size:  " + currentSize.ToString());
          currentSize += ks.SkipSize;
         } while(currentSize <= ks.MaxSize);
        }

        Console.WriteLine("Mode is " + symAlg.Mode.ToString());
        Console.WriteLine("Padding type is " + symAlg.Padding.ToString());

        Console.WriteLine("Input the desired key size:");
        int desiredKeySize = Int32.Parse(Console.ReadLine());
        KeySizesEx kse = new KeySizesEx(symAlg.LegalKeySizes);
        
        if(true == kse.IsValidKeySize(desiredKeySize))
        {
         Console.WriteLine(desiredKeySize.ToString() +
          " is a valid key size.");
        }
        else
        {
         Console.WriteLine(desiredKeySize.ToString() +
          " is NOT a valid key size.");
        }
       }
       catch(CryptographicUnexpectedOperationException cuoe)
       {
        Console.WriteLine("CryptographicUnexpectedOperationException:  "
         + cuoe.Message);
        Console.WriteLine(cuoe.StackTrace);
       }
       catch(CryptographicException ce)
       {
        Console.WriteLine("CryptographicException:  " + ce.Message);
        Console.WriteLine(ce.StackTrace);
       }
       catch(Exception ge)
       {
        Console.WriteLine("Exception:  " + ge.GetType().Name + " " + ge.Message);
        Console.WriteLine(ge.StackTrace);
       }
       finally
       {
        Console.WriteLine("Press the return key to continue...");
        Console.Read();
       }
      }
      private static String ArrayToHexString(byte[] ByteData)
      {
       StringBuilder retVal = new StringBuilder();
       
       foreach(byte b in ByteData)
       {
        retVal.Append(b.ToString("X2"));
        retVal.Append(" ");
       }
       retVal.Remove(retVal.Length - 1, 1);

       return retVal.ToString();
      } 
     }

     public class KeySizesEx
     {
      private KeySizes[] ks = null;

      public KeySizesEx(KeySizes[] ks)
      {
       this.ks = ks;
      }

      public bool IsValidKeySize(int DesiredSize)
      {
       bool retVal = false;

       foreach(KeySizes keySize in ks)
       {
        if(DesiredSize >= keySize.MinSize &&
         DesiredSize <= keySize.MaxSize &&
         (DesiredSize % keySize.SkipSize) == 0)
        {
         retVal = true;
         break;
        }
       }

       return retVal;
      }
     }
    }

  • 相关阅读:
    发布 Rafy .NET Standard 版本 Nuget 包
    使用 MarkDown & DocFX 升级 Rafy 帮助文档
    apache2服务器支持cgi功能
    百兆网口与千兆网口速率协商不成功
    ubuntu etho0 up cron
    linux 后台进程
    MySQL的事务性
    linux下visual studio code配置c++调试环境实例
    linux下visual studio code中gdb调试文件launch.json解析
    Zookeeper安装后,编译C client时报错"syntax error near unexpected token `1.10.2"
  • 原文地址:https://www.cnblogs.com/dushu/p/2511209.html
Copyright © 2011-2022 走看看