Results 1 to 5 of 5

Thread: Encrypt INI FILE

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Santo Domingo,D.N., Dom. Rep.
    Posts
    707

    Talking Encrypt INI FILE

    I need a Help. I know nothing about encrypting. Is there is a way to encrypt an INI file using VB?

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belfast, N. Ireland
    Posts
    167
    Here's a class which will encrypt a text string,
    You could use it to encrypt each line of your .ini file:

    VB Code:
    1. ' // clsCipher.cls
    2.  
    3. Option Explicit
    4.  
    5. Private mstrKey As String
    6. Private mstrText As String
    7.  
    8. '~~~.KeyString
    9. 'A string (key) used in encryption and decryption
    10. Public Property Let KeyString(strKey As String)
    11.     mstrKey = strKey
    12.     Initialize
    13. End Property
    14.  
    15. '~~~.Text
    16. 'Write text to be encrypted or decrypted
    17. Public Property Let Text(strText As String)
    18.     mstrText = strText
    19. End Property
    20.  
    21. 'Read text that was encrypted or decrypted
    22. Public Property Get Text() As String
    23.     Text = mstrText
    24. End Property
    25.  
    26. '~~~.DoXor
    27. 'Exclusive-or method to encrypt or decrypt
    28. Public Sub DoXor()
    29.     Dim lngC As Long
    30.     Dim intB As Long
    31.     Dim lngN As Long
    32.     For lngN = 1 To Len(mstrText)
    33.         lngC = Asc(Mid(mstrText, lngN, 1))
    34.         intB = Int(Rnd * 256)
    35.         Mid(mstrText, lngN, 1) = Chr(lngC Xor intB)
    36.     Next lngN
    37. End Sub
    38.  
    39. '~~~.Stretch
    40. 'Convert any string to a printable, displayable string
    41. Public Sub Stretch()
    42.     Dim lngC As Long
    43.     Dim lngN As Long
    44.     Dim lngJ As Long
    45.     Dim lngK As Long
    46.     Dim lngA As Long
    47.     Dim strB As String
    48.     lngA = Len(mstrText)
    49.     strB = Space(lngA + (lngA + 2) \ 3)
    50.     For lngN = 1 To lngA
    51.         lngC = Asc(Mid(mstrText, lngN, 1))
    52.         lngJ = lngJ + 1
    53.         Mid(strB, lngJ, 1) = Chr((lngC And 63) + 59)
    54.         Select Case lngN Mod 3
    55.         Case 1
    56.             lngK = lngK Or ((lngC \ 64) * 16)
    57.         Case 2
    58.             lngK = lngK Or ((lngC \ 64) * 4)
    59.         Case 0
    60.             lngK = lngK Or (lngC \ 64)
    61.             lngJ = lngJ + 1
    62.             Mid(strB, lngJ, 1) = Chr(lngK + 59)
    63.             lngK = 0
    64.         End Select
    65.     Next lngN
    66.     If lngA Mod 3 Then
    67.         lngJ = lngJ + 1
    68.         Mid(strB, lngJ, 1) = Chr(lngK + 59)
    69.     End If
    70.     mstrText = strB
    71. End Sub
    72.  
    73. '~~~.Shrink
    74. 'Inverse of the Stretch method;
    75. 'result can contain any of the 256-byte values
    76. Public Sub Shrink()
    77. On Error Resume Next
    78.     Dim lngC As Long
    79.     Dim lngD As Long
    80.     Dim lngE As Long
    81.     Dim lngA As Long
    82.     Dim lngB As Long
    83.     Dim lngN As Long
    84.     Dim lngJ As Long
    85.     Dim lngK As Long
    86.     Dim strB As String
    87.     lngA = Len(mstrText)
    88.     lngB = lngA - 1 - (lngA - 1) \ 4
    89.     strB = Space(lngB)
    90.     For lngN = 1 To lngB
    91.         lngJ = lngJ + 1
    92.         lngC = Asc(Mid(mstrText, lngJ, 1)) - 59
    93.         Select Case lngN Mod 3
    94.         Case 1
    95.             lngK = lngK + 4
    96.             If lngK > lngA Then lngK = lngA
    97.             lngE = Asc(Mid(mstrText, lngK, 1)) - 59
    98.             lngD = ((lngE \ 16) And 3) * 64
    99.         Case 2
    100.             lngD = ((lngE \ 4) And 3) * 64
    101.         Case 0
    102.             lngD = (lngE And 3) * 64
    103.             lngJ = lngJ + 1
    104.         End Select
    105.         Mid(strB, lngN, 1) = Chr(lngC Or lngD)
    106.     Next lngN
    107.     mstrText = strB
    108. End Sub
    109.  
    110. 'Initializes random numbers using the key string
    111. Private Sub Initialize()
    112.     Dim lngN As Long
    113.     Randomize Rnd(-1)
    114.     For lngN = 1 To Len(mstrKey)
    115.         Randomize Rnd(-Rnd * Asc(Mid(mstrKey, lngN, 1)))
    116.     Next lngN
    117. End Sub

    Encrypt like this:

    VB Code:
    1. Dim chrx As New clsCipher
    2.  
    3.     chrx.KeyString = X 'insert a unique KEY here
    4.     chrx.Text = X 'insert your text here
    5.     chrx.DoXor
    6.     chrx.Stretch

    and Decrypt like this:

    VB Code:
    1. Dim chrx As New clsCipher
    2.  
    3.     chrx.KeyString = X
    4.     chrx.Text = X
    5.     chrx.Shrink
    6.     chrx.DoXor

  3. #3
    New Member
    Join Date
    Oct 2010
    Location
    Nassau,Bahamas
    Posts
    6

    Re: Encrypt INI FILE

    Hi,
    I am fairly new to VB. I tried this code and it is giving me the error message "Expected end of statement" Is this because I am using an older version of VB?

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Encrypt INI FILE

    That thread is 8 years old. Next time create your own thread, and put the link to that thread in your message.

    Did you copy & paste that code in a class ?

  5. #5
    New Member
    Join Date
    Oct 2010
    Location
    Nassau,Bahamas
    Posts
    6

    Re: Encrypt INI FILE

    Hi,
    I apologize. I didn't think that my replying to a post would make me seem selfish in my quest for answers. I also didn't know that this action is considered a request for private help. Now that I know, I will create a new thread.
    P.S I did copy and paste this code in an application that uses visual basic scripting

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