-
Nov 3rd, 2007, 08:31 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] What is the fastest and simplest file encryption?
I am looking for the fastest file encryption? Does anyone know how to take the content of a binary file - reverse and save it?
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 3rd, 2007, 08:53 PM
#2
Frenzied Member
Re: What is the fastest and simplest file encryption?
vb Code:
Dim f As long
Dim d() As Byte
Dim i As Long
Dim filein As String
Dim fileout As String
f = FreeFile
Open filein For Binary As #f
ReDim d(LOF(f) - 1)
Get #f, , d
Close #f
f = FreeFile
Open fileout For Binary As #f
For i = UBound(d) To 0 Step -1
Put #f, , d(i)
Next i
Close #f
Erase d
-
Nov 3rd, 2007, 09:36 PM
#3
Re: What is the fastest and simplest file encryption?
You would do better reading 256kb (or another number) blocks of the file from the end backwards and writing a new file. It would be faster and of course you would do the same to revert it to normal. I would recommend that you put a header on the file so that you know something about what you did to the file. In that way you could tell if it was already reversed...
-
Nov 3rd, 2007, 11:32 PM
#4
Re: What is the fastest and simplest file encryption?
as long as the file is not TOO large, you could load the entire thing into a byte array and XOR every byte by a certain value. Xoring it by the same value later would decrypt it. However setting file encryption to true would prevent anyone except an administrator on the computer you created it on from reading the file.
-
Nov 4th, 2007, 12:07 AM
#5
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Thanks jeroen79. Can that handle any file size?
Originally Posted by randem
You would do better reading 256kb (or another number) blocks of the file from the end backwards and writing a new file. It would be faster and of course you would do the same to revert it to normal.
Can you provide a sample like jeroen79 or provide a link that shows a sample?
Originally Posted by randem
I would recommend that you put a header on the file so that you know something about what you did to the file. In that way you could tell if it was already reversed...
I plan to do this because otherwise, I might end up reversing twice.
Originally Posted by Lord Orwell
as long as the file is not TOO large, you could load the entire thing into a byte array and XOR every byte by a certain value. Xoring it by the same value later would decrypt it.
Can you provide a sample like jeroen79 or provide a link that shows a sample?
Originally Posted by Lord Orwell
However setting file encryption to true would prevent anyone except an administrator on the computer you created it on from reading the file.
I don't understand? Please elaborate.
Thank You so far.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 4th, 2007, 03:52 AM
#6
Re: What is the fastest and simplest file encryption?
Here ya go
Code:
Option Explicit
Private Sub Command1_Click()
Dim FNumIn As Integer
Dim FNumOut As Integer
Dim i As Long
Dim sFilenameIn As String
Dim sFilenameOut As String
Dim FileBlocks As Long
Dim bBuf() As Byte
Dim lOffsetIn As Long
Const bufsze = 256
Dim Quit As Boolean
Dim Remainder As Long
sFilenameIn = App.Path & "\DataIn.dat"
sFilenameOut = App.Path & "\DataOut.dat"
FNumIn = FreeFile
Open sFilenameIn For Binary Access Read Shared As #FNumIn
lOffsetIn = LOF(FNumIn) + 1
FNumOut = FreeFile
Open sFilenameOut For Binary Access Write As #FNumOut
ReDim bBuf(bufsze)
Do While Not Quit
lOffsetIn = lOffsetIn - bufsze
If lOffsetIn < 0 Then
Remainder = bufsze - lOffsetIn
lOffsetIn = 1
Quit = True
ReDim bBuf(Remainder)
End If
' Seek FNumIn, lOffsetIn
Get FNumIn, lOffsetIn, bBuf
Put FNumOut, , bBuf
Loop
Close #FNumIn
Close #FNumOut
End Sub
-
Nov 4th, 2007, 04:38 AM
#7
Addicted Member
Re: What is the fastest and simplest file encryption?
Here you can use this, I made this awhile back for my LAN Messenger I made, to encrypt and decrypt the packets sent to and from the client and server. I have used it for serveral other things as well, such as file encryption.
vb Code:
Public Function Encrypt(ByVal Plain As String, _
sEncKey As String) As String
Dim LenLetter As Integer, itempnum As Integer, i As Integer
Dim Letter As String, KeyNum As String, encstr As String, temp As String, _
temp2 As String, itempstr As String, encrypted2 As String
Dim Math As Long
On Error GoTo oops
If sEncKey = "" Then sEncKey = "Jaquio"
'Sets the Encryption Key if one is not set
ReDim encKEY(1 To Len(sEncKey))
'starts the values for the Encryption Key
For i = 1 To Len(sEncKey$)
KeyNum = Mid$(sEncKey$, i, 1) 'gets the letter at index i
encKEY(i) = Asc(KeyNum) 'sets the the Array value
'to ASC number for the letter
'This is the first letter so just hold the value
If i = 1 Then Math = encKEY(i): GoTo nextone
'compares the value to the previous value and then
'either adds/subtracts the value to the Math total
If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) <= _
encKEY(i - 1) Then Math = Math - encKEY(i)
If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) <= _
encKEY(i - 1) Then Math = Math - encKEY(i)
If i >= 2 And encKEY(i) >= Math And encKEY(i) >= _
encKEY(i - 1) Then Math = Math + encKEY(i)
If i >= 2 And encKEY(i) < Math And encKEY(i) _
>= encKEY(i - 1) Then Math = Math + encKEY(i)
nextone:
Next i
For i = 1 To Len(Plain) 'Now for the String to be encrypted
Letter = Mid$(Plain, i, 1) 'sets Letter to
'the letter at index i
LenLetter = Asc(Letter) + Math 'Now it adds the Asc
'value of Letter to Math
'checks and corrects the format then adds a space to separate them frm each other
If LenLetter >= 100 Then encstr = _
encstr & Asc(Letter) + Math & " "
'checks and corrects the format then adds a space
'to separate them frm each other
If LenLetter <= 99 Then encstr$ = encstr & "0" & _
Asc(Letter) + Math & " "
Next i
'This is part of what i'm doing to convert the encrypted
'numbers to Letters so it sort of encrypts the
'encrypted message.
temp$ = encstr 'hold the encrypted data
temp$ = TrimSpaces(temp) 'get rid of the spaces
itempnum% = Mid(temp, 1, 2) 'grab the first 2 numbers
temp2$ = Chr(itempnum% + 100) 'Now add 100 so it
'will be a valid char
'If its a 2 digit number hold it and continue
If Len(itempnum%) >= 2 Then itempstr$ = Str(itempnum%)
'If the number is a single digit then add a '0' to the front
'then hold it
If Len(itempnum%) = 1 Then itempstr$ = "0" & _
TrimSpaces(Str(itempnum%))
encrypted2$ = temp2 'set the encrypted message
For i = 3 To Len(temp) Step 2
itempnum% = Mid(temp, i, 2) 'grab the next 2 numbers
' add 100 so it will be a valid char
temp2$ = Chr(itempnum% + 100)
'if its the last number we only want to hold it we
'don't want to add a '0' even if its a single digit
If i = Len(temp) Then itempstr$ = _
Str(itempnum%): GoTo itsdone
'If its a 2 digit number hold it and continue
If Len(itempnum%) = 2 Then itempstr$ = _
Str(itempnum%)
'If the number is a single digit then add a '0'
'to the front then hold it
If Len(TrimSpaces(Str(itempnum))) = 1 Then _
itempstr$ = "0" & TrimSpaces(Str(itempnum%))
'Now check to see if a - number was created
'if so cause an error message
If Left(TrimSpaces(Str(itempnum)), 1) = "-" Then _
Err.Raise 20000, , "Unexpected Error"
itsdone:
'Set The Encrypted message
encrypted2$ = encrypted2 & temp2$
Next i
'Encrypt = encstr 'Returns the First Encrypted String
Encrypt = encrypted2 'Returns the Second Encrypted String
Exit Function 'We are outta Here
oops:
Debug.Print "Error description", Err.Description
Debug.Print "Error source:", Err.Source
Debug.Print "Error Number:", Err.Number
End Function
Public Function Decrypt(ByVal Encrypted As String, _
sEncKey As String) As String
Dim NewEncrypted As String, Letter As String, KeyNum As String, EncNum As String
Dim encbuffer As Long, Math As Long
Dim strDecrypted As String, Kdecrypt As String, lastTemp As String
Dim LenTemp As Integer, itempnum As Integer, i As Integer
Dim temp As String, temp2 As String, itempstr As String
On Error GoTo oops
If sEncKey = "" Then sEncKey = "Jaquio"
ReDim encKEY(1 To Len(sEncKey))
'Convert The Key For Decryption
For i = 1 To Len(sEncKey$)
KeyNum = Mid$(sEncKey$, i, 1) 'Get Letter i% in the Key
encKEY(i) = Asc(KeyNum) 'Convert Letter i to Asc value
'if it the first letter just hold it
If i = 1 Then Math = encKEY(i): GoTo nextone
If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) _
<= encKEY(i - 1) Then Math = Math - encKEY(i)
'compares the value to the previous value and
'then either adds/subtracts the value to the
'Math total
If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) _
<= encKEY(i - 1) Then Math = Math - encKEY(i)
If i >= 2 And encKEY(i) >= Math And encKEY(i) _
>= encKEY(i - 1) Then Math = Math + encKEY(i)
If i >= 2 And encKEY(i) < Math And encKEY(i) _
>= encKEY(i - 1) Then Math = Math + encKEY(i)
nextone:
Next i
'This is part of what i'm doing to convert the encrypted
'numbers to Letters so it sort of encrypts the encrypted
'message.
temp$ = Encrypted 'hold the encrypted data
For i = 1 To Len(temp)
itempstr = TrimSpaces(Str(Asc(Mid(temp, i, 1)) - _
100)) 'grab the next 2 numbers
'If its a 2 digit number hold it and continue
If Len(itempstr$) = 2 Then itempstr$ = itempstr$
If i = Len(temp) - 2 Then LenTemp% = _
Len(Mid(temp2, Len(temp2) - 3))
If i = Len(temp) Then itempstr$ = _
TrimSpaces(itempstr$): GoTo itsdone
'If the number is a single digit then add a '0' to the
'front then hold it
If Len(TrimSpaces(itempstr$)) = 1 Then _
itempstr$ = "0" & TrimSpaces(itempstr$)
'Now check to see if a - number was created if so
'cause an error message
If Left(TrimSpaces(itempstr$), 1) = "-" Then _
Err.Raise 20000, , "Unexpected Error"
itsdone:
temp2$ = temp2$ & itempstr 'hold the first decryption
Next i
Encrypted = TrimSpaces(temp2$) 'set the encrypted data
For i = 1 To Len(Encrypted) Step 3
'Format the encrypted string for the second decryption
NewEncrypted = NewEncrypted & _
Mid(Encrypted, CLng(i), 3) & " "
Next i
' Hold the last set of numbers to check it its the correct format
lastTemp$ = TrimSpaces(Mid(NewEncrypted, _
Len(NewEncrypted$) - 3))
If Len(lastTemp$) = 2 Then
' If it = 2 then its not the Correct format and we need to fix it
lastTemp$ = Mid(NewEncrypted, _
Len(NewEncrypted$) - 1) 'Holds Last Number so a '0'
'Can be added between them
'set it to the new format
Encrypted = Mid(NewEncrypted, 1, _
Len(NewEncrypted) - 2) & "0" & lastTemp$
Else
Encrypted$ = NewEncrypted$ 'set the new format
End If
'The Actual Decryption
For i = 1 To Len(Encrypted)
Letter = Mid$(Encrypted, i, 1) 'Hold Letter at index i
EncNum = EncNum & Letter 'Hold the letters
If Letter = " " Then 'we have a letter to decrypt
encbuffer = CLng(Mid(EncNum, 1, _
Len(EncNum) - 1)) 'Convert it to long and
'get the number minus the " "
strDecrypted$ = strDecrypted & Chr(encbuffer - _
Math) 'Store the decrypted string
EncNum = "" 'clear if it is a space so we can get
'the next set of numbers
End If
Next i
Decrypt = strDecrypted
Exit Function
oops:
Debug.Print "Error description", Err.Description
Debug.Print "Error source:", Err.Source
Debug.Print "Error Number:", Err.Number
Err.Raise 20001, , "You have entered the wrong encryption string"
End Function
Private Function TrimSpaces(strString As String) As String
Dim lngpos As Long
Do While InStr(1&, strString$, " ")
DoEvents
lngpos& = InStr(1&, strString$, " ")
strString$ = Left$(strString$, (lngpos& - 1&)) & _
Right$(strString$, Len(strString$) - _
(lngpos& + Len(" ") - 1&))
Loop
TrimSpaces$ = strString$
End Function
It is a fairly weak encryption method but it works for me and that is all I really needed.
-
Nov 4th, 2007, 12:17 PM
#8
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Thanks for doing the follow-up randem.
Thanks Jaquio, I will check it out.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 4th, 2007, 08:38 PM
#9
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Jaquio,
I tried to copy you code over to a vb module but it was all jumbled around. Not sure what happen but it was not like copying randem's code. Can you see if you can change the tag to [code]? Also, if you can explain the functions and sub procedures, that would be great.
Thank You
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 4th, 2007, 08:42 PM
#10
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
BTW... randem, can your code handle any file size and what is the best way to put a header into the file? I am thinking of using a string. Thanks
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 4th, 2007, 08:51 PM
#11
Re: What is the fastest and simplest file encryption?
Yes, it can handle any size file and you would write out the header immediately before writing out the first block of the output file.
-
Nov 4th, 2007, 08:52 PM
#12
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Thanks for your reply.
So, I would write the header as a string write? Also, somehow, the code that you provided is not producing an output file.
BTW... I would write out the header like this right?
Code:
Open sFilenameOut For Binary Access Write As #FNumOut
Print #FNumOut, "Header"
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 4th, 2007, 08:56 PM
#13
Re: What is the fastest and simplest file encryption?
No, You would set up a predefined type definition (fixed size) that consisted of the data you want to have in the header.
The code I gave is tested and does output a file... You do realize that if you don't supply an input file there can be no output????
-
Nov 4th, 2007, 09:01 PM
#14
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Originally Posted by randem
No, You would set up a predefined type definition (fixed size) that consisted of the data you want to have in the header.
Hate to ask you to do this, but would you be kind enough to supply the example to the above?
Originally Posted by randem
The code I gave is tested and does output a file... You do realize that if you don't supply an input file there can be no output????
This is strange. I did supply an input file. I will test it again.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 4th, 2007, 09:02 PM
#15
Re: What is the fastest and simplest file encryption?
-
Nov 4th, 2007, 09:04 PM
#16
Re: What is the fastest and simplest file encryption?
BTW: You do have to do some of the work...
-
Nov 4th, 2007, 09:19 PM
#17
Addicted Member
Re: What is the fastest and simplest file encryption?
Here it is in Code tags
Code:
Public Function Encrypt(ByVal Plain As String, _
sEncKey As String) As String
Dim LenLetter As Integer, itempnum As Integer, i As Integer
Dim Letter As String, KeyNum As String, encstr As String, temp As String, _
temp2 As String, itempstr As String, encrypted2 As String
Dim Math As Long
On Error GoTo oops
If sEncKey = "" Then sEncKey = "Jaquio"
'Sets the Encryption Key if one is not set
ReDim encKEY(1 To Len(sEncKey))
'starts the values for the Encryption Key
For i = 1 To Len(sEncKey$)
KeyNum = Mid$(sEncKey$, i, 1) 'gets the letter at index i
encKEY(i) = Asc(KeyNum) 'sets the the Array value
'to ASC number for the letter
'This is the first letter so just hold the value
If i = 1 Then Math = encKEY(i): GoTo nextone
'compares the value to the previous value and then
'either adds/subtracts the value to the Math total
If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) <= _
encKEY(i - 1) Then Math = Math - encKEY(i)
If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) <= _
encKEY(i - 1) Then Math = Math - encKEY(i)
If i >= 2 And encKEY(i) >= Math And encKEY(i) >= _
encKEY(i - 1) Then Math = Math + encKEY(i)
If i >= 2 And encKEY(i) < Math And encKEY(i) _
>= encKEY(i - 1) Then Math = Math + encKEY(i)
nextone:
Next i
For i = 1 To Len(Plain) 'Now for the String to be encrypted
Letter = Mid$(Plain, i, 1) 'sets Letter to
'the letter at index i
LenLetter = Asc(Letter) + Math 'Now it adds the Asc
'value of Letter to Math
'checks and corrects the format then adds a space to separate them frm each other
If LenLetter >= 100 Then encstr = _
encstr & Asc(Letter) + Math & " "
'checks and corrects the format then adds a space
'to separate them frm each other
If LenLetter <= 99 Then encstr$ = encstr & "0" & _
Asc(Letter) + Math & " "
Next i
'This is part of what i'm doing to convert the encrypted
'numbers to Letters so it sort of encrypts the
'encrypted message.
temp$ = encstr 'hold the encrypted data
temp$ = TrimSpaces(temp) 'get rid of the spaces
itempnum% = Mid(temp, 1, 2) 'grab the first 2 numbers
temp2$ = Chr(itempnum% + 100) 'Now add 100 so it
'will be a valid char
'If its a 2 digit number hold it and continue
If Len(itempnum%) >= 2 Then itempstr$ = Str(itempnum%)
'If the number is a single digit then add a '0' to the front
'then hold it
If Len(itempnum%) = 1 Then itempstr$ = "0" & _
TrimSpaces(Str(itempnum%))
encrypted2$ = temp2 'set the encrypted message
For i = 3 To Len(temp) Step 2
itempnum% = Mid(temp, i, 2) 'grab the next 2 numbers
' add 100 so it will be a valid char
temp2$ = Chr(itempnum% + 100)
'if its the last number we only want to hold it we
'don't want to add a '0' even if its a single digit
If i = Len(temp) Then itempstr$ = _
Str(itempnum%): GoTo itsdone
'If its a 2 digit number hold it and continue
If Len(itempnum%) = 2 Then itempstr$ = _
Str(itempnum%)
'If the number is a single digit then add a '0'
'to the front then hold it
If Len(TrimSpaces(Str(itempnum))) = 1 Then _
itempstr$ = "0" & TrimSpaces(Str(itempnum%))
'Now check to see if a - number was created
'if so cause an error message
If Left(TrimSpaces(Str(itempnum)), 1) = "-" Then _
Err.Raise 20000, , "Unexpected Error"
itsdone:
'Set The Encrypted message
encrypted2$ = encrypted2 & temp2$
Next i
'Encrypt = encstr 'Returns the First Encrypted String
Encrypt = encrypted2 'Returns the Second Encrypted String
Exit Function 'We are outta Here
oops:
Debug.Print "Error description", Err.Description
Debug.Print "Error source:", Err.Source
Debug.Print "Error Number:", Err.Number
End Function
Public Function Decrypt(ByVal Encrypted As String, _
sEncKey As String) As String
Dim NewEncrypted As String, Letter As String, KeyNum As String, EncNum As String
Dim encbuffer As Long, Math As Long
Dim strDecrypted As String, Kdecrypt As String, lastTemp As String
Dim LenTemp As Integer, itempnum As Integer, i As Integer
Dim temp As String, temp2 As String, itempstr As String
On Error GoTo oops
If sEncKey = "" Then sEncKey = "Jaquio"
ReDim encKEY(1 To Len(sEncKey))
'Convert The Key For Decryption
For i = 1 To Len(sEncKey$)
KeyNum = Mid$(sEncKey$, i, 1) 'Get Letter i% in the Key
encKEY(i) = Asc(KeyNum) 'Convert Letter i to Asc value
'if it the first letter just hold it
If i = 1 Then Math = encKEY(i): GoTo nextone
If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) _
<= encKEY(i - 1) Then Math = Math - encKEY(i)
'compares the value to the previous value and
'then either adds/subtracts the value to the
'Math total
If i >= 2 And Math - encKEY(i) >= 0 And encKEY(i) _
<= encKEY(i - 1) Then Math = Math - encKEY(i)
If i >= 2 And encKEY(i) >= Math And encKEY(i) _
>= encKEY(i - 1) Then Math = Math + encKEY(i)
If i >= 2 And encKEY(i) < Math And encKEY(i) _
>= encKEY(i - 1) Then Math = Math + encKEY(i)
nextone:
Next i
'This is part of what i'm doing to convert the encrypted
'numbers to Letters so it sort of encrypts the encrypted
'message.
temp$ = Encrypted 'hold the encrypted data
For i = 1 To Len(temp)
itempstr = TrimSpaces(Str(Asc(Mid(temp, i, 1)) - _
100)) 'grab the next 2 numbers
'If its a 2 digit number hold it and continue
If Len(itempstr$) = 2 Then itempstr$ = itempstr$
If i = Len(temp) - 2 Then LenTemp% = _
Len(Mid(temp2, Len(temp2) - 3))
If i = Len(temp) Then itempstr$ = _
TrimSpaces(itempstr$): GoTo itsdone
'If the number is a single digit then add a '0' to the
'front then hold it
If Len(TrimSpaces(itempstr$)) = 1 Then _
itempstr$ = "0" & TrimSpaces(itempstr$)
'Now check to see if a - number was created if so
'cause an error message
If Left(TrimSpaces(itempstr$), 1) = "-" Then _
Err.Raise 20000, , "Unexpected Error"
itsdone:
temp2$ = temp2$ & itempstr 'hold the first decryption
Next i
Encrypted = TrimSpaces(temp2$) 'set the encrypted data
For i = 1 To Len(Encrypted) Step 3
'Format the encrypted string for the second decryption
NewEncrypted = NewEncrypted & _
Mid(Encrypted, CLng(i), 3) & " "
Next i
' Hold the last set of numbers to check it its the correct format
lastTemp$ = TrimSpaces(Mid(NewEncrypted, _
Len(NewEncrypted$) - 3))
If Len(lastTemp$) = 2 Then
' If it = 2 then its not the Correct format and we need to fix it
lastTemp$ = Mid(NewEncrypted, _
Len(NewEncrypted$) - 1) 'Holds Last Number so a '0'
'Can be added between them
'set it to the new format
Encrypted = Mid(NewEncrypted, 1, _
Len(NewEncrypted) - 2) & "0" & lastTemp$
Else
Encrypted$ = NewEncrypted$ 'set the new format
End If
'The Actual Decryption
For i = 1 To Len(Encrypted)
Letter = Mid$(Encrypted, i, 1) 'Hold Letter at index i
EncNum = EncNum & Letter 'Hold the letters
If Letter = " " Then 'we have a letter to decrypt
encbuffer = CLng(Mid(EncNum, 1, _
Len(EncNum) - 1)) 'Convert it to long and
'get the number minus the " "
strDecrypted$ = strDecrypted & Chr(encbuffer - _
Math) 'Store the decrypted string
EncNum = "" 'clear if it is a space so we can get
'the next set of numbers
End If
Next i
Decrypt = strDecrypted
Exit Function
oops:
Debug.Print "Error description", Err.Description
Debug.Print "Error source:", Err.Source
Debug.Print "Error Number:", Err.Number
Err.Raise 20001, , "You have entered the wrong encryption string"
End Function
Private Function TrimSpaces(strString As String) As String
Dim lngpos As Long
Do While InStr(1&, strString$, " ")
DoEvents
lngpos& = InStr(1&, strString$, " ")
strString$ = Left$(strString$, (lngpos& - 1&)) & _
Right$(strString$, Len(strString$) - _
(lngpos& + Len(" ") - 1&))
Loop
TrimSpaces$ = strString$
End Function
Hopefully it copys okay, there isn't much to explain that isn't already it is commented so hopefully it does the job if you don't understand anything about it just ask.
To Encrypt:
Encrypt("Data to be encrypted","An encryption key of your choice(can be anything)")
To Decrypt:
Decrypt("Data to be decrypted","Your encryption key you chose.")
Hopefully it works okay for you. If you need it used in an example just let me know.
-
Nov 4th, 2007, 11:51 PM
#18
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Originally Posted by randem
BTW: You do have to do some of the work...
Not that I don't want to do the work, just don't understand how to get to the destination.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 5th, 2007, 01:17 AM
#19
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
OK, this is what I have so far.
Code:
Option Explicit
Private Type FileHeader
intBufferSize As Integer
lngRemainder As Long
End Type
Private Sub Main()
Call ReverseIt
Call UnReverseIt
End Sub
Private Sub ReverseIt()
Dim FNumIn As Integer
Dim FNumOut As Integer
Dim sFilenameIn As String
Dim sFilenameOut As String
Dim bBuf() As Byte
Dim lOffsetIn As Long
Const bufsze As Integer = 256
Dim Quit As Boolean
Dim Remainder As Long
Dim HDR As FileHeader
HDR.intBufferSize = bufsze
'HDR.lngRemainder = Remainder
sFilenameIn = App.Path & "\Test.vbs"
sFilenameOut = Left(sFilenameIn, InStrRev(sFilenameIn, ".")) & "txt"
If Dir(sFilenameOut) <> "" Then Kill sFilenameOut
'Open the input file as binary
FNumIn = FreeFile
Open sFilenameIn For Binary Access Read Shared As #FNumIn
lOffsetIn = LOF(FNumIn) + 1 'Get the total length of the file
'Open the output file as binary
FNumOut = FreeFile
Open sFilenameOut For Binary Access Write As #FNumOut
' Put FNumOut, , HDR
ReDim bBuf(bufsze)
Do While Not Quit
'Subtract the remaining total length from the buffer size to get the last positioning
lOffsetIn = lOffsetIn - bufsze
'Get the remaining
If lOffsetIn < 0 Then
Remainder = bufsze - lOffsetIn
lOffsetIn = 1
Quit = True
ReDim bBuf(Remainder)
End If
' Seek FNumIn, lOffsetIn
'Get the binary from the position
Get FNumIn, lOffsetIn, bBuf
'Output it backout with the end of the input file
Put FNumOut, , bBuf
Loop
Close #FNumIn
Close #FNumOut
End Sub
Private Sub UnReverseIt()
Dim FNumIn As Integer
Dim FNumOut As Integer
Dim sFilenameIn As String
Dim sFilenameOut As String
Dim bBuf() As Byte
Dim lOffsetIn As Long
Const bufsze As Integer = 256
Dim Quit As Boolean
Dim Remainder As Long
Dim HDR As FileHeader
HDR.intBufferSize = bufsze
'HDR.lngRemainder = Remainder
sFilenameIn = App.Path & "\Test.txt"
sFilenameOut = App.Path & "\Final.txt"
If Dir(sFilenameOut) <> "" Then Kill sFilenameOut
'Open the input file as binary
FNumIn = FreeFile
Open sFilenameIn For Binary Access Read Shared As #FNumIn
lOffsetIn = LOF(FNumIn) + 1 'Get the total length of the file
'Open the output file as binary
FNumOut = FreeFile
Open sFilenameOut For Binary Access Write As #FNumOut
' Put FNumOut, , HDR
ReDim bBuf(bufsze)
Do While Not Quit
'Subtract the remaining total length from the buffer size to get the last positioning
lOffsetIn = lOffsetIn - bufsze
'Get the remaining
If lOffsetIn < 0 Then
Remainder = bufsze - lOffsetIn
lOffsetIn = 1
Quit = True
ReDim bBuf(Remainder)
End If
' Seek FNumIn, lOffsetIn
'Get the binary from the position
Get FNumIn, lOffsetIn, bBuf
'Output it backout with the end of the input file
Put FNumOut, , bBuf
Loop
Close #FNumIn
Close #FNumOut
End Sub
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 5th, 2007, 06:31 AM
#20
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
I hope you don't mind me changing your code around randem. I was having trouble but once I subtracted a 1 during the redim from the array, it worked fine. I also took off the +1 for the filesize. Anyway, if you can give me an example of a header to keep the remainder and the buffer size, I would appreciate it.
Thanks
Code:
Private Sub Main()
Call EncryptMe(App.Path & "\Test01.txt", App.Path & "\Test02.txt")
Call EncryptMe(App.Path & "\Test02.txt", App.Path & "\Test03.txt")
End Sub
Private Sub EncryptMe(strSource As String, _
strTarget As String, _
Optional intBufferSize As Integer = 2)
If Dir(strTarget) <> "" Then Kill strTarget
Dim intFreeFileIn As Integer
intFreeFileIn = FreeFile
Open strSource For Binary Access Read Shared As #intFreeFileIn
Dim lngFileSize As Long
lngFileSize = LOF(intFreeFileIn)
Dim intFreeFileOut As Integer
intFreeFileOut = FreeFile
Open strTarget For Binary Access Write As #intFreeFileOut
Dim byteArr() As Byte
ReDim byteArr(intBufferSize - 1) 'Subtract 1 because of 0 base array
Dim lngPos As Long
lngPos = lngFileSize
Do
lngPos = lngPos - intBufferSize
If lngPos <= 0 Then Exit Do
Get #intFreeFileIn, lngPos + 1, byteArr
Put #intFreeFileOut, , byteArr
Loop
ReDim byteArr((intBufferSize + lngPos) - 1)
Get #intFreeFileIn, 1, byteArr
Put #intFreeFileOut, , byteArr
Close #intFreeFileIn
Close #intFreeFileOut
Erase byteArr
End Sub
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 5th, 2007, 09:26 PM
#21
Re: What is the fastest and simplest file encryption?
No, I don't mind at all. That is what you are supposed to do to make the code usable for your situation. I however don't understand what you mean about the header. You have the header defined. All you need to do is to write the header out then continue to write the file out after that. When you get to the end and have all your information for the header you back up to the beginning of the file and rewrite the header that will contain all the information about the file.
You do need to store the block size for the file in your header also.
Ex.
Code:
Private Type FileHeader
BlockSize As Long
Remainder As Long
VerificationInfo(30) As String ' Must use fixed sizes for your header
End Type
When you reverse it you read the header info. check to see if it is a valid header then use the information in the header to read the file to reverse it.
Last edited by randem; Nov 5th, 2007 at 09:32 PM.
-
Nov 5th, 2007, 10:03 PM
#22
Re: What is the fastest and simplest file encryption?
Can you provide a sample like jeroen79 or provide a link that shows a sample?
i don't program in vb6 any more, but based on memory, this code will probably work, but it is untested.
Code:
dim a$, workchar$, filelen&, workbyte as byte, Password as byte, cl as long
password = 15 'set this to a value between 1 and 255
'obviously, zero is allowed but it won't change anything
open "filetoencrypt" for binary as #1
FileLen = len(1)
A$ = space(filelen) 'make a string the length of the file
get #1, 1, a$ 'read entire file into a$
for cl = 1 to len(a$) 'set up a loop the length of the file
WorkChar = mid(a$, cl, 1) 'set workchar to a character from file
WorkByte = asc(workchar) 'convert it to a byte
workbyte = workbyte XOR Password 'encrypt the byte
workchar = chr(workbyte) 'covert it back to a string
mid(a$, cl, 1) = workchar 'update the file in memory, one char at a time
next cl
put #1, 1, a$ 'rewrite file
close #1
i would test this on a file copy before using it on anything important. You could also (assuming i didn't screw this up any) write to a 2nd file instead of overwriting the original.
I don't understand? Please elaborate.
if you have your folder's encryption enabled, then any other user won't be able to view the contents of the folder. And if they try to copy your files to another computer and view them, even administrators won't be able to because they will not have the correct certificate assigned to any of the users. Encryption is based on an unique string generated when an OS is installed which means if you reinstall your OS without backing up your certificates the files will be lost totally!. Hope that's clear.
Thank You so far.[/QUOTE]
-
Nov 6th, 2007, 12:29 PM
#23
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Originally Posted by randem
You have the header defined. All you need to do is to write the header out then continue to write the file out after that. When you get to the end and have all your information for the header you back up to the beginning of the file and rewrite the header that will contain all the information about the file.
You do need to store the block size for the file in your header also.
When you reverse it you read the header info. check to see if it is a valid header then use the information in the header to read the file to reverse it.
I know how to write the header out. However, the concept is what I am still trying to understand. I don’t have all of the file info until I cycle to the end. How do I write it out there?
Also, I am a bit confused how to read the header to get info and does this change the LOF of the file?
Thanks Lord Orwell. I am going to look at your stuff right after I get this working.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 6th, 2007, 12:47 PM
#24
Re: What is the fastest and simplest file encryption?
adding a header to the file will make the output file larger, but won't change the length of the original. However you must make sure you know how long you made the header because you will need to strip it off when you switch it back. It wouldn't actually harm anything to leave it on there, but your program would add a new one every time you re-encrypted it and the file would get bigger every time.
-
Nov 6th, 2007, 12:58 PM
#25
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Any samples to help me get started Lord?
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 6th, 2007, 02:52 PM
#26
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
OK, so far, I got this much. I know how to write out and read in. I do have a question on how does the read in process knows the length of the header to read in. Also, after I read the header in, how do I read the rest of the data in?
Code:
Option Explicit
Private Type tHeader
MaxRecord As Long
End Type
Private Sub Main()
Call Write(ActiveWorkbook.Path & "\Test.txt")
Call Read(ActiveWorkbook.Path & "\Test.txt")
End Sub
Private Function Write(strFilename As String) As Long
Dim udtHeader As tHeader
udtHeader.MaxRecord = 5555
Dim intFreeFile As Integer
intFreeFile = FreeFile
Open strFilename For Output As #intFreeFile
Close #intFreeFile ' Close the file before opening in Binary mode
intFreeFile = FreeFile
Open strFilename For Binary Lock Read Write As #intFreeFile
Put #intFreeFile, 1, udtHeader
Put #intFreeFile, , "Test Data"
Close #intFreeFile
End Function
Private Function Read(strFilename As String) As Long
Dim udtHeader As tHeader
Dim intFreeFile As Integer
intFreeFile = FreeFile
Open strFilename For Binary Lock Read Write As #intFreeFile
Get #intFreeFile, 1, udtHeader
MsgBox udtHeader.MaxRecord
Close #intFreeFile
End Function
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 6th, 2007, 02:56 PM
#27
Re: What is the fastest and simplest file encryption?
If you have a header you already know the length as per my example
Code:
Private Type FileHeader
BlockSize As Long
Remainder As Long
VerificationInfo(30) As String ' Must use fixed sizes for your header
End Type
That is the length...
-
Nov 6th, 2007, 03:32 PM
#28
Re: What is the fastest and simplest file encryption?
Example
Code:
Dim FH as FileHeader
Dim FHL as long
FHL = LenB(FH)
Now if you just set a buffer of size FHL then read into that buffer your next read will be the actual start of the file...
-
Nov 6th, 2007, 03:55 PM
#29
Re: What is the fastest and simplest file encryption?
Originally Posted by Liquid Metal
Does anyone know how to take the content of a binary file - reverse and save it?
Code:
Dim MyData As String
Private Sub Form_Load()
Open "OldFile" For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Open "NewFile" For Binary As #2
For I = Len(MyData) To 1 Step -1
Put #2, , Mid$(MyData, I, 1)
Next
Close
End Sub
This code builds a second file with the data in reverse order.
-
Nov 6th, 2007, 04:27 PM
#30
Re: What is the fastest and simplest file encryption?
yes it does but it also writes it one byte at a time and this is incredibly slow. It would take about a minute to switch around a megabyte. You should do the byte reversal in memory into a 2nd string before re-writing it.
Code:
string2 = string1 'sets the size of string 2
for cl = 1 to len(string1)
mid(string2, cl, 1) = mid(string1, len(string1) - cl + 1, 1)
next cl
Last edited by Lord Orwell; Nov 6th, 2007 at 04:35 PM.
-
Nov 6th, 2007, 05:02 PM
#31
Re: What is the fastest and simplest file encryption?
Originally Posted by Lord Orwell
yes it does but it also writes it one byte at a time and this is incredibly slow. It would take about a minute to switch around a megabyte. You should do the byte reversal in memory into a 2nd string before re-writing it.
OK, I see what you mean. That takes more string space, but what the heck, memory is cheap these days. Here's my adjusted code for big files:
Code:
Dim MyOldData As String, MyNewData As String
Private Sub Form_Load()
Open "OldFile" For Binary As #1
MyOldData = Space$(LOF(1))
Get #1, , MyOldData
For I = Len(MyOldData) To 1 Step -1
MyNewData = MyNewData + Mid$(MyOldData, I, 1)
Next
Open "NewFile" For Binary As #2
Put #2, , MyNewData
Close
End Sub
-
Nov 6th, 2007, 05:13 PM
#32
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Originally Posted by randem
If you have a header you already know the length as per my example
Code:
Private Type FileHeader
BlockSize As Long
Remainder As Long
VerificationInfo(30) As String ' Must use fixed sizes for your header
End Type
That is the length...
I did some research and I thought they recommend not to use the fix size data type when working with binary files. You do make sense because if you don't declare the string a fix size, how does it know the size when reading back right?
Regardless, I am able to read back in the header but still having trouble reading the rest of the data.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 6th, 2007, 06:56 PM
#33
Re: What is the fastest and simplest file encryption?
Why are you having trouble. You have all the information from the header. After reading the header and getting the information. Redim the buffer to the blocksize from the header then read the file. The very next read will be the start of the file...
Last edited by randem; Nov 6th, 2007 at 07:01 PM.
-
Nov 6th, 2007, 07:15 PM
#34
Re: What is the fastest and simplest file encryption?
Originally Posted by Code Doc
OK, I see what you mean. That takes more string space, but what the heck, memory is cheap these days. Here's my adjusted code for big files:
Code:
Dim MyOldData As String, MyNewData As String
Private Sub Form_Load()
Open "OldFile" For Binary As #1
MyOldData = Space$(LOF(1))
Get #1, , MyOldData
For I = Len(MyOldData) To 1 Step -1
MyNewData = MyNewData + Mid$(MyOldData, I, 1)
Next
Open "NewFile" For Binary As #2
Put #2, , MyNewData
Close
End Sub
not exactly. If you append a string over and over like that, you are redimensioning the memory area every time you append one character to it. That was why the code sample i posted set up a 2nd string of the same length first. When speed is a concern, every little bit helps.
-
Nov 6th, 2007, 08:31 PM
#35
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
Originally Posted by randem
Why are you having trouble. You have all the information from the header. After reading the header and getting the information. Redim the buffer to the blocksize from the header then read the file. The very next read will be the start of the file...
That is where I am getting confuse. So the only way to GET the data back is by redim the array right?
If I was to use a
Code:
PUT #File, , "TEST"
, is there any way of pulling the data back without using a byte array?
Also, for the header, what do you save in it? The file size, remainder and anything else?
Thanks
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 7th, 2007, 12:15 AM
#36
Re: What is the fastest and simplest file encryption?
Try reading the data back into the header, then redim the array to the buffer size then read the data...
-
Nov 7th, 2007, 04:05 AM
#37
Thread Starter
Frenzied Member
Re: What is the fastest and simplest file encryption?
hmmmm....
Anyway, don't seem like I am getting any where with my questions. Thanks for the initial start but I am on a deadline for my project so will resolve this on my own at another time.
Last edited by Liquid Metal; Nov 7th, 2007 at 04:13 AM.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 7th, 2007, 08:46 AM
#38
Re: [RESOLVED] What is the fastest and simplest file encryption?
Really don't know what the issue is here.
#1 you load the original file into memory
#2, you created the header with the size of the original file stored in it.
#3, you wrote the header
#4, you appended the reversed file with a 2nd PUT statement
so reading it back in reverse
#1 you open the file and read the header(and it HAS to be a fixed size no matter what some other idiot is telling you to confuse you. Trust Randem on this, how else would you know how big the header is?)
#2 you take the size of the original file from the newly read header and redim the variable to that size so the next GET statement knows how much data to read
#3 You call the GET statement with the newly dimmed array and it will read the original file into memory(but still reversed)
#4 you reverse it and write it back to disk with a simple PUT statement.
It's really not that complicated.
-
Nov 7th, 2007, 09:30 AM
#39
Re: What is the fastest and simplest file encryption?
Hi Liquid Metal,
I know you marked the thread as resolved, but I see you still have problems.
Take a look at this example:
VB - 128, 160 and 256 Bit File Encryption/Decryption with MD5, SHA1 and SHA256
The encryption is very strong, I know you asked for a simple encryption, but you don't have to understand it. Just look at how the header file is saved in the encrypted file, and after that you have the encrypted data, just as you want to do.
As for your questions:
Originally Posted by Liquid Metal
If I was to use a
Code:
PUT #File, , "TEST"
, is there any way of pulling the data back without using a byte array?
Also, for the header, what do you save in it? The file size, remainder and anything else?
To read that "test" into a string variable, you do it like this.
Code:
Dim StrData As String
StrData = String(4, 0)
Get #File, , StrData
Note that i'm setting the string variable to an empty 4 character string. This is done to "make" a buffer.
The Get function does not know how much data to read from the file, 1 byte ?, 2 bytes ?, etc. That is why you have to set the buffer to a predefined length.... also, that is why you need a header in you encrypted file, to define what you have in the file, so then later you know how to read the file. You read the header that is always the same length, where in the header it describes what is in the rest of the file, so you know what to read after the header.
For example, in the link with my encryption, you will see that in the header i save:
3 bytes to define that that is an encrypted file
1 byte for encryption algorithm (one of 128 bit or 160 bit or 256 Bit)
4 bytes to store a random number that helps making the encryption stronger
16 or 20 or 32 bytes of the encrypted password (to verify if the password is correct when decrypting the file)
Then the actual encrypted data.
Note that the header in my case is not always the same length, because the encrypted password could be 16 or 20 or 32 bytes. But because before that I save the encryption algorithm, I know what to expect when reading the file.
Saving in the header what algorithm I used to encrypt the file also helps decrypting the file, otherwise I won't know what encryption strength to use when decrypting the file.
Anyways... I hope you have a better idea now why you need a header.
But in your case when you use such a simple encryption (personally I would not even call it encryption), you don't really need a header. If you do use a header it would be just to flag the file that the file is encrypted or not.... or maybe even the file name of the original file if you change the file name or extension after you encrypt it.
Last edited by CVMichael; Nov 7th, 2007 at 09:33 AM.
-
Nov 7th, 2007, 08:57 PM
#40
Thread Starter
Frenzied Member
Re: [RESOLVED] What is the fastest and simplest file encryption?
CVMichael,
Thanks for taking the time to answer my post. Thanks for posting the code for obtaining the string. I was trying to understand on all the different methods to obtain data from a binary file.
I knew that you are specialized in encryption/decryption because I have viewed your http://www.vbforums.com/showthread.php?t=232284 last year. Frankly, before starting this thread, I was going to use your encryption program but was concerned about the speed performance since it is strong encryption.
I was going to make something simple so I can learn the basic concept first and was going to revisit your encryption program at a later time.
Randem, LO and the rest have been really helpful. So far, with their help, I was able to code this out. If the code needs to be cleaned, please point out. Thanks
Code:
Private Type udtHeader
lngBufferSize As Long
lngRemainderByte As Long
intEncrypted As Integer
' strEncrypted As String * 10
End Type
Private Sub Flip(strSource As String, strTarget As String)
If Dir(strTarget) <> "" Then Kill strTarget
' Open as Read - Input File
Dim intFreeFileIn As Integer
intFreeFileIn = FreeFile
Open strSource For Binary Access Read Shared As #intFreeFileIn
Dim udtHD As udtHeader
Get #intFreeFileIn, 1, udtHD
' Open as Write - Output File
Dim intFreeFileOut As Integer
intFreeFileOut = FreeFile
Open strTarget For Binary Access Write As #intFreeFileOut
Dim lngPos As Long
Dim byteArr() As Byte
Dim intBufferSize As Integer
If udtHD.intEncrypted <> 12345 Then
' Allocate the space
Put #intFreeFileOut, 1, udtHD
intBufferSize = Int((5 * Rnd) + 1) ' Generate random value between 1 and 5.
lngPos = LOF(intFreeFileIn) 'Input File Size
Else
lngPos = LOF(intFreeFileIn) - udtHD.lngRemainderByte
intBufferSize = udtHD.lngBufferSize
'Write out the remainder first
ReDim byteArr(udtHD.lngRemainderByte - 1) 'Subtract 1 because of 0 base array
Get #intFreeFileIn, lngPos + 1, byteArr
Put #intFreeFileOut, , byteArr
End If
ReDim byteArr(intBufferSize - 1) 'Subtract 1 because of 0 base array
Do
' Work from the end of the file and subtract the buffer
lngPos = lngPos - intBufferSize
If udtHD.intEncrypted <> 12345 Then
If lngPos <= 0 Then Exit Do
Else
If lngPos < Len(udtHD) Then Exit Do
End If
' Read In - Write Out
Get #intFreeFileIn, lngPos + 1, byteArr
Put #intFreeFileOut, , byteArr
Loop
If udtHD.intEncrypted <> 12345 Then
' Read In - Write Out - the REMAINING Bytes
ReDim byteArr((intBufferSize + lngPos) - 1)
Get #intFreeFileIn, 1, byteArr 'LenB(udtHD)
Put #intFreeFileOut, , byteArr
udtHD.lngBufferSize = intBufferSize
udtHD.lngRemainderByte = intBufferSize + lngPos
udtHD.intEncrypted = 12345
Put #intFreeFileOut, 1, udtHD
End If
Erase byteArr
Close #intFreeFileIn
Close #intFreeFileOut
End Sub
Last edited by Liquid Metal; Nov 7th, 2007 at 09:01 PM.
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|