On 11.3.2006, at 02:05, Michael Williams wrote:

Does anyone have much experience with the e-Cryptit engine and general file encryption? I'm interested in encrypting/decrypting files on a drive. I don't want to encrypt the text within the files, but the file as a whole. Thanks in advance.

Regards,
Michael

Here is a one to encrypt the file, the only thing seriously different about decrypting would be to extract the padding bytes to make sure they don't end up in the final decrypted file.


Protected Sub EncryptFile(src as FolderItem,dst as FolderItem,key as String)
   Dim inStream as BinaryStream
   Dim outStream as BinaryStream
   Dim buffer as String
   Dim outBuffer as String
   Dim cipher as AES_CBC
   Dim hash as SHA_256
   Dim cipherKey as String
   Dim i as Integer
   Dim rand as Random

   inStream = src.OpenAsBinaryFile(false)
   outStream = dst.CreateBinaryFile("????")

   // Hash the Key material to get the correct bit size from the key
   hash = new SHA_256()
   hash.Update(key)
   cipherKey = hash.Final()

   cipher = new AES_CBC(cipherKey)

   while not inStream.EOF
      buffer = inStream.Read(4096)

      outBuffer = cipher.Encrypt(buffer)

      if outBuffer.LenB() > 0 then
         outStream.Write(outBuffer)
      end if
   wend

   // Write the padding
   buffer = ""
   rand = new Random()
   rand.Seed = Ticks()
   for i = 1 to cipher.PaddingBlockSize()
      buffer = buffer + Chr(rand.InRange(1,255))
   next
   outBuffer = cipher.Encrypt(buffer)
   if outBuffer.LenB() > 0 then
      outStream.Write(outBuffer)
   end if

   outBuffer = cipher.FinishEncrypt()

   if outBuffer.LenB() > 0 then
      outStream.Write(outBuffer)
   end if

   inStream.Close()
   outStream.Close()
End Sub


--
______________________________________________________________________
Björn Eiríksson                        [EMAIL PROTECTED]
Einhugur Software
http://www.einhugur.com/
______________________________________________________________________
Einhugur Software has sold its products in 52 countries world wide.
______________________________________________________________________
For support:                           [EMAIL PROTECTED]
To post on the maillist:               [EMAIL PROTECTED]


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to