I don't have an issue with this code, but I thought that someone might could use it or improve on it:
Have you ever needed a randomly generated set of numbers (12345-12347-1234) etc, but to have a little bit above average security yet still be easy to implement into your code, then maybe this will help you out.
This code creates a set of 3 randomly generated numbers plus the year, but the catch is that the first set of numbers adds up to a set number, the second set adds up to a different number and the third set adds up to a different number. then the numbers are split so that they can be verified that they add up to each correct variable.
so for instance the random number set generated is: 65612-1514-9325-2017 | 65612 - the first set adds up to 20, 1514 - the second set adds up to 11, 9325 - the third set adds up to 19 and the final number is the current year (or can be optional)
first we need to create a function to generate a random set of numbers:
then to create the function that will give us the values for each set of numbers, these values can be set in the following code, or you can also have the user set these variables in the registry or settings.Code:Function grs(ByRef length As Integer) As String Randomize() Dim ac As String ac = "0123456789" Dim i As Integer For i = 1 To length grs = grs & Mid$(ac, Int(Rnd() * Len(ac) + 1), 1) Next End Function
Since we used "-" to separate the number sets in the function above, we can Split these and then verify that they are correct. This code just Displays the result, I have not written any code that will "Verify" if the variables below (c1,c2 or c3) add up to the same as the variables set above (should'nt be that hard).Code:Function cypt() Dim a1, a2, a3 As Integer ' Variable for each SET of numbers Dim va, vb, vc, vd, ve As Integer Do Until Val(va) + Val(vb) + Val(vc) + Val(vd) + Val(ve) = 20 va = CInt(Int((9 * Rnd()) + 1)) vb = CInt(Int((9 * Rnd()) + 1)) vc = CInt(Int((9 * Rnd()) + 1)) vd = CInt(Int((9 * Rnd()) + 1)) ve = CInt(Int((9 * Rnd()) + 1)) a1 = va & vb & vc & vd & ve Loop Dim v1, v2, v3, v4 As Integer Do Until Val(v1) + Val(v2) + Val(v3) + Val(v4) = 11 v1 = CInt(Int((9 * Rnd()) + 1)) v2 = CInt(Int((9 * Rnd()) + 1)) v3 = CInt(Int((9 * Rnd()) + 1)) v4 = CInt(Int((9 * Rnd()) + 1)) a2 = v1 & v2 & v3 & v4 Loop Dim v5, v6, v7, v8 As Integer Do Until Val(v5) + Val(v6) + Val(v7) + Val(v8) = 19 v5 = CInt(Int((9 * Rnd()) + 1)) v6 = CInt(Int((9 * Rnd()) + 1)) v7 = CInt(Int((9 * Rnd()) + 1)) v8 = CInt(Int((9 * Rnd()) + 1)) a3 = v5 & v6 & v7 & v8 Loop Return a1 & "-" & a2 & "-" & a3 & "-" & Date.Now.Year End Function
If someone has improvements, please feel free to post them. Thank you.Code:Dim s As String = txResult.Text Dim sp() As String Dim c1, c2, c3 As Long sp = s.Split("-") Label1.Text = sp(0) For Each nm1 As String In Label1.Text If Long.TryParse(nm1, Nothing) Then c1 += nm1 End If Next cnt1.Text = c1 Label2.Text = sp(1) For Each nm2 As String In Label2.Text If Long.TryParse(nm2, Nothing) Then c2 += nm2 End If Next cnt2.Text = c2 Label3.Text = sp(2) For Each nm3 As String In Label3.Text If Long.TryParse(nm3, Nothing) Then c3 += nm3 End If Next cnt3.Text = c3




Reply With Quote
