RobLog

Web Design in the World of .NET (C# and VB.NET, XML, and Javascript). I learned how to program from TheDailyWTF.com!
posts - 140, comments - 129, trackbacks - 5

My Links

News

Main Site Cert Corner Goals About Me

Article Categories

Archives

Post Categories

Image Galleries

.NET

Personal

WOW


AutoGenerate A Random Hexadecimal String using RNGCryptoServiceProvider (VB.NET)

Here is a quick way to generate a random set of hexadecimal digits.  Say you are setting up encryption for your Wireless Router traffic, or you need it for some other type of encryption. This provides an easy way to ensure that it is completely random.

Private Sub ShowRandomHexadecimalDigits()
   Dim random() As Byte = New Byte(1024) {}
   'RNGCryptoServiceProvider is an implementation of an RNG
   Dim rng As New System.Security.Cryptography.RNGCryptoServiceProvider
   rng.GetNonZeroBytes(random) ' bytes in random are now random 
   For i As Integer = 0 To random.Length - 1
        Select Case random(i)
        Case 48 To 57, 65 To 70, 97 To 102
             Response.Write(ChrW(random(i)).ToString().ToLower())
        End Select
   Next
End Sub

 

posted on Monday, March 07, 2005 9:52 AM