help read file in memory ecc... you can do ?

Just starting out? Need help? Post your questions and find answers here.
pbprog
User
User
Posts: 11
Joined: Mon Apr 06, 2009 11:00 am

help read file in memory ecc... you can do ?

Post by pbprog »

hello
1 - help reading a file in memory :cry:

2 - help crypt and decrypt the file :cry:

3 - help decrypt file in memory and read decrypted file in memory :cry:

my version pure basic PureBasic 4.20


expect more news soon

I hope to answer soon
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post 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.
I love Purebasic.
pbprog
User
User
Posts: 11
Joined: Mon Apr 06, 2009 11:00 am

Post 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?
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
pbprog
User
User
Posts: 11
Joined: Mon Apr 06, 2009 11:00 am

Post 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
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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 :)
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
pbprog
User
User
Posts: 11
Joined: Mon Apr 06, 2009 11:00 am

Post 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 ...
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: help read file in memory ecc... you can do ?

Post by Joakim Christiansen »

pbprog wrote:hello
3 - help decrypt file in memory and read decrypted file in memory :cry:
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$
I like logic, hence I dislike humans but love computers.
pbprog
User
User
Posts: 11
Joined: Mon Apr 06, 2009 11:00 am

Post 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?
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post 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. :)
I like logic, hence I dislike humans but love computers.
pbprog
User
User
Posts: 11
Joined: Mon Apr 06, 2009 11:00 am

Post 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 ....
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post 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.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post 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
pdwyer wrote: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
Very nice
I like logic, hence I dislike humans but love computers.
Post Reply