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

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

    namespace APress.DotNetSecurity.Chapter2.RNGBasics
    {
        class RNGBasicsTester
        {
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("Creating an RNG off of CSP parameters...");
                    CspParameters csp = new CspParameters(1,
                        "Microsoft Enhanced Cryptographic Provider v1.0", "Administrator");
                    RandomNumberGenerator rng = new RNGCryptoServiceProvider(csp);
                    
                    Console.WriteLine("Getting random data...");
                    byte[] randomData = new byte[18];
                    rng.GetBytes(randomData);
                    Console.WriteLine("Retrieved the following random bytes:  " +
                        ArrayToHexString(randomData));

                    Console.WriteLine("Translating the byte data to integer data...");
                    BinaryReader binReader = new BinaryReader(new MemoryStream(randomData));
                    try
                    {
                        int randomValue;
                        do
                        {
                            randomValue = binReader.ReadInt32();
                            Console.WriteLine("New random integer value:  " +
                                randomValue.ToString());
                        } while(true);
                    }
                    catch(EndOfStreamException eose)
                    {
                        Console.WriteLine("Got the expected EndOfStreamException with " +
                            "the stream.");
                    }
                }
                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);
                    retVal.Append(" ");
                }
                retVal.Remove(retVal.Length - 1, 1);

                return retVal.ToString();
            }    
        }
    }
  • 相关阅读:
    WIN7每次从关闭屏幕状态恢复都会挂断宽带连接,请问如何解决?
    程序設計学习之路:不走弯路,就是捷径
    Customize Firefox "Close tab" button
    域名常识
    一到十的英文单词,一十二个月份的英文单词,四季的英文单词,第一,第二第三的英文单词
    Dependency Walker
    刪除當前目錄隱藏文件,非隱藏文件,文件夾等好用的批處理。
    使用 Sandcastle Help File Builder 制作 VS.NET 的 HELP 文件
    字符“23.00”转成int型!Input string was not in a correct format.
    VisualStudio 2010 SP1安装时提示计算机环境导致无法安装的解决办法
  • 原文地址:https://www.cnblogs.com/shihao/p/2511195.html
Copyright © 2011-2022 走看看