Results 1 to 2 of 2

Thread: Please Help! Encryption problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Question Please Help! Encryption problem

    Hey All,

    I found the following encrypt/decrypt code below somewhere.

    Code:
    Public Function EncryptString(ByVal InString As String, ByVal EncryptKey As String) As String
     Dim TempKey, OutString As String
     Dim OldChar, NewChar, CryptChar As Long
     Dim i As Integer
     
     ' Initilize i and make sure the EncryptKey is long enough
     i = 0
     Do
      TempKey = TempKey + EncryptKey
     Loop While Len(TempKey) < Len(InString)
     
     ' Loop through the string to encrypt each character.
     Do
      i = i + 1
      OldChar = Asc(Mid(InString, i, 1))
      CryptChar = Asc(Mid(TempKey, i, 1))
      ' If it's an even character, add the ASCII value of the
      ' appropriate character in the Key, otherwise, subract it.
      ' Also, make sure the value is between 0 and 127.
      Select Case i Mod 2
       Case 0      'Even Character
        NewChar = OldChar + CryptChar
        If NewChar > 127 Then NewChar = NewChar - 127
       Case Else   'Odd Character
        NewChar = OldChar - CryptChar
        If NewChar < 0 Then NewChar = NewChar + 127
      End Select
      ' If the value is less than 35, add 40 to it (to make sure
      ' it's in the display range) and put it in an escape
      ' sequence (using ! [ASCII Value 33] as the escape char)
      If NewChar < 35 Then
       OutString = OutString + "!" + Chr(NewChar + 40)
      Else
       OutString = OutString + Chr(NewChar)
      End If
     Loop Until i = Len(InString)
     
     EncryptString = OutString
    
    End Function
    
    Public Function DecryptString(ByVal InString As String, ByVal EncryptKey As String) As String
     Dim TempKey, OutString As String
     Dim OldChar, NewChar, CryptChar As Long
     Dim i, c As Integer
     
     ' Initialize c and i (loop variables)
     c = 0       ' c is used for InString
     i = 0       ' i is used for EncryptKey
     ' Make sure the EncryptKey is long enough
     Do
      TempKey = TempKey + EncryptKey
     Loop While Len(TempKey) < Len(InString)
     
     Do
      ' In the decrypt function, two integers are need keeping
      ' track of location (becuase the escape sequence it two
      ' chars long, but only has one placeholder in the key)
      i = i + 1
      c = c + 1
      OldChar = Asc(Mid(InString, c, 1))
      ' If this is an escape sequence, get the next character and
      ' subtract 40 from it's value.
      If OldChar = 33 Then
       c = c + 1
       OldChar = Asc(Mid(InString, c, 1))
       OldChar = OldChar - 40
      End If
      CryptChar = Asc(Mid(TempKey, i, 1))
      ' If it's an even character, subract the appropriate key
      ' value... also, if it's out of range, bring it back in.
      Select Case i Mod 2
       Case 0      'Even Character
        NewChar = OldChar - CryptChar
        If NewChar < 0 Then NewChar = NewChar + 127
       Case Else   'Odd Character
        NewChar = OldChar + CryptChar
        If NewChar > 127 Then NewChar = NewChar - 127
      End Select
      OutString = OutString + Chr(NewChar)
     Loop Until c = Len(InString)
     
     DecryptString = OutString
    
    End Function
    Now, this code works very well, but I have a problem...

    I need to encrypt a string, and place it on a webpage for an
    unlock code. If the encrypted string (using the code above)
    has any brackets{} or arrow<> symbols in it, then the the HTML
    code thinks it is part of the HTML structural code for the web page
    and therefore, will not show it.

    Would someone please show me how to fix this?

    What I would really like is an encrytp/decrypt code that only uses
    the following characters...

    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdefghijklmnopqrstuvwxyz
    0123456789

    Also, it needs to use an encrypt key.

    Thanks in advance for your help,
    Ron

  2. #2
    Lively Member ranma_at's Avatar
    Join Date
    Oct 2002
    Location
    @ Home
    Posts
    72
    When i use encryption, and i did it alot i always used a HEX output to store the encryption. So you dont get those weird outputs...
    like for this :

    http://www.planet-source-code.com/vb...41006&lngWId=1

    here the HEX code:
    VB Code:
    1. Option Explicit  
    2.  
    3. Private m_InitHex as Boolean  
    4. Private m_ByteToHex(0 To 255, 0 To 1) as Byte  
    5. Private m_HexToByte(48 To 70, 48 To 70) as Byte  
    6.  
    7. Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination as Any, ByVal Length as Long, ByVal Fill as Byte)  
    8.  
    9. Function HexToStr(HexText as String, Optional ByVal Separators as Long = 1) as String  
    10.  
    11.   Dim a as Long  
    12.   Dim Pos as Long  
    13.   Dim PosAdd as Long  
    14.   Dim ByteSize as Long  
    15.   Dim HexByte() as Byte  
    16.   Dim ByteArray() as Byte  
    17.  
    18.   'Initialize the hex routine
    19.   If (Not m_InitHex) Then Call InitHex  
    20.  
    21.   'The destination string is half
    22.   'the size of the source string
    23.   'when the separators are removed
    24.   If (Len(HexText) = 2) Then  
    25.     ByteSize = 1  
    26.   Else  
    27.     ByteSize = ((Len(HexText) + 1) \ (2 + Separators))  
    28.   End If  
    29.   ReDim ByteArray(0 To ByteSize - 1)  
    30.  
    31.   'Convert every HEX code to the
    32.   'equivalent ASCII character
    33.   PosAdd = 2 + Separators  
    34.   HexByte() = StrConv(HexText, vbFromUnicode)  
    35.   For a = 0 To (ByteSize - 1)  
    36.     ByteArray(a) = m_HexToByte(HexByte(Pos), HexByte(Pos + 1))  
    37.     Pos = Pos + PosAdd  
    38.   Next  
    39.  
    40.   'Now finally convert the byte
    41.   'array to the return string
    42.   HexToStr = StrConv(ByteArray, vbUnicode)  
    43.  
    44. End Function  
    45. Private Sub InitHex()  
    46.  
    47.   Dim a as Long  
    48.   Dim b as Long  
    49.   Dim HexBytes() as Byte  
    50.   Dim HexString as String  
    51.  
    52.   'The routine is initialized
    53.   m_InitHex = True  
    54.  
    55.   'Create a string with all hex values
    56.   HexString = String$(512, "0")  
    57.   For a = 1 To 255  
    58.     Mid$(HexString, 1 + a * 2 + -(a < 16)) = Hex(a)  
    59.   Next  
    60.   HexBytes = StrConv(HexString, vbFromUnicode)  
    61.  
    62.   'Create the Str->Hex array
    63.   For a = 0 To 255  
    64.     m_ByteToHex(a, 0) = HexBytes(a * 2)  
    65.     m_ByteToHex(a, 1) = HexBytes(a * 2 + 1)  
    66.   Next  
    67.  
    68.   'Create the Str->Hex array
    69.   For a = 0 To 255  
    70.     m_HexToByte(m_ByteToHex(a, 0), m_ByteToHex(a, 1)) = a  
    71.   Next  
    72.  
    73. End Sub  
    74. Function StrToHex(text as String, Optional Separator as String = " ") as String  
    75.  
    76.   Dim a as Long  
    77.   Dim Pos as Long  
    78.   Dim Char as Byte  
    79.   Dim PosAdd as Long  
    80.   Dim ByteSize as Long  
    81.   Dim ByteArray() as Byte  
    82.   Dim ByteReturn() as Byte  
    83.   Dim SeparatorLen as Long  
    84.   Dim SeparatorChar as Byte  
    85.  
    86.   'Initialize the hex routine
    87.   If (Not m_InitHex) Then Call InitHex  
    88.  
    89.   'Initialize variables
    90.   SeparatorLen = Len(Separator)  
    91.  
    92.   'Create the destination bytearray, this
    93.   'will be converted to a string later
    94.   ByteSize = (Len(text) * 2 + (Len(text) - 1) * SeparatorLen)  
    95.   ReDim ByteReturn(ByteSize - 1)  
    96.   Call FillMemory(ByteReturn(0), ByteSize, Asc(Separator))  
    97.  
    98.   'We convert the source string into a
    99.   'byte array to speed this up a tad
    100.   ByteArray() = StrConv(text, vbFromUnicode)  
    101.  
    102.   'Now convert every character to
    103.   'it's equivalent HEX code
    104.   PosAdd = 2 + SeparatorLen  
    105.   For a = 0 To (Len(text) - 1)  
    106.     ByteReturn(Pos) = m_ByteToHex(ByteArray(a), 0)  
    107.     ByteReturn(Pos + 1) = m_ByteToHex(ByteArray(a), 1)  
    108.     Pos = Pos + PosAdd  
    109.   Next  
    110.  
    111.   'Convert the bytearray to a string
    112.   StrToHex = StrConv(ByteReturn(), vbUnicode)  
    113.  
    114. End Function
    Things are not happening to you, things are happening because of you!

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