Page 1 of 1

AES-256

Posted: Mon Aug 02, 2010 11:33 am
by orb_505
Hi all,

I'm writing a program which will copy a file from a HDD to a USB pen. I want the file to be encrypted on the USB pen using AES-256. Does anyone know if this can be done and if so a code example?

Many thanks

Mark

Re: AES-256

Posted: Mon Aug 02, 2010 2:24 pm
by cas
Yes, it can be done. Just use PB help file and go through these commands:
OpenFileRequester()/SaveFileRequester()
ReadFile()
AllocateMemory()
ReadData()
AESEncoder()
CreateFile()
CloseFile()

Re: AES-256

Posted: Mon Aug 02, 2010 2:39 pm
by orb_505
Hi Cas,

I saw the AESEncoder command but it gave the impression it was for strings. Either way I'm not creating the file but copying it using CopyFile(), the file itself isn't created by my program, a third party creates it, I just want it copying to a USB pen but then for the security of postal transportation encoding it via AES.

I was hoping for a command which you could just throw it the filename and it'll encrypt it for you :)

Mark

Re: AES-256

Posted: Mon Aug 02, 2010 2:51 pm
by netmaestro
The approach depends on the file size. How large is it?

Re: AES-256

Posted: Mon Aug 02, 2010 3:12 pm
by orb_505
The size can vary but anything up to a gig really.

Re: AES-256

Posted: Mon Aug 02, 2010 4:18 pm
by Little John
Encryption is not only for strings. Actually, AES encrypted data can contain zeros somewhere in the middle, which is not possible with PB strings.

Here is a basic algorithm for copying a file of arbitrary size (as long as there is enough free space on the target drive):

Code: Select all

; tested with PB 4.50

EnableExplicit

#MaxChunk = 4096

Procedure.i Copy (infile$, outfile$)
   Protected ifn, ofn, bytes
   Protected *buffer

   ifn = ReadFile(#PB_Any, infile$)
   If ifn = 0
      ProcedureReturn #False          ; error
   EndIf

   ofn = CreateFile(#PB_Any, outfile$)
   If ofn = 0
      CloseFile(ifn)
      ProcedureReturn #False          ; error
   EndIf

   *buffer= AllocateMemory(#MaxChunk)
   Repeat
      bytes = ReadData(ifn, *buffer, #MaxChunk)
      ; +----------------------------------------------+
      ; | Do encryption or other funny stuff here with |
      ; | the memory block to which *buffer points.    |
      ; +----------------------------------------------+
      WriteData(ofn, *buffer, bytes)
   Until bytes < #MaxChunk
   FreeMemory(*buffer)
   CloseFile(ofn)
   CloseFile(ifn)
   
   ProcedureReturn #True              ; success
EndProcedure

Define infile$, outfile$

infile$ = "source.dat"
outfile$ = "target.dat"
If Copy(infile$, outfile$)
   Debug "OK"
Else
   Debug "Error"
EndIf
Regards, Little John

Re: AES-256

Posted: Mon Aug 02, 2010 6:21 pm
by netmaestro

Re: AES-256

Posted: Tue Aug 03, 2010 8:44 am
by orb_505
Thats great! Many thanks! I'm just working on shoe-horning it into my program.

Cheers!

Mark