AES-256

Just starting out? Need help? Post your questions and find answers here.
orb_505
User
User
Posts: 23
Joined: Wed Apr 05, 2006 2:30 pm

AES-256

Post 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
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: AES-256

Post 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()
orb_505
User
User
Posts: 23
Joined: Wed Apr 05, 2006 2:30 pm

Re: AES-256

Post 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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: AES-256

Post by netmaestro »

The approach depends on the file size. How large is it?
BERESHEIT
orb_505
User
User
Posts: 23
Joined: Wed Apr 05, 2006 2:30 pm

Re: AES-256

Post by orb_505 »

The size can vary but anything up to a gig really.
Little John
Addict
Addict
Posts: 4828
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: AES-256

Post 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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: AES-256

Post by netmaestro »

BERESHEIT
orb_505
User
User
Posts: 23
Joined: Wed Apr 05, 2006 2:30 pm

Re: AES-256

Post by orb_505 »

Thats great! Many thanks! I'm just working on shoe-horning it into my program.

Cheers!

Mark
Post Reply