Results 1 to 8 of 8

Thread: [RESOLVED] Fastest Encryption

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    Resolved [RESOLVED] Fastest Encryption

    Hi, I would like to know what is the fastest encryption available, but I don't want it to be intensively secure. I just want something illegible to the human eye. For example, letter key shifting doesn't work because it's easy to visualize what the words were before hand. I just need something that you can't just crack by staring at it for 10 seconds, but it can't be resource intensive or slow. Any ideas?

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Fastest Encryption

    Just call encode or decode

    VB Code:
    1. Public Function Encode(Data As String) As String
    2.    
    3.     Randomize
    4.    
    5.     Dim Key() As Long
    6.     ReDim Key(Len(Data))
    7.    
    8.     Dim i As Long
    9.     Dim LenData As Long
    10.     Dim Coded As String
    11.     Coded = ""
    12.    
    13.     LenData = Len(Data)
    14.     For i = 1 To LenData
    15.         Key(i) = (Rnd() * 50 + 1) + 20 'Define keys for each character
    16.     Next
    17.    
    18.     For i = 1 To LenData
    19.         'Adding the key to each character's ascii
    20.         If Asc(Mid$(Data, i, 1)) + Key(i) > 255 Then
    21.             'If the new ascii value exceeds 255(Highest char ascii), then count upwards from 0
    22.             Coded = Coded & Chr$(Key(i)) & Chr$(Asc(Mid$(Data, i, 1)) + Key(i) - 255)
    23.         Else
    24.             Coded = Coded & Chr$(Key(i)) & Chr$(Asc(Mid$(Data, i, 1)) + Key(i))
    25.         End If
    26.     Next
    27.     'Return encoded value
    28.     'Debug.Print Len(Coded)
    29.     Encode = Coded
    30.    
    31. End Function
    32.  
    33. Public Function Decode(Data As String) As String
    34.    
    35.     Dim Key() As Long
    36.     ReDim Key(Len(Data))
    37.    
    38.     Dim i As Long
    39.     Dim Decoded As String
    40.     Dim CodedString As String
    41.     Dim LenData As Long
    42.     Dim LenCodedString As Long
    43.     Dim NextChr As Long
    44.    
    45.     Decoded = ""
    46.     CodedString = ""
    47.    
    48.     'Seperate the key from the actual code
    49.     LenData = Len(Data)
    50.     For i = 1 To LenData
    51.         If (i / 2) = Int(i / 2) Then
    52.             CodedString = CodedString & Mid$(Data, i, 1)
    53.         Else
    54.             Key(((i - 1) / 2) + 1) = Asc(Mid$(Data, i, 1))
    55.         End If
    56.     Next
    57.    
    58.     'Minus the key from each character
    59.     LenCodedString = Len(CodedString)
    60.     For i = 1 To LenCodedString
    61.     NextChr = Asc(Mid$(CodedString, i, 1)) - Key(i)
    62.     'If the new ascii is below 0, then count backwards from 255
    63.     If NextChr <= 0 Then
    64.     NextChr = NextChr + 255
    65. End If
    66. 'Add to decoded string
    67. Decoded = Decoded + Chr$(NextChr)
    68. Next
    69. 'Return Decoded value
    70. Decode = Decoded
    71.  
    72. End Function
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: Fastest Encryption

    I modified the code in my original thread ( VB - 31 Bit Encryption function ) for speed

    It should be fast enough, and it's definitelly not readable because it's using random numbers XORed with original string to encrypt.
    VB Code:
    1. Public Function RndCrypt(ByRef Str As String, ByVal Password As String) As String
    2.     '
    3.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    4.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=231798[/url]
    5.     '
    6.     Dim SK As Long, K As Long, Data() As Byte
    7.     Data = StrConv(Str, vbFromUnicode)
    8.    
    9.     ' init randomizer for password
    10.     Rnd -1
    11.     Randomize Len(Password)
    12.     ' (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd)) -> makes sure that a
    13.     ' password like "pass12" does NOT give the same result as the password "sspa12" or "12pass"
    14.     ' or "1pass2" etc. (or any combination of the same letters)
    15.    
    16.     For K = 1 To Len(Password)
    17.         SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))
    18.     Next K
    19.    
    20.     ' init randomizer for encryption/decryption
    21.     Rnd -1
    22.     Randomize SK
    23.    
    24.     ' encrypt/decrypt every character using the randomizer
    25.     For K = 0 To UBound(Data)
    26.         Data(K) = Fix(256 * Rnd) Xor Data(K)
    27.     Next K
    28.    
    29.     RndCrypt = StrConv(Data, vbUnicode)
    30. End Function

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Fastest Encryption

    Remove StrConv and any string processing besides copying byte array to a string and vice versa and you get much faster result (especially with a compiled program).

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

    Re: Fastest Encryption

    Quote Originally Posted by Merri
    Remove StrConv and any string processing besides copying byte array to a string and vice versa and you get much faster result (especially with a compiled program).
    You mean something like this ?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemoryStrToAny Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, ByVal pSrc As String, ByVal ByteLen As Long)
    4. Private Declare Sub CopyMemoryAnyToStr Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As String, pSrc As Any, ByVal ByteLen As Long)
    5.  
    6. Public Function RndCrypt(ByRef Str As String, ByVal Password As String) As String
    7.     '
    8.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    9.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=231798[/url]
    10.     '
    11.     Dim SK As Long, K As Long, Data() As Byte
    12.    
    13.     ReDim Data(Len(Str) - 1)
    14.     CopyMemoryStrToAny Data(0), Str, Len(Str)
    15.    
    16.     ' init randomizer for password
    17.     Rnd -1
    18.     Randomize Len(Password)
    19.     ' (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd)) -> makes sure that a
    20.     ' password like "pass12" does NOT give the same result as the password "sspa12" or "12pass"
    21.     ' or "1pass2" etc. (or any combination of the same letters)
    22.    
    23.     For K = 1 To Len(Password)
    24.         SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))
    25.     Next K
    26.    
    27.     ' init randomizer for encryption/decryption
    28.     Rnd -1
    29.     Randomize SK
    30.    
    31.     ' encrypt/decrypt every character using the randomizer
    32.     For K = 0 To UBound(Data)
    33.         Data(K) = Fix(256 * Rnd) Xor Data(K)
    34.     Next K
    35.    
    36.     RndCrypt = String(Len(Str), 0)
    37.     CopyMemoryAnyToStr RndCrypt, Data(0), Len(Str)
    38. End Function

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Fastest Encryption

    There are slowdown factors:
    • In CopyMemory, you're passing as string. This causes Unicode <-> ANSI conversion behind the scenes.
    • Doing API calls is slow.
    • Mid$ and Asc are very slow.
    • String$ is slow.
    • Password is passed to the function ByVal (= extra copy made of the string = slow).

    Every time you copy memory, every time you handle memory, every time you calculate anything, you're slowing things down. It is a challenge to get rid of all the things that unnecessarily slowdown the code

    Of course, it is all up to how much speed is required and how it is required. For example, doing lots of sequential calls for small files would require minimal bloat between the actual processing of the data, thus CopyMemory would be a major cause of slowdown with, say, 10000 files. In the other hand, with smaller amount of big files it would be important to make sure the main processing is fast and most of the waiting is caused by the main processing.


    But if we truly wanted the fastest encryption, we'd use ASM and not VB.

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

    Re: Fastest Encryption

    So, how would you write the code improving the points you made ?

    I want to learn how to make fast code

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    Re: Fastest Encryption

    Thx for the rndCrypt function. it's quite quick. Exactly what I was looking for. Hopefully won't be too similar that people can recognize characters.

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