Reputation: 250
I am in the midst of writing a simple encryptor/decryptor program and have run into a small issue. All the characters are being generated a different string of characters to uniquely identify them. These strings are meant to be all the same length but don't seem to be. The code in question: (Full code can be viewed here)
Dim genLength As Integer = 0
Dim charNow As Integer = 0
Dim charGenned As String
Dim rndGend As Integer = 0
While genLength < enhancedMode
fs.Write(arrayAll(charNow))
Dim rndString As String = ""
While rndGend < encLength
Randomize()
Dim genned As Integer = CInt(Math.Ceiling(Rnd() * 46)) + 1
charGenned = arrayAll(genned)
rndString = rndString + charGenned
rndGend += 1
End While
fs.Write(rndString & vbNewLine)
rndGend = 0
charNow = charNow + 1
genLength = genLength + 1
End While
From what I can tell this should give me the result I am looking for, but the generated strings are not at all consistent in length. A sample of the output:
alh*ph)lufe$2fz!d7c0$qfd(ol6f173#
b^i24@^v0gx%01iqrpugg8)(mqsl8%
c1km5jnz0hti&u$#rqeh5ism31t^96^
dkx&6$ok!@u#*e^x6659jpvcnn258zpi
e%y1(y3%@w9kk9&h7d6gw)w72*3c9*d)j
fy#(i4yeg0%ltj@887!x4!e32^703e4l
gj$4#5&f!!zzdkvs)v@@94)*rcmroy
While the string after the letter A is 32 digits long, as is for B, and C, when you get to G, the string is only 29 characters long. It is most noticeable in the fact that the program only generates strings for characters up to numeral 5, then stops:
3%y1(y3%@w9kk9&h7d6gw)w72*3c9*d)j
4y#(i4yeg0%ltj@887!x4!e32^703e4l
5j$4#5&f!!zzdkvs)
What is going amiss here?
Upvotes: 1
Views: 75
Reputation: 15091
I just tidied up a bit and changed to the Random class which I think is much easier to use. If you declaring an array in vb.net remember it is Dim variable(ubound) As Type. ubound stands for the Upper bound of the array, the highest index so an array with 47 elements would have indexes 0-46. The ubound would be 46 Dim variable(46) As String
Private r As New Random
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
Dim arrayLetters() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Dim arrayAll() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")"}
Dim encLength As Integer = 16
If CheckBox2.Checked = True Then
encLength = 32
End If
Dim enhancedMode As Integer = 26
If CheckBox1.Checked = True Then
enhancedMode = 46
End If
Dim genLength As Integer = 0
Dim charNow As Integer = 0
Dim charGenned As String
Dim rndGend As Integer = 0
Dim sb As New StringBuilder
While genLength < enhancedMode
sb.Append(arrayAll(charNow))
Dim rndString As String = ""
While rndGend < encLength
Dim genned As Integer = r.Next(0, 46)
charGenned = arrayAll(genned)
rndString &= charGenned
rndGend += 1
End While
sb.AppendLine(rndString)
rndGend = 0
charNow += 1
genLength += 1
End While
Dim name As String
If TextBox1.Text = "" Then
name = "KeyGenned"
Else
name = TextBox1.Text
End If
Dim path As String = Application.StartupPath & "\" & name & ".txt"
File.WriteAllText(path, sb.ToString)
Close()
End Sub
Upvotes: 2