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
AES-256
Re: AES-256
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
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
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
- netmaestro
- PureBasic Bullfrog

- Posts: 8452
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: AES-256
The size can vary but anything up to a gig really.
-
Little John
- Addict

- Posts: 4828
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: AES-256
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):
Regards, Little John
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- netmaestro
- PureBasic Bullfrog

- Posts: 8452
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: AES-256
Thats great! Many thanks! I'm just working on shoe-horning it into my program.
Cheers!
Mark
Cheers!
Mark

