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!
Last edited by Vanasha; Sep 13th, 2007 at 10:58 AM.
Reason: Code update
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."
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.
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."