Results 1 to 4 of 4

Thread: VB6 - Save settings into a file compressed and encrypted

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    VB6 - Save settings into a file compressed and encrypted

    If you want to save the application settings (or any other data) encrypted and/or compressed, this is how you do it:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function Compress Lib "zlib.dll" Alias "compress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long
    4. Private Declare Function Uncompress Lib "zlib.dll" Alias "uncompress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long
    5.  
    6. Private Function LoadSettings(Optional EncPassword As String = "") As PropertyBag
    7.     '
    8.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    9.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=434490[/url]
    10.     '
    11.  
    12.     Dim PB As PropertyBag, FLNum As Integer
    13.     Dim Data() As Byte, DataLength As Long
    14.     Dim EncCompSettings As Byte, OriginalSize As Long
    15.     Dim RndNum As Double
    16.    
    17.     Const Encrypted As Byte = 1
    18.     Const Compressed As Byte = 2
    19.    
    20.     ' On error just return an empty property bag
    21.     On Error GoTo ReturnEmpty
    22.    
    23.     Set PB = New PropertyBag
    24.    
    25.     FLNum = FreeFile
    26.     Open App.Path & "\Settings.dat" For Binary Access Read As FLNum
    27.         Get FLNum, , DataLength ' Get data length (size of buffer)
    28.        
    29.         If DataLength > 0 Then
    30.             ' Get whether it was compressed and/or encrypted
    31.             Get FLNum, , EncCompSettings
    32.            
    33.             ' if encrypted, read the random number
    34.             If (EncCompSettings And Encrypted) = Encrypted Then Get FLNum, , RndNum
    35.            
    36.             ReDim Data(DataLength)
    37.             Get FLNum, , Data ' read the data with the settings
    38.            
    39.             ' If data was encrypted, then decrypt it
    40.             If (EncCompSettings And Encrypted) = Encrypted Then
    41.                 Data = StrConv(RndCrypt(StrConv(Data, vbUnicode), EncPassword & RndNum), vbFromUnicode)
    42.             End If
    43.            
    44.             ' If data was compressed, then decompress it
    45.             If (EncCompSettings And Compressed) = Compressed Then
    46.                 Get FLNum, , OriginalSize
    47.                
    48.                 Dim OData() As Byte ' temporary buffer for decompression
    49.                 ReDim OData(OriginalSize)
    50.                 Uncompress OData(0), OriginalSize, Data(0), UBound(Data) + 1 ' decompress data
    51.                
    52.                 Data = OData
    53.                 Erase OData
    54.             End If
    55.            
    56.             ' Put the data in the property bag
    57.             PB.Contents = Data
    58.         End If
    59.     Close FLNum
    60.    
    61. ReturnEmpty:
    62.     Set LoadSettings = PB ' return the property bag
    63. End Function
    64.  
    65. Private Function SaveSettings(SavePB As PropertyBag, Optional CompressData As Boolean = False, Optional EncPassword As String) As Boolean
    66.     '
    67.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    68.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=434490[/url]
    69.     '
    70.  
    71.     Dim Data() As Byte, DataLength As Long
    72.     Dim FLNum As Integer, RndNum As Double
    73.     Dim EncCompSettings As Byte, OriginalSize As Long
    74.    
    75.     Const Encrypted As Byte = 1
    76.     Const Compressed As Byte = 2
    77.    
    78.     ' On error exit and return False
    79.     On Error GoTo Err_Exit
    80.    
    81.     Data = SavePB.Contents ' get the contents of the property bag
    82.    
    83.     OriginalSize = UBound(Data) + 1 ' get size of buffer (un-altered/original)
    84.    
    85.     If CompressData Then
    86.         EncCompSettings = Compressed ' save the setting that we compressed the data
    87.        
    88.         Dim CData() As Byte ' temporary buffer to compress data
    89.         DataLength = (UBound(Data) + 1) * 1.01 + 12
    90.         ReDim CData(DataLength - 1)
    91.        
    92.         Compress CData(0), DataLength, Data(0), UBound(Data) + 1 ' compress the data
    93.        
    94.         ReDim Preserve CData(DataLength - 1) ' resize buffer to the compressed length
    95.         Erase Data
    96.         Data = CData ' assign compressed data to main buffer
    97.     End If
    98.    
    99.     If Len(EncPassword) > 0 Then
    100.         EncCompSettings = EncCompSettings Or Encrypted ' save the setting that we encrypted the data
    101.        
    102.         Randomize
    103.         ' get a random number
    104.         RndNum = 2 ^ 15 * Rnd + 2 ^ 20 * Rnd + 2 ^ 25 * Rnd + 2 ^ 30 * Rnd
    105.        
    106.         ' encrypt the data using our password and the random number
    107.         Data = StrConv(RndCrypt(StrConv(Data, vbUnicode), EncPassword & RndNum), vbFromUnicode)
    108.     End If
    109.    
    110.     ' delete the file if it already exists
    111.     ' (in case the size of the current file is less than before)
    112.     If Len(Dir(App.Path & "\Settings.dat")) > 0 Then
    113.         On Error Resume Next
    114.         SetAttr App.Path & "\Settings.dat", vbNormal
    115.         Kill App.Path & "\Settings.dat"
    116.         On Error GoTo 0
    117.     End If
    118.    
    119.     ' Create file, and save all the settings
    120.     FLNum = FreeFile
    121.     Open App.Path & "\Settings.dat" For Binary Access Write As FLNum
    122.         DataLength = UBound(Data)
    123.        
    124.         Put FLNum, , DataLength
    125.         Put FLNum, , EncCompSettings
    126.        
    127.         If (EncCompSettings And Encrypted) = Encrypted Then Put FLNum, , RndNum
    128.        
    129.         Put FLNum, , Data
    130.        
    131.         If (EncCompSettings And Compressed) = Compressed Then Put FLNum, , OriginalSize
    132.     Close FLNum
    133.    
    134.     SaveSettings = True
    135.     Exit Sub
    136.    
    137. Err_Exit:
    138.     SaveSettings = False
    139. End Function
    140.  
    141. Public Function RndCrypt(ByVal Str As String, ByVal Password As String) As String
    142.     '
    143.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    144.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=231798[/url]
    145.     '
    146.     Dim SK As Long, K As Long
    147.    
    148.     ' init randomizer for password
    149.     Rnd -1
    150.     Randomize Len(Password)
    151.     ' (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd)) -> makes sure that a
    152.     ' password like "pass12" does NOT give the same result as the password "sspa12" or "12pass"
    153.     ' or "1pass2" etc. (or any combination of the same letters)
    154.    
    155.     For K = 1 To Len(Password)
    156.         SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))
    157.     Next K
    158.    
    159.     ' init randomizer for encryption/decryption
    160.     Rnd -1
    161.     Randomize SK
    162.    
    163.     ' encrypt/decrypt every character using the randomizer
    164.     For K = 1 To Len(Str)
    165.         Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))
    166.     Next K
    167.    
    168.     RndCrypt = Str
    169. End Function
    How to use the functions (save and load settings/data):
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim LoadPB As PropertyBag
    3.    
    4.     ' load the settings
    5.     ' Use any password you want, as long as it's the same when you save data
    6.     Set LoadPB = LoadSettings("password=-0987654321")
    7.    
    8.     ' read the values from the property bag
    9.     Me.Top = LoadPB.ReadProperty("Top", Me.Top)
    10.     Me.Left = LoadPB.ReadProperty("Left", Me.Left)
    11.     Me.Width = LoadPB.ReadProperty("Width", Me.Width)
    12.     Me.Height = LoadPB.ReadProperty("Height", Me.Height)
    13. End Sub
    14.  
    15. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    16.     Dim SavePB As New PropertyBag
    17.    
    18.     ' assign the values you need to save in the property bag
    19.     SavePB.WriteProperty "Top", Me.Top
    20.     SavePB.WriteProperty "Left", Me.Left
    21.     SavePB.WriteProperty "Width", Me.Width
    22.     SavePB.WriteProperty "Height", Me.Height
    23.    
    24.     ' save the settings
    25.     ' Password must be the same as when you load the data
    26.     SaveSettings SavePB, True, "password=-0987654321"
    27. End Sub
    As you may already noticed, this code uses ZLib.DLL to compress data, this is the website for ZLIB:
    http://www.zlib.net/
    And to get the actual DLL, this is the direct link to the ZIP where the DLL is: ZLib.dll
    Upzip the file, and copy and paste the DLL to your windows/system32 directory.
    Last edited by CVMichael; Oct 24th, 2006 at 09:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width