Page 1 of 1

Encrypt/Decrypt

Posted: Thu Jul 26, 2012 7:21 pm
by SoFlawLess_
How would I encrypt/decrypt my buffer:

Code: Select all

      lBuf = AllocateMemory(sfsize)
      ReadData(#Source_File, lBuf, sfsize)
      CloseFile(#Source_File)
I've tried this but I'm not sure how to use it:
http://www.purebasic.fr/english/viewtopic.php?p=282549

:shock:

Re: Encrypt/Decrypt

Posted: Thu Jul 26, 2012 8:49 pm
by luis
I suppose something like this (not tested):

XOrCrypt(lBuf, sfsize, "key of whatever length") ;encrypt

XOrCrypt(lBuf, sfsize, "key of whatever length") ;decrypt



The important point is: to any encryption/decryption routine you probably have to pass the address of your data ( lBuf ) and its length ( sfsize ) in a form or another.


EDIT:
Oh I'm sorry, I've put a "@" in front of lBuf that wasn't needed. Removed. :oops:
Force of habit when using a var that it isn't a pointer. Better use a pointer (*lBuf) like suggested by Warmonger. After all AllocateNemory return an address.
The address is a number so you can store it in an int like you did, but it's usually better to store it in a pointer, that is better suited to cope with further memory addressing operations.

Re: Encrypt/Decrypt

Posted: Fri Jul 27, 2012 8:42 am
by Warmonger
Maybe something like this.

Code: Select all

;Get Length Of File
Length = Lof(#Source_File)
If Length
  ;Copy File Data Into Buffer
  *lBuf = AllocateMemory(Length)
  ReadData(#Source_File, *lBuf, Length)
  CloseFile(#Source_File)
  ;Encrypt Buffer Data
  XOrCrypt(*lBuf, Length, "1337")
Else
  MessageRequester("Error", "No data to read", #MB_ICONERROR)
EndIf