Hi,
Is there a ocx/class/dll to preform a file decryption to memory and an
encryption to file?
The file must never contain real readable data.
thanks
Frank Adam - 29 Nov 2004 13:00 GMT
>Hi,
>
>Is there a ocx/class/dll to preform a file decryption to memory and an
>encryption to file?
>
>The file must never contain real readable data.
The average Joe will have a hard time to read this, but if you need
more secure stuff, do a Google search for MD5 DLLs, or the PGP DLL,
which used to be available from the Pretty Good Privacy site. :
Aircode !
Function EncryptFile(inputFile as string) As Byte()
dim b() as Byte, i as long, fLen as long
dim hFile as integer
hFile = FreeFile
open inputFile for Binary as hFile
' if error, yell and exit sub
fLen = Lof(Hfile) - 1
redim b(fLen)
get #hFile,,b
close hFile
for i = lbound(b) to ubound(b)
b(i) = ((b(i) xor i) And 255)
next
EncryptFile = b
End Sub
May call it with :
const outfilepath = "yourpath\yourfile.enc"
Dim b() as byte, hFile as integer
b = EncryptFile("yourpath\yourfile.ext")
open outfilepath for binary as hFile
put #hfile,,b
close hFile
To convert back, just repeat the process on the encrypted file.

Signature
Regards, Frank
Norm Cook - 29 Nov 2004 13:50 GMT
Not very fancy and uses the simplest type of encryption, but here you go
To do:
1) add file exists checking for the decrypt function
2) this saves the key with the file but you might want to store it
elsewhere
(e. g. resource file, database, ini, whatever)
3) you might want to convert the FilePath and string to be encrypted
into properties
4) look into more exotic encryption schemes
'Class Module -- clsCrypt
Option Explicit
Private mKey As Byte
Public Property Get CryptKey() As Byte
CryptKey = mKey
End Property
Public Property Let CryptKey(ByVal vNewValue As Byte)
If vNewValue Then 'can't be zero else no encryption
mKey = vNewValue
Else
Err.Raise 5, , "Key cannot be zero"
End If
End Property
Public Sub EncryptToFile(ByVal FilePath As String, ByVal strCrypt As String)
Dim ff As Long
Dim i As Long
Dim b() As Byte
b = StrConv(strCrypt, vbFromUnicode) 'conv to byte arr
For i = 0 To UBound(b) 'encrypt the array
b(i) = b(i) Xor mKey
Next
'write out the key & the array
ff = FreeFile
KillIfExists FilePath
Open FilePath For Binary As ff
Put #ff, , mKey
Put #ff, , b
Close ff
End Sub
Public Function DecryptFileString(ByVal FilePath As String) As String
Dim ff As Long
Dim i As Long
Dim b() As Byte
ff = FreeFile
Open FilePath For Binary As ff
ReDim b(LOF(ff) - 2) '1 for key, 1 for zero based
Get #ff, , mKey 'read the key
Get #ff, , b 'get the array
Close ff
For i = 0 To UBound(b) 'decrypt
b(i) = b(i) Xor mKey
Next
DecryptFileString = StrConv(b, vbUnicode)
End Function
Private Sub KillIfExists(ByVal FilePath As String)
On Error Resume Next
Kill FilePath
End Sub
'========================
form -- usage
Option Explicit
Private Sub Form_Load()
Dim crypt As clsCrypt
Set crypt = New clsCrypt
crypt.CryptKey = 15 'could use random number here, 1 to 255
crypt.EncryptToFile "c:\somepath\test.txt", "Hello, this is the string to
be encrypted."
Debug.Print crypt.DecryptFileString("c:\somepath\test.txt")
End Sub
> Hi,
>
[quoted text clipped - 4 lines]
>
> thanks
Shell - 29 Nov 2004 21:32 GMT
In responce to the post:
On 29 Nov 2004 03:01:33 -0800, TheEtron@gmail.com (The Etron)
stated...and I replied:
>Hi,
>
[quoted text clipped - 4 lines]
>
>thanks
If you want to use simple xor translation, you can use the examples
given. If you want to use the Windows Cryptographic providers, I have
a class module on my website that includes encryption of a string to a
file, and decryption of a file to a string. There are also
encryption/decryption of strings and files, encryption/decryption by
passphrase of strings and files, signing text and validating
signatures, and hashing functions.
To show how to use the encrypt to file function I've included a code
snip below:
Set crypt = New CryptAPIcls
If crypt.SessionStart("ContainerName") Then
If crypt.EncryptString2File(sPlainText, sFileSpec) Then
End If
End If
crypt.SessionEnd
Set crypt = Nothing
Shell
-
http://drshell.home.mindspring.com/
Into computers since 1972.
WARNING! Information and e-mail addresses contained herein, are for personal use only. By entering this site, you agree that you will use this data only for lawful purposes and that, under no circumstances will you use this data to: allow, enable, or otherwise support the transmission of mass unsolicited, commercial advertising or solicitations via direct mail, electronic mail, or by telephone. Violators will be dealt with accordingly.
-
The Etron - 30 Nov 2004 07:39 GMT
I will look into all the solutions.
Thanks!