Wrox Programmer Forums
|
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Pro Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 28th, 2003, 02:13 PM
Authorized User
 
Join Date: Sep 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Greywacke
Default Len() Function Returning 0 on a 32 length string?

hi all, usually i dont get stuck while developing, but this time i am and i have no idea which way... why would Len(string) return 0 here even tho it is of length 32?

ok... this has been bugging me all day, let me paste the code i am using for a 128-bit encryption string generator, as well as the functions to encrypt and decrypt using one pass (1-bit encryption), as well as the master function which calls the individual functions and saves to db when encrypting, and deletes from db when decrypting.

Code:
'*******************************************************************
'**************** DECLARE: Generate 128-bit Key Function ***********
'*******************************************************************
Function GenKey()
    Dim i, cypher
    cypher = ""
    For i = 1 To 32 Step 1
        Randomize()
        cypher = cypher & Chr(Round(Rnd() * 254) + 1)
    Next
End Function

'*******************************************************************
'**************** DECLARE: Encryption Function *********************
'*******************************************************************
Function EnCrypt(strCryptThis, g_Key)
    Dim strChar, iKeyChar, iStringChar, i, n, iCryptChar, strEncrypted
    For i = 1 To Len(strCryptThis)
        If n > Len(g_Key) Then ' problem is here, Len(g_Key) keeps returning 0 even tho GenKey creates a 32 length key...
            n = 1
        Else
            n = n + 1
        End If
        iKeyChar = Asc(mid(g_Key,n,1))
        iStringChar = Asc(mid(strCryptThis,i,1))
        iCryptChar = iKeyChar Xor iStringChar
        strEncrypted =  strEncrypted & Chr(iCryptChar)
    Next
    EnCrypt = strEncrypted
End Function

'*******************************************************************
'**************** DECLARE: Decryption Function *********************
'*******************************************************************
Function DeCrypt(strEncrypted, g_Key)
    Dim strChar, iKeyChar, iStringChar, i, n, iDeCryptChar, strDecrypted
    For i = 1 To Len(strEncrypted)
        If n > Len(g_Key) Then ' I guess the problem would be here too
            n = 1
        Else
            n = n + 1
        End If
        iKeyChar = (Asc(mid(g_Key,n,1)))
        iStringChar = Asc(mid(strEncrypted,i,1))
        iDeCryptChar = iKeyChar Xor iStringChar
        strDecrypted =  strDecrypted & Chr(iDeCryptChar)
    Next
    DeCrypt = strDecrypted
End Function

'*******************************************************************
'**************** DECLARE: Cryptography Master Function ************
'*******************************************************************
Function Crypto(Input, Key)
    Dim i, n, cypher, gwConn, gwRS, crypt_id, encrypted, char, parsed, return
    return = "; "
    return = Split(return, "; ")
    Set gwConn = Application("gwConn")
    Set gwRS = Server.CreateObject("ADODB.Recordset")
    gwRS.Open "Cryptography",gwConn,adOpenDynamic,adLockOptimistic,adCmdTable
        If Key = "" Then    ' Encrypt
            cypher = GenKey()
            return(0) = EnCrypt(Input, cypher) & ""
            Do While InStr(return(0),"; ") > 0
                cypher = GenKey()
                return(0) = Encrypt(Input, cypher)
            Loop
            gwRS.AddNew
                gwRS("crypt_key") = cypher
            gwRS.Update
            crypt_id = gwRS("crypt_id")
            return(1) = crypt_id & ""
        Else                ' Decrypt
            gwRS.Find("crypt_id = " & Key)
            If Not gwRS.EOF Then
                gwRS.MoveFirst
                cypher = gwRS("crypt_key")
                return(0) = DeCrypt(Input, cypher)
                return(1) = (gwRS.Delete(adAffectCurrent))
            Else
                return(0) = Input & ""
                return(1) = "0"
            End If
        End If
        Crypto = join(return, "; ")
End Function
now, when callinng Crypto("test","") then the key is generated and saved to database successfully. After which Encrypt(Input, cypher) is called, where the n if statement is buggered due to Len(g_Key) returning 0... now why would this be anyone?

A swift response would be greatly appreciated and thanks in advance,
Pierre du Toit

Sincerely,
Pierre du Toit.
__________________
Sincerely,
Pierre "Greywacke" du Toit
[email protected]
don't worry about my 0 thankyou's either way, i would say thank you should you help, and i will gladly help others when this work rush is over.
 
Old October 28th, 2003, 04:00 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I had just a quick look, but it looks like there's a problem in GenKey(). It creates the string cypher, but it never returns it.
Code:
Function GenKey()
    Dim i, cypher
    cypher = ""
    For i = 1 To 32 Step 1
        Randomize()
        cypher = cypher & Chr(Round(Rnd() * 254) + 1)
    Next
    GenKey = cypher
End Function
Could that be causing the problem?

HtH,

Imar

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 28th, 2003, 05:20 PM
Authorized User
 
Join Date: Sep 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Greywacke
Default

oh thanks you are so sharp, or perhaps i am overworked o_O
wierd thing is, it writes the key to database successfully, and reads it successfully, just that when i pass it through g_Var when calling the Encrypt function it reads the length wrong... now i know why it might be...

thanks,
Pierre du Toit

Sincerely,
Pierre du Toit.
 
Old October 28th, 2003, 05:25 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, you may be overworked. Simple test: look in the mirror. Do you see square eyes?? ;)

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fix string length Ivanchan Excel VBA 2 July 26th, 2007 08:55 AM
Chap 3, "Length of String" richajos BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 February 25th, 2006 08:38 PM
How to return a string to it's exact original (len EDCH123 Java Databases 0 January 25th, 2006 04:06 PM
JavaScript: what is the function of "length" in th gilgalbiblewheel Javascript 2 March 9th, 2005 07:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.