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();
        }    
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值