Re: 64 bit random numbers
From: Michel Gallant (neutron_at_nspxistar.ca)
Date: 10/21/03
- Previous message: Michael Giagnocavo [MVP]: "Re: 64 bit random numbers"
- In reply to: Michael Giagnocavo [MVP]: "Re: 64 bit random numbers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Tue, 21 Oct 2003 16:51:30 -0400
If you have VJ# redistributable, you have support for displaying any size
byte array to a decimal number.
For example, this sample generates a cryptographically random number (byte
array), displays as formatted hex dump, and then displays the number (big endian
assumed) as a decimal integer using VJ# java.math.BigInteger class.
- Michel Gallant
Visual Security MVP
//*************************************************************************
//
// RandomBig
// Random number generator display utility for .NET
//
// Creates cryptographically strong random number
// Displays as hex bytes
// Displays as decimal integer using VJ#.net java.math.BigInteger support class
// (assumes big endian ordered bytes for conversion to decimal integer)
//
// To compile, requires VJ# redistributable classes.
// e.g. to compile with .NET 1.0:
// csc /r:"c:\WINNT\Microsoft Visual JSharp .NET\Framework\v1.0.4205\vjslib.dll" RandomBig.cs
//
// Copyright (C) 2003. Michel I. Gallant
//
//**************************************************************************
using System;
using System.Security.Cryptography;
using java.math;
public class RandomBig {
const String title = "RandomBig";
public static void Main() {
String[] args = Environment.GetCommandLineArgs();
if(args.Length<2){
Console.WriteLine("Usage: RandomBig [int from 5 to 5000]");
return;
}
String fname = args[1];
int numbytes=0;
try{
numbytes = Int32.Parse(args[1]);
}
catch(FormatException){
Console.WriteLine("Argument not a valid integer");
return;
}
catch(Exception exc){
Console.WriteLine("Problem parsing argument {0}\n{1}", args[1], exc);
return;
}
byte[] ranbytes = new byte[numbytes];
RNGCryptoServiceProvider randgen= new RNGCryptoServiceProvider();
randgen.GetBytes(ranbytes); // The array is now filled with cryptographically strong random bytes.
Console.WriteLine("\n----------------- Random Bytes[{0}]: --------------------------", numbytes);
for(int i=1; i<=ranbytes.Length; i++){
Console.Write("{0:x2} ", ranbytes[i-1]) ;
if(i%16 == 0)
Console.WriteLine("");
}
Console.WriteLine("\n---------------------------------------------------------------");
RandomBig.DisplayBigDecimal(ranbytes);
}
private static void DisplayBigDecimal(byte[] bytes)
{
//------ Display in decimal using J# java BigInteger class -----------
sbyte[] sbytes = new sbyte[bytes.Length+1]; //Java uses signed bytes
sbytes[0] = 0; //set sign byte to zero.
for(int i=1; i<sbytes.Length; i++)
sbytes[i] = (sbyte) bytes[i-1]; //cast byte-->sbyte
BigInteger bigN = new BigInteger(sbytes) ; //requires sbyte array
Console.WriteLine("\nDecimal: ({0} digits)\n{1}", bigN.ToString().Length, bigN.ToString()) ;
}
}
----------------------------------------------------------------------------------------------------
-------
"Michael Giagnocavo [MVP]" <mggUNSPAM@Atrevido.net> wrote in message
news:uYGig8AmDHA.2068@TK2MSFTNGP09.phx.gbl...
> You can use the System.Security.Cryptography.RNGCryptoServiceProvider to get
> a random number generator. Then you should be able to use GetBytes to fill
> a byte array. From there, you can use System.BitConverter to change those
> bytes into UInt64s.
> -mike
> MVP
>
> "george r smith" <gsmith@budgetext.com> wrote in message
> news:uDDBRd0lDHA.976@tk2msftngp13.phx.gbl...
> > I am trying to compute a an array of four 64 bit random numbers
> > but I am not having any luck. Following is my code. The problem is in
> > the line aUInt64 = UInt64.Parse(aString);
> >
> > I thought that since a byte is 8 bits that an array of eight Bytes would
> > give me a 64 bit number but since some of the strings are comming
> > back with 22 characters the program blows up because the number
> > is too large.
> >
> > The docs say that UInt64 (same as ulong) cannot exceed
> > 18,446,744,073,709,551,615 so I think that is my problem.
> >
> > Can some one show me how to compute this array of 64 bit
> > random numbers. Am I using the wrong library.
> >
> > thanks much
> > grs
> >
> > static void Main(string[] args)
> > {
> > byte[] random = new Byte[8];
> > System.UInt64 aUInt64 = 0;
> > int aLength;
> >
> > RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
> >
> > for (int count = 0; count < 4; count++)
> > {
> > rng.GetBytes(random);
> > String aString = "";
> >
> > for (int i = 0; i < 8; i++)
> > {
> > aString = aString + random[i].ToString();
> > }
> >
> > aLength = aString.Length; // for debug just want to see
> length
> > aUInt64 = UInt64.Parse(aString);
> > keys.SetValue(aUInt64,count);
> >
> > Console.WriteLine(aString);
> > }
> > Console.ReadLine();
> > }
> > }
> >
> >
> >
> >
>
>
- Previous message: Michael Giagnocavo [MVP]: "Re: 64 bit random numbers"
- In reply to: Michael Giagnocavo [MVP]: "Re: 64 bit random numbers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|