my project is file transfer ,
what I need is a way to encrypt and decrypt the data with a key , i did try to use modRC4.bas but the the sent file is damaged !
can you help me please ?
Printable View
my project is file transfer ,
what I need is a way to encrypt and decrypt the data with a key , i did try to use modRC4.bas but the the sent file is damaged !
can you help me please ?
Kinda hard to help you without seeing:
1. Your code
2. The encryption code
3. What parts of the file you are encrypting and if you're decrypting those same parts
4. How you're transferring your file
etc...
Here is a basic encryption/decryption algorithm that i created that works with string variables.
Read a chunk of the file at a time and pass it through EncryptString before sending. Then on receiving end use DecryptString on received data before writing to file.
Code:Option Explicit
Private Const EncKey As String = "GST5DKHGV7VH4RK&^kmG8&%87TYKhg"
Public Function EncryptString(sData As String) As String
'Encryption
Dim keypos As Integer, i As Integer, buf As Integer
keypos = 1
If Len(sData) > 0 Then
'loop for every character
For i = 1 To Len(sData)
'perform ascii addition
buf = Asc(Mid(sData, i, 1)) + Asc(Mid(EncKey, keypos, 1))
'correct buffer if it goes over ascii limit
If buf > 255 Then
buf = buf - 256
End If
'Add to return parameter
EncryptString = EncryptString & Chr$(buf)
'advance keypos
If keypos = Len(EncKey) Then
keypos = 1
Else
keypos = keypos + 1
End If
Next i
End If
End Function
Public Function DecryptString(sData As String) As String
'Decryption
Dim keypos As Integer, i As Integer, buf As Integer
keypos = 1
If Len(sData) > 0 Then
'loop for every character
For i = 1 To Len(sData)
'perform ascii subtraction
buf = Asc(Mid(sData, i, 1)) - Asc(Mid(EncKey, keypos, 1))
'correct buffer if it goes over ascii limit
If buf < 0 Then
buf = 256 - Abs(buf)
End If
'Add to return parameter
DecryptString = DecryptString & Chr$(buf)
'advance keypos
If keypos = Len(EncKey) Then
keypos = 1
Else
keypos = keypos + 1
End If
Next i
End If
End Function
One should keep in mind that VB String variables should not be used for binary data. There are many places where the text of a String is converted from internal Unicode to ANSI/ASCII and back. Not all of these conversions are symmetric for all possible character values, some of which binary data may tend to run into more often than plain text.
It depends on what sort of file is being sent. If the file is plaintext without special characters then strings are ok, however if your sending files like videos or music then you'll need to use a byte array.
The algorithm can easily be altered to work with byte arrays instead of strings
I will try yours Lozzenger ...
thanx Lozzenger alot , it's working very well with all types of files :)
but it increases size of data alot :(
I don't see why it would increase the data size when it returns a string that's the same length as the one you passed in the function
Also be aware that if you're transferring the files by FTP, you have to specify binary transfer. ASCII transfer can modify a file.
Quote:
Originally Posted by Lozzenger
how can I modify it to work with byte arrays instead of strings?
thnxQuote:
Originally Posted by dBlues
Is this still a question?
yeah, I still can't modify it to work with byte arrays instead of stringsQuote:
Originally Posted by Hack