generating 25 alphanumeric code with conditions

theonebit

Member
Joined
Sep 25, 2011
Messages
10
Programming Experience
1-3
Hi,
I have a problem. I have been programming an activation type of security protection for my
software but i ran into problems when i try to generate an alphanumeric key with
conditions. i cant seem to be able to make the keys to be valid with the condition.
Basically i can get the 25 keys but all of them seem to be returning 1 (ones) instead of 0
(zero which what i want. below is my sample code may be someone can tell me what i am doing
wrong, by the way i did google, woogle,bing, bang, yahoo, yadoo etc but no go.
code: generate the keys
VB.NET:
Public Function GenerateCode() As Object
        Dim IntRnd As Object
        Dim IntStep As Object
        Dim StrName As Object
        Dim IntNameLength As Object
        Dim IntLength As Object
        Dim StrInputString As Object

      'set the char needed
  StrInputString = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   
     IntLength = Len(StrInputString)
     
'set the length
   IntNameLength = 25
      
  Randomize()
     
   StrName = ""
    
    For IntStep = 1 To IntNameLength
            IntRnd = Int((IntLength * Rnd()) + 1)
            StrName = StrName & Mid(StrInputString, IntRnd, 1)
        Next
        Return StrName
    End Function
code: here where i put the condition and here where it giving me hell
VB.NET:
Public Function GetPID()
        Dim pid As String = String.Empty
        Dim digitalPid As Byte()
        Dim sequenceNumber As Integer = 0
        Dim upgradeFlag As Boolean = False
        Dim isOem As Boolean = False
        Dim oemID As String = Nothing
        Dim builder As New StringBuilder(&H18, &H18)
        digitalPid = New Byte(&H100 - 1) {}
        Buffer.BlockCopy(BitConverter.GetBytes(digitalPid.Length), 0, digitalPid, 0, 4)
        Dim num As Integer
 dim key as string 
redo1: key = generatecode()
        num = setcon(key, con1, con2)
if num = 0 then
return key 
else
goto redo1:
end if
      End Function
the damn thing keeps returning 1 instead of 0. I do know i can simply change to 1 and be
off the issue. but if is that easy to generate 1 the isnt easy just to break it. oh yeah
the whole thing hangs when i generate the code???
if i loop it, its able to get a 0 but i cant take the key thats in the loop??
any idea or comment is much appreciated, i do not need the coding just the logic should do,
i should be able to find my way around...
 
heres a little keygen i like to use. counter = the length of the key

VB.NET:
        Dim counter As Double = 0
        Dim dice As Double
        Dim rollnum As Short
        Dim Generator As System.Random = New System.Random()
        Dim genKey As String = ""
        TextBox1.Text = ""
        Do Until counter = 32
            dice = Generator.Next(3)
            Select Case dice
                Case 0
                    '0-9 = chr(48) to chr(57)
                    rollnum = 48 + Generator.Next(10)
                    genKey = genKey & Chr(rollnum)
                Case 1
                    'A-Z = chr(65) to chr(90)
                    rollnum = 65 + Generator.Next(26)
                    genKey = genKey & Chr(rollnum)
                Case 2
                    'a-z = chr(97) to chr(122)
                    rollnum = 97 + Generator.Next(26)
                    genKey = genKey & Chr(rollnum)
            End Select
            counter += 1
        Loop
        counter = 0
        TextBox1.Text = genKey
        genKey = ""
 
hi,

thank you. i will keep the snipphet for future use. but it does not serve my purpose currently
if you could, please view my code and see if you help me out... with my mistakes
 
Is setcon a method/sub routine? Where is the code for it?

Also, you could potentially get stuck in a loop which never ends if the required condition is never met.

Why don't you build a key in sections so that it will always meet your requirments (just assuming what you want here).
 
Is setcon a method/sub routine? Where is the code for it?

Also, you could potentially get stuck in a loop which never ends if the required condition is never met.

Why don't you build a key in sections so that it will always meet your requirments (just assuming what you want here).

its a sub. you are right i am stuck in a infinite loop. that sub routine cannot be shown (conf, pls dont ask more) .
but i do realize something, it is the way i generate the keys, its random,
i randomly generate key than make it check then take the key.
I am currently rethinking the whole process.
is it possible to built a code in the generated key so that the only the subroutine knows to read it and return the value i want for it?
if it is possible could someone give me head up on how to implement such?
 
Well it could be done I think, but it would be flawed as if anybody ever knew what the extra bit of code in the key represented, they could just generate another one using that method.
Depends how secure the keys must be, and how complicated you want to make it I suppose.

Do you know 100% that your setcon sub works ok? If you try sending it a bad and a good string, do you get a 1 and 0 returned as expected? Create a list box and stick your key generated outputs (loop it 10 times etc) into it and just have a quick look to make sure they are actually random and not the same stuff everytime.
 
setcon works 100% no doubt
but will try the listbox thing
it should be random but i will verify
what i dont understand is that when i do a do until num = 0 loop it works but
i do not know which key. that is the issue
below is my code
VB.NET:
do until num = 0
pk = string.empty
pk = getrandomkey()
num = setcon(pk, con1, con2)
loop

now if i return num, i will get a 0 but how in the blue hell i get the key that return the 0
is beyond me currently.
the if and end if method i will get a infinite loop.
 
Last edited:
Setcon just returns 0 or 1 i assume? so num will never = 10?

try..

VB.NET:
dim i as integer
dim pk as string

for i = 0 to 10

pk = getrandomkey
listbox1.items.add("Key " & pk  & " Condition Check returned " & setcon(pk, con1, con2))

next
 
Without seeing setcon, couldnt tell you why the condition is not being met.

generatecode() works perfect, its the way setcon checks the key that looks like the problem as
LeonR suggested already.

Good Luck.
 
my bad!
typo
it suppose to be 0
i did not copy and paste but type the code.
so 1 got added, thank you for notifying
i will edit it
and try the given code
and display the results
 
Back
Top