Link to home
Start Free TrialLog in
Avatar of IT-Freek
IT-Freek

asked on

How do i encrypt a string in Visual Basic using Data Protection API

I have a desktop application ,which is installed on 100 PCs here.
This application is developed in  VB 6 and SQL Server.
We store the Account number in our database
There is an additional requirement from the client to encrypt the account number using DP API, Data Protection API.
They do not want us to store the Account Number in plain text.
Can you pass on the code which will help me in encrypting the Account Number using DP API
Thanks,
Avatar of Dave Howe
Dave Howe
Flag of United Kingdom of Great Britain and Northern Ireland image

Here is some example code I found online - I don't have a copy of vb6 on this machine to test it though.
IF you have any issues, I will check it further when I get home :)
Private Declare Sub SecureZeroMemory Lib "kernel32.dll" (S As String, L As
Long)
 
Private Type Blob
  Size As Long
  Data As Long
End Type
 
Private Declare Sub LocalFree Lib "kernel32.dll" (ByVal M As Long)
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
 (ByVal Dest As Long, ByVal Source As Long, ByVal Count As Long)
Private Declare Function LocalAlloc Lib "kernel32.dll" _
 (ByVal Flags As Long, ByVal Sizes As Long) As Long
 
Private Declare Function CryptProtectData Lib "Crypt32.dll" _
 (pDataIn As Blob, ByVal szDataDescr As Long, ByVal Entrophy As Long, _
  ByVal pvReserved As Long, ByVal pPromptStruct As Long, _
  ByVal dwFlags As Long, pDataOut As Blob) _
 As Long
 
Private Declare Function CryptUnprotectData Lib "Crypt32.dll" _
 (pDataIn As Blob, ByVal szDataDescr As Long, ByVal Entrophy As Long, _
  ByVal pvReserved As Long, ByVal pPromptStruct As Long, _
  ByVal dwFlags As Long, pDataOut As Blob) _
 As Long
 
Sub EncryptString(Dest As Blob, Src As String)
  Dim DataIn As Blob, Desc As String
  DataIn.Data = StrPtr(Src)
  DataIn.Size = Len(Src) * 2
  Desc = "ISIS" & Chr$(0)
  MsgBox CryptProtectData(DataIn, StrPtr(Desc), 0, 0, 0, 0, Dest)
  MsgBox Err.LastDllError
End Sub
 
Sub E_Test()
  Dim Data As Blob
  Dim S As String
  EncryptString Data, "Hello"
  DecryptString S, Data
  MsgBox S
End Sub
 
Sub DecryptString(Dest As String, Src As Blob)
  Dim DataOut As Blob
  MsgBox CryptUnprotectData(Src, 0, 0, 0, 0, 0, DataOut)
  Dest = String$(DataOut.Size / 2, "*")
  CopyMemory StrPtr(Dest), DataOut.Data, DataOut.Size
  LocalFree DataOut.Data
End Sub 

Open in new window

Avatar of IT-Freek
IT-Freek

ASKER

Hi DaveHowe:
Thx for ur prompt reply.
Actually i forgot to mention 1 more thing.
The Encryption component should utilize DPAPI  for storing the Encryption Keys and Initialization Vectors (IV). This will ensures Keys and IVs are never stored in clear text on the machine.
The client needs triple D encryption.
Hey Guys,

Can someone help me out ??
ASKER CERTIFIED SOLUTION
Avatar of Dave Howe
Dave Howe
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apologize for the delay in closing this request