We help IT Professionals succeed at work.

Encrypt / decrypt querystring and cookie variables?

Jay_SE asked
Last Modified: 2010-04-21
I am looking for a vb-script function that encrypts and decrypts strings in an easy and efficient way (encryption on page 1 and decryption on page 2), but at the same time it should not make the decoded string too long. I have tried several vb-script encoders/encrypters, but the encoded text is either to similar to the real text or the encoded string becomes very, very long.

Is there someone out there who can provide me with some good code-samples in vb-script that fit the description above?

I want to encrypt 3 things:

1) user id and password that is stored in a client cookie
2) the hidden field values in a form
3) the data in the querystring that is viewed in the client browser.
Especially I want to encrypt the data shown after the questionmark when sending data between two asp-pages.

An example of how it looks now is: "https://www.experts-exchange.com/file.asp?category=1,2,4®istered=1..."
I want this to look like this instead: "https://www.experts-exchange.com/file.asp?7718d2cd0d812337dcd..."
or possibly like this "https://www.experts-exchange.com/file.asp?category=18d2cd0d8®istered=18d2cd0d8..."
..or similar.
Watch Question

I was unable to view your examples, but here's a simple set of encryption/decryption functions that use the SessionID as the key.  Usage is:

strSample1 = EnCrypt(rsData("MyDate"))
strSample1 = Decrypt(strSample1)


  Function EnCrypt(strCryptThis)
    Dim strChar, iKeyChar, iStringChar, i,g_Key, varSessionID
    varSessionID = Session.SessionID
    g_Key = mid(varSessionID,1,Len(strCryptThis))
    for i = 1 to Len(strCryptThis)
     iKeyChar = Asc(mid(g_Key,i,1))
     iStringChar = Asc(mid(strCryptThis,i,1))
     iCryptChar = iKeyChar Xor iStringChar
     iCryptCharHex = Hex(iCryptChar)
     iCryptCharHexStr = cstr(iCryptCharHex)
     if len(iCryptCharHexStr)=1 then iCryptCharHexStr = "0" & iCryptCharHexStr
     strEncrypted = strEncrypted & iCryptCharHexStr
    EnCrypt = strEncrypted
   End Function
   Function DeCrypt(strEncrypted)
    Dim strChar, iKeyChar, iStringChar, i,g_Key, varSessionID
    varSessionID = Session.SessionID
    g_Key = mid(varSessionID,1,Len(strEncrypted)/2)
    for i = 1 to Len(strEncrypted) /2
     iKeyChar = (Asc(mid(g_Key,i,1)))
     iStringChar2 = mid(strEncrypted,(i*2)-1,2)
     iStringChar = CLng("&H" & iStringChar2)
     iDeCryptChar = iKeyChar Xor iStringChar
     strDecrypted = strDecrypted & chr(iDeCryptChar)
    DeCrypt = strDecrypted
   End Function


I will try this solution, but since it is not an encryption-mechanism where I can use any key I am not sure if it fits my perpouse.

Is it possible to use an external key instead of the session id?
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)


Then this is it. Thank you for this help!

Can anybody help? I am using this script successfully until I try to encrypt a string longer than 9 characters, at which point it falls over with the error message below.

How might I solve this?

Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument: 'Asc'
/crypt.asp, line 9 

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions