Results 1 to 8 of 8

Thread: Encryption & Decryption

  1. #1

    Thread Starter
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Encryption & Decryption

    Hello everyone, I have been lurking over the forums for some time now, not posting. I have decided to make my own contribution. I don't know if anyone else has done this before or whatever, and I don't know how strong it is..

    Code:
    Public Function EncryptF(ByVal Password As String, ByVal Text As String) As String
    Dim Temp As Integer
    Dim i As Long
    If (Len(Password) < Len(Text)) Then
        Temp = Len(Text) / Len(Password)
        For i = 1 To Temp + 1
            Password = Password & Password
        Next i
    End If
    EncryptF = vbNullString
    For i = 1 To Len(Text)
        Temp = Asc(Mid(Text, i, 1)) + Asc(Mid(Password, i, 1))
        EncryptF = EncryptF & Chr(Temp)
    Next i
    Exit Function
    ErrorH:
    MsgBox ErrHandle, vbCritical, "Error"
    End Function
    
    Public Function DecryptF(ByVal Password As String, ByVal Text As String) As String
    On Error GoTo ErrorH
    Dim Temp As Integer
    Dim i As Long
    If (Len(Password) < Len(Text)) Then
        Temp = Len(Text) / Len(Password)
        For i = 1 To Temp + 1
            Password = Password & Password
        Next i
    End If
    DecryptF = vbNullString
    For i = 1 To Len(Text)
        Temp = Asc(Mid(Text, i, 1)) - Asc(Mid(Password, i, 1))
        DecryptF = DecryptF & Chr(Temp)
    Next i
    Exit Function
    ErrorH:
    MsgBox ErrHandle, vbCritical, "Error"
    End Function
    
    
    Private Function ErrHandle() As String
    If (Err.Description <> "") Then
        ErrHandle = "An error has occured!" & vbNewLine & Err.Number & ") " & Err.Description
    End If
    End Function
    



    The source for the Module is above, and there should be an attachment, with it in a module.
    Credits go to me!

    Warning: Attachment is out of date.


    Edit: Oh I forgot to say.. it doesn't work with special characters!
    Attached Files Attached Files
    Last edited by Vanasha; Sep 13th, 2007 at 10:58 AM. Reason: Code update

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Encryption & Decryption

    Quote Originally Posted by Vanasha
    . . . I don't know if anyone else has done this before or whatever
    A few times:

    http://vbforums.com/showthread.php?t=292138
    http://vbforums.com/showthread.php?t=404980
    http://vbforums.com/showthread.php?t=311963

    BTW, nice piece of code
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3

    Thread Starter
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Encryption & Decryption

    Hehe, thanks. I didn't know anyone else did it.. but hey.

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

    Re: Encryption & Decryption

    Here's just a few quick suggestions if you don't mind:

    1. Don't pass the Password and Text arguments as ByVal.
    2. Don't use string concatenation (String1 = String1 & Something). In your case, the output will always be the same length as the input so you can buffer enough space to store the output before-hand:

    EncryptF = Space$(Len(Text))


    And instead of:
    EncryptF = EncryptF & Chr(Temp)
    You could have:
    Mid$(EncryptF, i, 1) = Chr$(Temp)

    3. Which is important, your code will have problems encrypting some text because there is a possibility the new char (Temp) will exceed 255 (and in decryption, be negative). This will cause an error and won't encrypt the whole text:

    Temp = Asc(Mid(Text, i, 1)) + Asc(Mid(Password, i, 1))

    If the sum of those is greater than 255 then you will have problems. Or in decrypt, if they are less than 0 then you will get errors.

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Encryption & Decryption

    And use vbNullString instead of ""

  6. #6

    Thread Starter
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    Re: Encryption & Decryption

    Thanks for the advice.
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


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

  8. #8
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Encryption & Decryption

    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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