Inserting Encrypted String in Registry

Hello Everybody,

I am using CryptoAPI in VB to encrypt a string. The encryption is working very perfectly (Thanks to Davis Chapman's book).

Now the problem is to insert this string into Registry. I am using the API RegSetKeyValueEx to set the string. For eg. I want to set the String value EDate as the encrypted string. This EDate is in HKEY_LOCAL_MACHINE\SOFTWARE\Synx\ExpiryDate. The encrypted string is actually the expiry date for my activex control. In every initialize I read and decrypt this string to check the expiry date. If it has expired, my activex control locks itself and does not work untill the customer pays me for renewal. The API is not allowing me to write the Encrypted String. The .reg file is also not working. It writes simple string not the string with special characters. According to WinError.h the Error Code that returns is Access Denied (Error no 5).

Please tell me what I am doing is proper or is there a better way to do it?

Regards
Madhur
LVL 1
hmadhurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
It sounds like a permissions issue.Does it work if the user is logged in as administrator?
0
JonyvCommented:
Perhaps you should try to store the string as binary data (REG_BINARY) instead. This might work better if the encrypted string contains non-printable characters
0
emadatCommented:
First:
When you encrypt data; you might get a character with ASCII value of 0 in your encrypted string. Although the string will be fully accessible from VB; when you try to pass this string to an API call it will ignore all the characters starting from that character till the end. This is because API functions expects ASCIZ which is simply a string terminated by character #0.

Their is a simple and nice solution for this:
We convert each character of the string into its 2 character hexadecimal representation.
Example:
Imagine an encrypted string containing two characters:
S = Chr(0) & "A"
The hexadecimal representation for this string will be "0041" which is a fully qualified string.

Here are two functions to convert to and from HexaDecimal Representation:
'=====================================================
Function ToHex(strIn As String) As String
Dim I%, sTemp, sResult
    If Len(strIn) = 0 Then
        ToHex = ""
        Exit Function
    End If
    sResult = ""
    For I = 1 To Len(strIn)
        sTemp = Hex(Asc(Mid(strIn, I, 1)))
        If Len(sTemp) < 2 Then sTemp = "0" & sTemp
        sResult = sResult & sTemp
    Next I
    ToHex = sResult
End Function
'=====================================================
Function FromHex(strIn As String) As String
Dim I%, sTemp, sResult
    If Len(strIn) = 0 Then
        FromHex = ""
        Exit Function
    End If
    sResult = ""
    For I = 1 To Len(strIn) Step 2
        sTemp = "&H0" & Mid(strIn, I, 2)
        sResult = sResult & Chr(CInt(sTemp))
    Next I
    FromHex = sResult
End Function
'=====================================================

Second, "Access Denied" may be caused by access rights of the user accessing the registery.

Some parts of the registery are restricted to users with Administrator privilege only. Others are available for all users.
Check the following link for more information: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_key_security_and_access_rights.asp

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hmadhurAuthor Commented:
Hi emadat,
Thanks for the help. Even though the Access Denied is still there, the .reg merge file is working. I went through the MSDN articles. But they are a bit dangerous settings to play with because the user will have some previleges to access the registry and modify it. So better use the .reg merge file. :~)

Thanks and Regards
Madhur
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.