Page 1 of 1
help read file in memory ecc... you can do ?
Posted: Mon Apr 06, 2009 11:08 am
by pbprog
hello
1 - help reading a file in memory
2 - help crypt and decrypt the file
3 - help decrypt file in memory and read decrypted file in memory
my version pure basic PureBasic 4.20
expect more news soon
I hope to answer soon
Posted: Mon Apr 06, 2009 2:42 pm
by Heathen
Can you be a little more specific?
Does this help?
Code: Select all
Size = FileSize(filename)
If Size > 0
*buffer = AllocateMemory(Size)
If ReadFile(0,filename)
ReadData(0,*buffer,Size)
CloseFile(0)
EndIf
EndIf
I'm curious as to why you have not yet updated to the newest version of purebasic? It has some nice features.
Posted: Mon Apr 06, 2009 3:19 pm
by pbprog
ok now purebasic the upgrade to version 4.30
I formatted the pc .. and the purebasic was not updated ...
tanks for 'example
but now how to encrypt and decrypt a file?
and decrypts a file in memory?
Posted: Mon Apr 06, 2009 4:29 pm
by pdwyer
PB doesn't have any encryption libs built in, what algorithm are you wanting to use? or what level of encryption? How serious is this?
Posted: Mon Apr 06, 2009 4:55 pm
by pbprog
I am also a sufficient level of encryption simply by perhaps 64 to 128 bits, the important one when it goes to open the file does not understand anything of reading
Posted: Mon Apr 06, 2009 5:17 pm
by pdwyer
If you just need something simple, I would suggest an aglorithm like XTEA, it's fast, not a lot of code and seems to be secure from what I read. If I had secrets from the NSA I'm not sure I'd use it but I guess that's not were you are coming from

Posted: Tue Apr 07, 2009 11:53 am
by pbprog
how?
XTEA samples in pure basic
other algorithms such as the Advanced Encryption Standard (AES) and others might have examples?
AES at least as an example ...., I think it would be helpful to all ...
Re: help read file in memory ecc... you can do ?
Posted: Tue Apr 07, 2009 12:43 pm
by Joakim Christiansen
pbprog wrote:hello
3 - help decrypt file in memory and read decrypted file in memory

What kind of file is it?
I want to know what you mean with "read", output as text maybe?
Edit:
Some encryption algorithm you can play with:
Code: Select all
Procedure XOrCrypt(*Buffer,Len.l,Key$)
;just some weird and powerful(?) xor encryption algorithm by JLC
Protected i, Byte.b, KeyByte.b
Protected KeyLength = Len(Key$), KeyPos
For i=0 To Len-1
Byte = PeekB(*Buffer+i)
KeyByte = PeekB(@Key$+KeyPos)
;PeekB(*Buffer+i) ! PeekB(@Key$+KeyPos) ! Len ! i
Byte ! KeyByte ! Len ! i ! KeyLength ;xor the shit
PokeB(*Buffer+i,Byte)
KeyPos + 1
If KeyPos > KeyLength
KeyPos = 0
EndIf
Next
EndProcedure
String$ = "the string to be encrypted"
Length = Len(String$)
XOrCrypt(@String$,Length,"key of whatever length") ;encrypt
Debug String$ ;if it looks cut that's because a null-char. terminates it
XOrCrypt(@String$,Length,"key of whatever length") ;decrypt
Debug String$
Posted: Tue Apr 07, 2009 1:02 pm
by pbprog
and is a text file or ini file ... which is always text ..
I hope that I can then using GetIniKey read from memory .....
otherwise how do I interpret ini files from memory as GetIniKey?
Posted: Tue Apr 07, 2009 1:35 pm
by Joakim Christiansen
pbprog wrote:and is a text file or ini file ... which is always text ..
I hope that I can then using GetIniKey read from memory .....
otherwise how do I interpret ini files from memory as GetIniKey?
A simple thing would be to store the decrypted file in the temp directory and then read it using the normal commands. PB has commands for this btw, look in help file under "Preference", OpenPreferences and then ReadPreferenceString will probably help you.
If you REALLY need to do this directly in memory I'm not sure if you can get any commands to do that. Maybe make some commands yourself to do this (what I would do). Or see if anyone else can help you.

Posted: Tue Apr 07, 2009 2:03 pm
by pbprog
this example and for strings only how to encrypt and decrypt an entire file?
this example and for strings, how to encrypt and decrypt an entire file?
Code: Select all
Procedure XOrCrypt(*Buffer,Len.l,Key$)
;just some weird and powerful(?) xor encryption algorithm by JLC
Protected i, Byte.b, KeyByte.b
Protected KeyLength = Len(Key$), KeyPos
For i=0 To Len-1
Byte = PeekB(*Buffer+i)
KeyByte = PeekB(@Key$+KeyPos)
;PeekB(*Buffer+i) ! PeekB(@Key$+KeyPos) ! Len ! i
Byte ! KeyByte ! Len ! i ! KeyLength ;xor the shit
PokeB(*Buffer+i,Byte)
KeyPos + 1
If KeyPos > KeyLength
KeyPos = 0
EndIf
Next
EndProcedure
String$ = "the string to be encrypted"
Length = Len(String$)
XOrCrypt(@String$,Length,"key of whatever length") ;encrypt
Debug String$ ;if it looks cut that's because a null-char. terminates it
XOrCrypt(@String$,Length,"key of whatever length") ;decrypt
Debug String$
however, thanks to 'help ... and all the technical support ....
Posted: Tue Apr 07, 2009 2:05 pm
by eesau
pbprog wrote:this example and for strings only how to encrypt and decrypt an entire file?
If your file is text-only, you can read the entire file into a string and then do the en-/decrypting.
Posted: Tue Apr 07, 2009 2:15 pm
by pdwyer
I put this in the tips area now, not sure if it helps, the more I read of this thread the less I understand what you want to do.
http://www.purebasic.fr/english/viewtop ... 565#282565
Posted: Tue Apr 07, 2009 3:04 pm
by Joakim Christiansen
eesau wrote:pbprog wrote:this example and for strings only how to encrypt and decrypt an entire file?
If your file is text-only, you can read the entire file into a string and then do the en-/decrypting.
No matter what the file is you can read it into a memory buffer and use my procedure on it, you only need to know the memory address and length.
Example:
Code: Select all
Procedure CryptFile(file$)
Protected *Buffer
If ReadFile(0,file$) And Lof(0) > 0
*Buffer = AllocateMemory(Lof(0))
ReadData(0,*Buffer,Lof(0))
CloseFile(0)
CreateFile(0,file$)
XOrCrypt(*Buffer,MemorySize(*Buffer),"my key")
WriteData(0,*Buffer,MemorySize(*Buffer))
CloseFile(0)
FreeMemory(*Buffer)
EndIf
EndProcedure
But for large files reading it into memory is a bad idea, so the code should be modified to work with the content of the file instead then. Could probably do something like this and then "cut and paste" code from the other procedure:
Code: Select all
While Not Eof(0)
byte = ReadByte(0)
byte = ;new value
WriteByte(0,byte)
Wend
Very nice