VB - 31 Bit Encryption function-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 59

Thread: VB - 31 Bit Encryption function

  1. #1

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

    VB - 31 Bit Encryption function

    You can Encrypt AND Decrypt using the same function (with the same password of course)
    VB Code:
    1. Public Function RndCrypt(ByVal 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
    7.    
    8.     ' init randomizer for password
    9.     Rnd -1
    10.     Randomize Len(Password)
    11.     ' (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd)) -> makes sure that a
    12.     ' password like "pass12" does NOT give the same result as the password "sspa12" or "12pass"
    13.     ' or "1pass2" etc. (or any combination of the same letters)
    14.    
    15.     For K = 1 To Len(Password)
    16.         SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))
    17.     Next K
    18.    
    19.     ' init randomizer for encryption/decryption
    20.     Rnd -1
    21.     Randomize SK
    22.    
    23.     ' encrypt/decrypt every character using the randomizer
    24.     For K = 1 To Len(Str)
    25.         Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))
    26.     Next K
    27.    
    28.     RndCrypt = Str
    29. End Function
    Last edited by CVMichael; Nov 30th, 2005 at 10:23 AM.

  2. #2
    Lively Member putta's Avatar
    Join Date
    Oct 2004
    Location
    Original Citizen Of This Planet
    Posts
    86

    Talking Re: VB - 31 Bit Encryption function

    Hi Micheal,


    Was a useful code. One which really worked the way I wanted it. Thanks to you. One works out of 100zz.... thanks once again..

    --putta:-)

  3. #3
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    786

    Re: VB - 31 Bit Encryption function

    Forgive me for I am new at this encryption/hashing. Can you tell me in this code what each line is doing. For example:

    Public Function RndCrypt(ByVal Str As String, ByVal Password As String) As String
    What is the 'Str' being passed in? I can see the 'Password is the value to encrypt/unencrypt but am not sure what the 'Str' is. Again I basically have no experience with encryption/hashing.


    Thanks

    Your code:
    Public Function RndCrypt(ByVal Str As String, ByVal Password As String) As String
    Dim SK As Long, K As Long

    ' init randomizer for password
    Rnd -1
    Randomize Len(Password)
    ' (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd)) -> makes sure that a
    ' password like "pass12" does NOT give the same result as the password "sspa12" or "12pass"
    ' or "1pass2" etc. (or any combination of the same letters)

    For K = 1 To Len(Password)
    SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))
    Next K

    ' init randomizer for encryption/decryption
    Rnd -1
    Randomize SK

    ' encrypt/decrypt every character using the randomizer
    For K = 1 To Len(Str)
    Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))
    Next K

    RndCrypt = Str
    End Function

  4. #4
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    786

    Re: VB - 31 Bit Encryption function

    And of course I neglected to format the code!! My apologies...

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Re: VB - 31 Bit Encryption function

    I tryed your encryption code. and tryed as a test to encrypt you example as a test in a text box. after decryption this is all I got back. what's wrong? any ideas

    [Highlight=VB]
    Public Function RndCrypt(ByVal Str As String, ByVal Password As String) As String
    Dim SK As Long, K As Long

    ' init ran
    [Highlight=VB]
    When your dreams come true.
    On error resume pulling hair out.

  6. #6

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

    Re: VB - 31 Bit Encryption function

    mojo69, if you want to encrypt, you generally have a string that you want to encrypt and the password to encrypt it with...

    So, having said that, wich one do you think is which ?


    Quote Originally Posted by dreamvb
    after decryption this is all I got back.
    What did you get back ?

  7. #7
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Re: VB - 31 Bit Encryption function

    What did you get back ?
    Well basically your example does not decrypt correctly. for example. Asumeing I paste this into a text Box

    line 1
    line 2
    line 3
    line 4
    line 5
    line 6
    line 7
    line 8

    Now I use your code in a command button example:

    Private Sub Command1_Click()
    Text1.Text = RndCrypt(Text1.Text, "pass")
    End Sub

    That will encrypt the the text right. so now I go to decrypt the encryped text and this is what I get back.

    line 1
    line 2
    line 3
    line 4
    lin

    what you should be getting back is the original data that was put in.
    anyway it works ok for small passwords.
    When your dreams come true.
    On error resume pulling hair out.

  8. #8

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

    Re: VB - 31 Bit Encryption function

    There's nothing wrong with my encryption function.

    Did you try just storing the encrypted text in a string ? (without putting the output in the TextBox ?)

    Why do you need to display something (the encrypted text) that you won't be able to understand anyways ?

    You should test and see why it hapens before you blame someone else.
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim S As String
    3.     S = RndCrypt(Text1.Text, "pass")
    4.    
    5.     Text2.Text = S
    6.     Debug.Print Len(S), Len(Text2.Text), Asc(Mid(S, 36, 1)), Asc(Mid(S, 37, 1))
    7. End Sub
    You will see that Len(Text2.Text) = 35 even thought the Len(S) = 62, so how is that possible ?, it means that when you assign S to the text box, not all data goes in it...

    Why only 35 characters go in the text box ? because the 36'th character is 0...
    A text box stops displaying the text if it encounters 0... so there you go... that's the problem.

    And as I was saying before, you should not display the encrypted text for 2 reasons. One you don't need to look at it ? (or do you ?) and two, the text box control does not display all data if the data contains the 0 ASCII character...

  9. #9

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

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by dreamvb
    anyway it works ok for small passwords.
    Actually, the bigger the password, the more secure is the encryption... and it works with a password of any size...

  10. #10
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Re: VB - 31 Bit Encryption function

    Ok Thanks. I take it I was in the wrong. anyway nise Code keep up the good work.
    5 * from me
    When your dreams come true.
    On error resume pulling hair out.

  11. #11
    New Member
    Join Date
    Nov 2005
    Posts
    3

    Re: VB - 31 Bit Encryption function

    I've got a problem (and it could be me, I suppose). If I use the following combination of string and password it returns an inconclusive final character:

    pumpkins
    shadowhawk

    All other combinations seem to work OK, but with these two the returned string has a NULL on the end as far as I can tell.
    Any clues, or is it me?
    Thanks,
    Chips.

  12. #12
    New Member
    Join Date
    Nov 2005
    Posts
    3

    Re: VB - 31 Bit Encryption function

    Ahhh..
    CVMichael, you seem to have already answered this one, although I still need to understand why this happens.
    Rather than display the result as text, I debug.print the HEX value of each character and the final value in this case is '0', not '00' as I'd expect.
    Chips.

  13. #13
    New Member
    Join Date
    Nov 2005
    Posts
    3

    Re: VB - 31 Bit Encryption function

    OK, I really should have sat down and worked this out before I posted.
    Sorry to waste your time. It works perfectly. Rating your original post right now.

    Thanks,
    Chips.

  14. #14
    New Member
    Join Date
    Jun 2005
    Posts
    8

    Re: VB - 31 Bit Encryption function

    hi
    one more such function is

    Public Function Crypt(strOriginal, encrypting) As String
    Dim valString$, x%
    For x = 1 To Len(strOriginal)
    If encrypting = True Then
    valString = Asc(Mid(strOriginal, x, 1)) + (x ^ 2)
    Do While valString > 255
    valString = valString - 255
    Loop
    Else
    valString = Asc(Mid(strOriginal, x, 1)) - (x ^ 2)
    Do While valString <= 0
    valString = valString + 255
    Loop
    End If
    Crypt = Crypt & Chr(valString)
    Next

    End Function

  15. #15

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

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by sakshi
    hi
    one more such function is
    VB Code:
    1. Public Function Crypt(strOriginal, encrypting) As String
    2.     Dim valString$, x%
    3.     For x = 1 To Len(strOriginal)
    4.         If encrypting = True Then
    5.             valString = Asc(Mid(strOriginal, x, 1)) + (x ^ 2)
    6.             Do While valString > 255
    7.             valString = valString - 255
    8.             Loop
    9.          Else
    10.             valString = Asc(Mid(strOriginal, x, 1)) - (x ^ 2)
    11.             Do While valString <= 0
    12.                 valString = valString + 255
    13.             Loop
    14.         End If
    15.         Crypt = Crypt & Chr(valString)
    16.     Next
    17. End Function
    Hi sakshi, remember when you paste code, to encapsulate it in [vbcode]your vb code here[/vbcode]

    This code:
    VB Code:
    1. Do While valString > 255
    2.    valString = valString - 255
    3. Loop
    can be written as:
    VB Code:
    1. valString = ValString Mod 256
    Instead of the do while loop...

    Also, the function you pasted, is an encoding function, NOT an encryption function. An encryption function requires a password also. This function always encodes the same. An encryption function has different result every time the password changes.

  16. #16
    Lively Member Tegan's Avatar
    Join Date
    Jun 2005
    Location
    Lincoln, NE
    Posts
    108

    Re: VB - 31 Bit Encryption function

    Is there any way to modify your code to only print alphanumeric characters?
    Also It would be nice if you could set the length of the encrypted output.

    Say I wanted to encrypted "mynameistegan" using the key "mykey" but I wanted to make sure that the encrypted output is always 25 characters.
    Tegan Snyder

  17. #17

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

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by Tegan
    Is there any way to modify your code to only print alphanumeric characters?
    That will defy the purpose of the encryption. The encryption can encrypt binary data, so therefore the output will be binary.
    For what you are asking you should use Base64 encoding after you encrypt your data.
    Here is a link to my Base64 Encoding and Decoding.
    http://www.vbforums.com/showpost.php...53&postcount=4

    Quote Originally Posted by Tegan
    Also It would be nice if you could set the length of the encrypted output.

    Say I wanted to encrypted "mynameistegan" using the key "mykey" but I wanted to make sure that the encrypted output is always 25 characters.
    The RndCrypt function does not change the length of the string, so before you encrypt, you can set YOUR data the predefined length that you need.
    Something like:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim Str As String, EncStr As String
    3.    
    4.     ' some string to encrypt
    5.     Str = "akjhgkjdfhgklsjdfhgf"
    6.    
    7.     ' make the string 50 characters long
    8.     Str = Str & String(50 - Len(Str), 0)
    9.    
    10.     ' encrypt the string
    11.     EncStr = RndCrypt(Str, "my password")
    12.    
    13.     ' the encrypted length is 50 (same as the original)
    14.     Debug.Print Len(Str), Len(EncStr)
    15. End Sub

  18. #18
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Re: VB - 31 Bit Encryption function

    Changed a few things around and got the function executing faster.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount Lib "kernel32" () As Long
    4.  
    5. Private Function RandomBinaryB(ByVal tmpLength As Long) As String
    6.     Dim tmpRandomBinaryB() As Byte
    7.     Dim tmpIndex As Long
    8.     ReDim tmpRandomBinaryB(tmpLength - 1)
    9.     For tmpIndex = 0 To tmpLength - 1
    10.         tmpRandomBinaryB(tmpIndex) = Int(Rnd * 256)
    11.     Next
    12.     RandomBinaryB = StrConv(tmpRandomBinaryB, vbUnicode)
    13. End Function
    14.  
    15. Public Function RndCrypt(ByVal Str As String, ByVal Password As String) As String
    16.     ' Made by Michael Ciurescu (CVMichael from vbforums.com)
    17.     ' Original thread: [url]http://www.vbforums.com/showthread.php?t=231798[/url]
    18.     Dim SK As Long, K As Long
    19.     Rnd -1
    20.     Randomize Len(Password)
    21.     For K = 1 To Len(Password)
    22.         SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))
    23.     Next K
    24.     Rnd -1
    25.     Randomize SK
    26.     For K = 1 To Len(Str)
    27.         Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))
    28.     Next K
    29.     RndCrypt = Str
    30. End Function
    31.  
    32. Public Function RndCryptB(ByRef tmpToEncrypt As String, ByVal tmpPassword As String) As String
    33.     ' Original function/idea by Michael Ciurescu (CVMichael from vbforums.com)
    34.     ' This function by frozen on vbforums.com
    35.     ' Original thread: [url]http://www.vbforums.com/showthread.php?t=231798[/url]
    36.     Dim tmpToEncryptB() As Byte
    37.     Dim tmpPasswordB() As Byte
    38.     Dim tmpIndex As Long
    39.     Dim tmpSeed As Long
    40.     Rnd -1
    41.     Randomize Len(tmpPassword)
    42.     tmpPasswordB = StrConv(tmpPassword, vbFromUnicode)
    43.     For tmpIndex = 0 To UBound(tmpPasswordB) - 1
    44.         tmpSeed = tmpSeed + (((tmpIndex Mod 256) Xor tmpPasswordB(tmpIndex)) Xor Fix(256 * Rnd))
    45.     Next
    46.     Rnd -1
    47.     Randomize tmpSeed
    48.     tmpToEncryptB = StrConv(tmpToEncrypt, vbFromUnicode)
    49.     For tmpIndex = 0 To UBound(tmpToEncryptB) - 1
    50.         tmpToEncryptB(tmpIndex) = Fix(256 * Rnd) Xor tmpToEncryptB(tmpIndex)
    51.     Next tmpIndex
    52.     RndCryptB = StrConv(tmpToEncryptB, vbUnicode)
    53. End Function
    54.  
    55. Private Sub Form_Load()
    56.     Dim tmpBinary As String
    57.     Dim tmpIndex As Long
    58.     Dim tmpTicks As Long
    59.     tmpBinary = RandomBinaryB(1048576)
    60.     For tmpIndex = 0 To 5
    61.         tmpTicks = GetTickCount()
    62.         Call RndCrypt(tmpBinary, "abc")
    63.         Debug.Print tmpIndex, "RndCrypt(tmpBinary, abc)", GetTickCount() - tmpTicks
    64.         tmpTicks = GetTickCount()
    65.         Call RndCryptB(tmpBinary, "abc")
    66.         Debug.Print tmpIndex, "RndCryptB(tmpBinary, abc)", GetTickCount() - tmpTicks
    67.     Next
    68. End Sub

    Code:
     0            RndCrypt(tmpBinary, abc)     828 
     0            RndCryptB(tmpBinary, abc)    313 
     1            RndCrypt(tmpBinary, abc)     812 
     1            RndCryptB(tmpBinary, abc)    313 
     2            RndCrypt(tmpBinary, abc)     828 
     2            RndCryptB(tmpBinary, abc)    328 
     3            RndCrypt(tmpBinary, abc)     844 
     3            RndCryptB(tmpBinary, abc)    328 
     4            RndCrypt(tmpBinary, abc)     812 
     4            RndCryptB(tmpBinary, abc)    313 
     5            RndCrypt(tmpBinary, abc)     828 
     5            RndCryptB(tmpBinary, abc)    312
    Last edited by frozen; Feb 3rd, 2007 at 06:47 AM.

  19. #19

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

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by frozen
    Changed a few things around and got the function executing faster.
    It's nice, but... I could have done the same thing if anyone would have asked me to....

  20. #20
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Re: VB - 31 Bit Encryption function

    Why bother, you came up with the idea and kept it simple for people looking to learn. I was just using it on rather large strings and had to speed it up so I posted it.

    Just so you know, your the man. I love your code.

  21. #21
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: VB - 31 Bit Encryption function

    Can anyone tell me about how strong 31 bit encryption is? I've read that 128 is "uncrackable" with today's technology but roughly how strong is 31 bit?
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  22. #22

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

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by Chris H
    Can anyone tell me about how strong 31 bit encryption is? I've read that 128 is "uncrackable" with today's technology but roughly how strong is 31 bit?
    Well, let me put it this way:
    31 bit takes 2^31 = 2147483648 tries to break the encryption
    128 bit takes 2^128 = 3.4028236692093846346337460743177e+38 tries to break it....

    With my own computer (P4 3.5GHz), it would probably take a few hours to brake a 31 bit encryption

  23. #23
    New Member
    Join Date
    May 2007
    Posts
    1

    Re: VB - 31 Bit Encryption function

    Duh.. nevermind
    Last edited by camdagr8; May 23rd, 2007 at 11:43 AM.

  24. #24
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: VB - 31 Bit Encryption function

    I'm not real experienced in encryption, but what makes this 31-bit? I'm not really sure. I've been told the length of the key is what determines what "bit" encryption it is, but I don't see how that would be true.

    Also, what modifications would it take to make this into 128-bit encryption, 256? No need, it already seems pretty secure, I'm just curious.

  25. #25

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

    Re: VB - 31 Bit Encryption function

    What makes it 31 bit ?

    Well, basically, how many tries (when broute forcing) it takes to break the encryption. And that number is how big the "key" is.

    I this case, the "key" is the SK variable which is a Long type (32 bits), but in the for loop, I make the key starting at 0, so there is no negative numbers, therefore I'm not using one bit of the 32.

    The encryption is actually less than 31 bit, because of the way the key is made, there are many combinations that can result into the same key.

    So, for the 31 bit encryption, it takes 2^31=2147483648 tries to break it.

    A 128 bit encryption has a key of 16 bytes (128 / 8), for this encryption it takes 2^128=3.4028236692093846346337460743177e+38 tries to break it, and that is a pretty big number...

  26. #26
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by CVMichael
    What makes it 31 bit ?

    Well, basically, how many tries (when broute forcing) it takes to break the encryption. And that number is how big the "key" is.

    I this case, the "key" is the SK variable which is a Long type (32 bits), but in the for loop, I make the key starting at 0, so there is no negative numbers, therefore I'm not using one bit of the 32.

    The encryption is actually less than 31 bit, because of the way the key is made, there are many combinations that can result into the same key.

    So, for the 31 bit encryption, it takes 2^31=2147483648 tries to break it.

    A 128 bit encryption has a key of 16 bytes (128 / 8), for this encryption it takes 2^128=3.4028236692093846346337460743177e+38 tries to break it, and that is a pretty big number...
    Oh ok, that makes sense.

    What if the text is only 4 bytes long though and the key is 128? It would still only take 16 tries right?

    Sorry for the dumb questions.

  27. #27

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

    Re: VB - 31 Bit Encryption function

    Since the text is only 4 bytes, then it means that the encryption strength is 32 bits (4 bytes = 32 bits), even if the key is 128 bits (16 bytes). You need a text at least 16 bytes to take advantage of the full key...
    Quote Originally Posted by DigiRev
    What if the text is only 4 bytes long though and the key is 128? It would still only take 16 tries right?
    I think you are confuzing what are bits, and what are bytes...

    1 Byte = 8 bits = (2^8)=256 combinations
    So 4 bytes = 4*8=32 bits... (2^32)=4,294,967,296 combinations

    So if it's 4 bytes, then it takes 4,294,967,296 tries to break it... (that's how many combinations a long can take)

  28. #28
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by CVMichael
    Since the text is only 4 bytes, then it means that the encryption strength is 32 bits (4 bytes = 32 bits), even if the key is 128 bits (16 bytes). You need a text at least 16 bytes to take advantage of the full key...

    I think you are confuzing what are bits, and what are bytes...

    1 Byte = 8 bits = (2^8)=256 combinations
    So 4 bytes = 4*8=32 bits... (2^32)=4,294,967,296 combinations

    So if it's 4 bytes, then it takes 4,294,967,296 tries to break it... (that's how many combinations a long can take)
    Ah, that makes sense. I was thinking in terms of bytes and not bits.

  29. #29
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: VB - 31 Bit Encryption function

    This is a decent encryption function for personal use, but it does have weaknesses.

    First weakness: the same function encrypts and decrypts the text. This seriously limits the mathematical properties of the function. I personally don't know how to exploit this, but it was one of the properties that helped them break enigma.

    Second weakness: using VB6's random number generator with a calculated seed is clever, but in general a bad idea. From what I understand, the numbers that come out of typical RNGs are very predictable (from a cryptographic sense). I would bet this step doesn't contribute at all to the encryption, unless the attacker doesn't know the algorithm.

    Third weakness: you're essentially hashing the password to get SK, which means multiple passwords will give the same result and SK can only have, as you say in the title, ~31 bits. Modern cryptographic algorithms use more along the lines of 128 to 1024 bits (or more, if you're really paranoid). Worse than that, I think some values of SK are more likely than others (not sure though).

    How I would attack this encryption:
    - Figure out which values of SK are the most likely (possible taking into account the fact that normal people tend to use short passwords)
    - Test each value, one after the other, until 'text' comes out (something that matches the frequency analysis for English)
    Don't pay attention to this signature, it's contradictory.

  30. #30

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

    Re: VB - 31 Bit Encryption function

    Very good alkatran,

    If you said those things, it means that you understand how the encryption works, and have a good idea how to crack it.

    The thing is... I knew about the weaknesses for this encryption when I posted it. And I did not post it saying that this is a good encryption... I know it's a weak encryption. But thinking how many lines of code this one is for what it does... I think it's pretty good. I mean, beginners usually write encryptions by substracting a constant # from the ASCII code in the text, and call it encryption (but not to my standards), with a lot more code than this encryption.

    This encryption is for when you want to hide things as opposed to plain text, that's why I wrote in the title "31 Bit...", thinking that everyone with some basic encryption knoledge, knows that 128 bit and higher is the standard today.

    If you want to see a good encryption, take a look at this one:
    VB - 128, 160 and 256 Bit File Encryption/Decryption with MD5, SHA1 and SHA256 (which is made by me also)
    And yes... I know the pros & cons about that encryption also...
    Last edited by CVMichael; Jun 10th, 2007 at 11:21 AM.

  31. #31
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: VB - 31 Bit Encryption function

    This is the simple encryption suitable for beginner like me.

    Hence, I choose this encryption because my project do not required expert encryption.I also read through your another advance encryption. That was good and really professional.

    I implemented your 32-bits encryption to my program. It was running smooth.
    However, some of my network computer can't run this encryption. I study all the possible problem i found that my network vb compiler cannot compile the character . Perhaply and possiblility that computer installed chinese input visual basic to allow user enter chinese work for coding.

    Is that the problem on my side ?

    Can you help me to explore it ?

    Thanks
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  32. #32

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

    Re: VB - 31 Bit Encryption function

    Sorry, I don't know how to work with Unicode data. Maybe you should ask your question in the Classic Visual Basic, that way, people who know about unicode will reply to your question... hopefully...

  33. #33
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: VB - 31 Bit Encryption function

    Ok CVMichael .
    Thanks ..... ..... ..... .....
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  34. #34
    Member
    Join Date
    Aug 2007
    Posts
    40

    Re: VB - 31 Bit Encryption function

    WOW!
    I was looking for a way to encrypt some data... Ur code is amazing!! Works better than i expect. Thank you bud

  35. #35
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,116

    Re: VB - 31 Bit Encryption function

    Hi CVMichael,

    In order to understand security, one has to really understand how to break in or brute force. A person who has a good security system for their home is a person who knows how to break in their own home.

    Therefore, I have three questions for you. One, how did you get so good with the encryption/decryption process?

    Quote Originally Posted by CVMichael
    Well, let me put it this way:
    31 bit takes 2^31 = 2147483648 tries to break the encryption
    128 bit takes 2^128 = 3.4028236692093846346337460743177e+38 tries to break it....

    With my own computer (P4 3.5GHz), it would probably take a few hours to brake a 31 bit encryption
    Two, how would you do this? I am very curious to understand this to understand counter preventative measures.

    Three, what is a good pattern to keep passwords?

    Thanks

    Liquid Metal

    BTW...
    Code:
            Mid$(pi_strData, lngLP, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(pi_strData, lngLP, 1)))
    I am a bit lost with this line. How does it know whether to pick the CHR side or the ASC side?
    Last edited by Liquid Metal; Nov 27th, 2007 at 06:26 PM.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  36. #36
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: VB - 31 Bit Encryption function

    I had solved my problem.
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  37. #37

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

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by Liquid Metal
    One, how did you get so good with the encryption/decryption process?
    Well... I was always fascinated by it, and in the past I spent weeks at a time to try to make encryption algorithms, I made so many I lost count. I once wanted to see how many I can make, and made around 20 (I did not keep the code) most of them using XOR but in different ways. I stopped at around 20 because I did not see any point in making so many, but theoretically, I could make hundreds of encryption algorithms, and hundrets of variations for each encryption... of course not many strong ones...
    That's how I came up with this one, it was one of those 20, and also the other encryption algorithm I posted in the CodeBank: High Encryption

    Quote Originally Posted by Liquid Metal
    Two, how would you do this? I am very curious to understand this to understand counter preventative measures.
    You also quoted when I said: "With my own computer (P4 3.5GHz), it would probably take a few hours to brake a 31 bit encryption"
    Well, I was wrong !

    It takes only 2 SECONDS !

    I did not dare to actually try to break it until today *shame on me*, anyways, here's how to do "broute force":
    Code:
    Private Sub Form_Load()
        Dim SK As Long, K As Long
        Dim Str As String
        Dim EncStr As String
        Dim MsgStr As String
        
        ' Encrypt something
        EncStr = RndCrypt("Hello world !", "something 1")
        
        For SK = 1 To 2147483647
            Str = EncStr ' Reset to encrypted string
            
            ' Main encryption algorithm
            Rnd -1
            Randomize SK
            
            For K = 1 To Len(Str)
                Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))
            Next K
            
            ' Check to see if we have a valid string
            'If Str Like "*world*" Then
            If CheckPrintable(Str) Then
                
                MsgStr = "Encryption broken !" & vbNewLine & "Seed = " & SK & vbNewLine & "Full data = """ & Str & """"
                Debug.Print MsgStr
                MsgBox MsgStr
                
                Exit For
            End If
        Next SK
    End Sub
    
    Private Function CheckPrintable(ByVal Str As String) As Boolean
        Dim K As Long
        Dim PrintableChars As String
        
        ' for k = 32 to 126: ? chr(k); : next k
        PrintableChars = vbTab & " !""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
        
        For K = 1 To Len(PrintableChars)
            Str = Replace(Str, Mid$(PrintableChars, K, 1), "")
        Next K
        
        CheckPrintable = Len(Str) = 0
    End Function
    But of course, you need the source code of the encryption algorithm to be able to do this...

    Quote Originally Posted by Liquid Metal
    Three, what is a good pattern to keep passwords?
    Well, I'm not sure what you mean. To keep passwords ? as in to save log-in passwords in a database or file ? The best way is to HASH the password, because HASH-ing is not reversable (one way only)... that's why when you HASH, the ONLY way to break it is to broute force it...
    If you mean what passwords you should use in general, you should not use words like english words (or whatever language), and mix letters with numbers and also characters like +-=/\[]@#$%^&*... etc... (you get the idea)

    Quote Originally Posted by Liquid Metal
    I am a bit lost with this line. How does it know whether to pick the CHR side or the ASC side?
    Well, if you look carefully, the Chr holds everything in brackets, so first this runs:
    Fix(256 * Rnd) Xor Asc(Mid$(pi_strData, lngLP, 1))
    and then the result of all that is in Chr( ... )

    [Edit]
    See the new encryption based on this one, here: VB6 - 31 Bit Encryption - To the Next level
    Last edited by CVMichael; Nov 27th, 2007 at 10:53 PM.

  38. #38
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,116

    Re: VB - 31 Bit Encryption function

    I am not sure if the Brute code worked because it returned some other data. It also took really long.

    Also, I was refering to login passwords.

    I have a question for you on the XOR but will start another thread since it is not really related to your post.

    Thank you CVMichael.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  39. #39

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

    Re: VB - 31 Bit Encryption function

    Quote Originally Posted by Liquid Metal
    I am not sure if the Brute code worked because it returned some other data. It also took really long.
    Really ?
    Did you modify the code in any way ?

    Maybe your computer is slower than mine... I have a P4 3.5GHz 2GBytes RAM

    here's the full code again, but I added timing also:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim SK As Long, K As Long
        Dim Str As String
        Dim EncStr As String
        Dim MsgStr As String
        Dim StartTime As Single
        
        ' Encrypt something
        EncStr = RndCrypt("Hello world !", "something 1")
        
        StartTime = Timer
        For SK = 1 To 2147483647
            Str = EncStr ' Reset to encrypted string
            
            ' Main encryption algorithm
            Rnd -1
            Randomize SK
            
            For K = 1 To Len(Str)
                Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))
            Next K
            
            ' Check to see if we have a valid string
            'If Str Like "*world*" Then
            If CheckPrintable(Str) Then
                
                MsgStr = "Encryption broken !" & vbNewLine & "Seed = " & SK & vbNewLine & "Full data = """ & Str & """" & vbNewLine & "Time taken: " & Timer - StartTime
                Debug.Print MsgStr
                MsgBox MsgStr
                
                Exit For
            End If
        Next SK
    End Sub
    
    Private Function CheckPrintable(ByVal Str As String) As Boolean
        Dim K As Long
        Dim PrintableChars As String
        
        ' for k = 32 to 126: ? chr(k); : next k
        PrintableChars = vbTab & " !""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
        
        For K = 1 To Len(PrintableChars)
            Str = Replace(Str, Mid$(PrintableChars, K, 1), "")
        Next K
        
        CheckPrintable = Len(Str) = 0
    End Function
    
    Public Function RndCrypt(ByVal Str As String, ByVal Password As String) As String
        '
        '  Made by Michael Ciurescu (CVMichael from vbforums.com)
        '  Original thread: http://www.vbforums.com/showthread.php?t=231798
        '
        Dim SK As Long, K As Long
        
        ' init randomizer for password
        Rnd -1
        Randomize Len(Password)
        ' (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd)) -> makes sure that a
        ' password like "pass12" does NOT give the same result as the password "sspa12" or "12pass"
        ' or "1pass2" etc. (or any combination of the same letters)
        
        For K = 1 To Len(Password)
            SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))
        Next K
        
        ' init randomizer for encryption/decryption
        Rnd -1
        Randomize SK
        
        ' encrypt/decrypt every character using the randomizer
        For K = 1 To Len(Str)
            Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))
        Next K
        
        RndCrypt = Str
    End Function
    This is the result I get:
    Code:
    Encryption broken !
    Seed = 1746
    Full data = "Hello world !"
    Time taken: 0.359125
    So on my computer it takes only 1746 tries until it breaks the encryption, and that is done in only 360 ms

  40. #40
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,116

    Re: VB - 31 Bit Encryption function

    It works this time. Must be on my part earlier. Thanks for introducing to me the concept of brute force. I know what the phase mean but just never understand how one might crack the algorithm until now. I am going to try to understand it line by line.

    I understand how XOR works now too, thanks to you, Si_The_Geek, LaVolpe and the rest from this post:
    http://www.vbforums.com/showthread.php?t=498656.

    Thanks again CVMichael!
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

Page 1 of 2 12 LastLast

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.