Results 1 to 11 of 11

Thread: Simple key 'encryption'

  1. #1

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Simple key 'encryption'

    VB Code:
    1. Private Function InvertString(sT As String) As String
    2. Dim sTT As String
    3.  
    4. For I = Len(sT) To 1 Step -1
    5. sTT = sTT & Mid(sT, I, 1)
    6. Next
    7.  
    8. InvertString = sTT
    9.  
    10. End Function
    11.  
    12. Private Function Encrypt(sT As String, sKey As String) As String
    13. Dim sEncrypted As String
    14. Dim keyCount As Integer
    15.  
    16. sT = InvertString(sT)
    17. sKey = InvertString(sKey)
    18. keyCount = 1
    19. For I = 1 To Len(sT)
    20. sEncrypted = sEncrypted & Chr(Asc(Mid(sT, I, 1)) + Mid(sKey, keyCount, 1))
    21. keyCount = keyCount + 1
    22. If keyCount > Len(sKey) Then keyCount = 1
    23. Next
    24.  
    25. Encrypt = sEncrypted
    26.  
    27. End Function
    28.  
    29. Private Function Decrypt(sT As String, sKey As String) As String
    30. Dim sDecrypted As String
    31. Dim keyCount As Integer
    32.  
    33. sKey = InvertString(sKey)
    34. keyCount = 1
    35.  
    36. For I = 1 To Len(sT)
    37. sDecrypted = sDecrypted & Chr(Asc(Mid(sT, I, 1)) - Mid(sKey, keyCount, 1))
    38. keyCount = keyCount + 1
    39. If keyCount > Len(sKey) Then keyCount = 1
    40. Next
    41.  
    42. Decrypt = InvertString(sDecrypted)
    43.  
    44. End Function

    Just use, Encrypt() And Decrypt() - remember to pass the same key!!
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Did you try your own code before posting it ?

    First of all, it seems that you like to declare variables global (I think)., because variable "I" is not declared anywhere in any of the functions...

    Second, I gt a "Type mismatch", at this line:
    VB Code:
    1. sEncrypted = sEncrypted & Chr(Asc(Mid(sT, I, 1)) + Mid(sKey, keyCount, 1))

    Asc(Mid(sT, I, 1)) returns a number, and you want to add it to character Mid(sKey, keyCount, 1)...

    Third, even if I put Chr(Mid(sKey, keyCount, 1)), it will add the 2 numbers togeter, and if the result is over 255, it will also give an error...

    Fourth... there are LOTS of other posts with lame addition/substraction encryptions, that are really easy to crack, if you reverse the strings before you encrypt it, it won't add much of a "twist" to it...

    Take a look at the encryptions I posted in the CodeBank, even this encryption is better than what you posted, and it's less code than that
    VB - 31 Bit Encryption function
    And after you look at that one, look at this one:
    VB - 128, 160 and 256 Bit File Encryption/Decryption with MD5, SHA1 and SHA256

  3. #3

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994
    Originally posted by CVMichael
    Did you try your own code before posting it ?

    First of all, it seems that you like to declare variables global (I think)., because variable "I" is not declared anywhere in any of the functions...

    Second, I gt a "Type mismatch", at this line:
    VB Code:
    1. sEncrypted = sEncrypted & Chr(Asc(Mid(sT, I, 1)) + Mid(sKey, keyCount, 1))

    Asc(Mid(sT, I, 1)) returns a number, and you want to add it to character Mid(sKey, keyCount, 1)...

    Third, even if I put Chr(Mid(sKey, keyCount, 1)), it will add the 2 numbers togeter, and if the result is over 255, it will also give an error...

    Fourth... there are LOTS of other posts with lame addition/substraction encryptions, that are really easy to crack, if you reverse the strings before you encrypt it, it won't add much of a "twist" to it...

    Take a look at the encryptions I posted in the CodeBank, even this encryption is better than what you posted, and it's less code than that
    VB - 31 Bit Encryption function
    And after you look at that one, look at this one:
    VB - 128, 160 and 256 Bit File Encryption/Decryption with MD5, SHA1 and SHA256
    Whatever you say, I don't have any problems running the code, and I did call it SIMPLE encryption, furthermore, if you don't like it - don't use it, I really don't care.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  4. #4
    Addicted Member SpeedyDog's Avatar
    Join Date
    Feb 2004
    Location
    Missouri, USA
    Posts
    189
    yeah... I got that type mismatch too :-P

  5. #5
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    He did say it was simple. Here, I converted his code into something that compiles. I like it - It gives you a simple layer of security when you just don't want anyone reading your plaintext and you don't care if they try to hack into it.

    VB Code:
    1. Option Explicit
    2. '
    3. Private Const KEY As String = "Key1"
    4. '
    5.  
    6. Private Sub Command1_Click()
    7.     Text2 = Encrypt(Text1, KEY)
    8. End Sub
    9.  
    10. Private Sub Command2_Click()
    11.     Text3 = Decrypt(Text2, KEY)
    12. End Sub
    13.  
    14. Private Function InvertString(sT As String) As String
    15.     Dim sTT As String
    16.     Dim i As Integer
    17.     '
    18.     For i = Len(sT) To 1 Step -1
    19.         sTT = sTT & Mid(sT, i, 1)
    20.     Next
    21.     '
    22.     InvertString = sTT
    23. End Function
    24.  
    25. Private Function Encrypt(sT As String, sKey As String) As String
    26.     Dim strEncrypted As String
    27.     Dim keyCount As Long
    28.     Dim i As Long
    29.     Dim lngTemp1 As Long
    30.     Dim lngTemp2 As Long
    31.     '
    32.     sT = InvertString(sT)
    33.     sKey = InvertString(sKey)
    34.     keyCount = 1
    35.     '
    36.     For i = 1 To Len(sT)
    37.         '
    38.         lngTemp1 = Asc(Mid(sT, i, 1))
    39.         lngTemp2 = Asc(Mid(sKey, keyCount, 1))
    40.         strEncrypted = strEncrypted & Chr(lngTemp1 + lngTemp2)
    41.         keyCount = keyCount + 1
    42.         '
    43.         If keyCount > Len(sKey) Then
    44.             keyCount = 1
    45.         End If
    46.     Next
    47.     '
    48.     Encrypt = strEncrypted
    49. End Function
    50.  
    51. Private Function Decrypt(sT As String, sKey As String) As String
    52.     Dim strDecrypted As String
    53.     Dim keyCount As Integer
    54.     Dim i As Long
    55.     Dim lngTemp1 As Long
    56.     Dim lngTemp2 As Long
    57.     '
    58.     sKey = InvertString(sKey)
    59.     keyCount = 1
    60.     '
    61.     For i = 1 To Len(sT)
    62.         '
    63.         lngTemp1 = Asc(Mid(sT, i, 1))
    64.         lngTemp2 = Asc(Mid(sKey, keyCount, 1))
    65.         strDecrypted = strDecrypted & Chr(lngTemp1 - lngTemp2)
    66.         keyCount = keyCount + 1
    67.         '
    68.         If keyCount > Len(sKey) Then
    69.             keyCount = 1
    70.         End If
    71.     Next
    72.     '
    73.     Decrypt = InvertString(strDecrypted)
    74. End Function

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    It encrypts only strings where the ASCII values are less than 128, and the key has to have the ASCII values less than 128 also.

    Encrypt something like this, and you'll see what I mean:
    VB Code:
    1. Debug.Print Encrypt(String(20, 254), KEY)

  7. #7
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Thumbs up

    The code author never meant for it to encrypt "binary data". Just the printable ASCII characters. Again, it works very well for what it was designed to do.

  8. #8
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Originally posted by CVMichael
    VB - 31 Bit Encryption function
    This is very cool, too. You're right, much less code. I'm not sure how one would rate it better or worse than the original post, except to see which one is more crackable..?

    I think it's cool you got it to work as a single function.

  9. #9
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322
    You guys may want to check out CAPICOM. It's bascially a com wrapper around the cryptoapi. Would probably be a easy means for security in your applications.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Resolved Re: Simple key 'encryption'

    Windows 2000 Workstation has a problem with Capicom, that I'm trying to figure out.


    EDIT:

    As it turns out, it only seems to have a problem with

    CAPICOM_ENCRYPTION_ALGORITHM_AES

    but,

    CAPICOM_ENCRYPTION_ALGORITHM_3DES

    works fine under both XP and W2K...
    Last edited by dglienna; Jan 7th, 2005 at 01:55 AM.

  11. #11
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    307

    Re: Simple key 'encryption'

    when i try it, it only runs once then errors out. any thoughts?
    "Anybody can get angry or sad and frown but it takes a person with character to smile when times are hard."

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