PDA

View Full Version : save bytes inside database


HW2002
07-28-2008, 12:52 PM
hi,

i wonder how i can save bytes inside database?

I load image using urlstream and i want to save image source inside database?

regards,
hw2002

evride
07-28-2008, 07:28 PM
why would you do that? why not just store the file in the ApplicationStorageDirectory? then you can store the link to the file in a local database.

but if you really must store it in a database for some odd reason, then use the Base64 class to encode the image's byte data to base64 then you can store it in the database.

HW2002
07-28-2008, 08:43 PM
why would you do that? why not just store the file in the ApplicationStorageDirectory? then you can store the link to the file in a local database.

but if you really must store it in a database for some odd reason, then use the Base64 class to encode the image's byte data to base64 then you can store it in the database.

I need to secure/hide the images, so the user cannot find them just as regular files.

Do you have any example how to get the image byte data to base64?

_urlStream.readbyte() ?

Any url or tutorial would also be great :)

Thanks for help.

evride
07-28-2008, 09:11 PM
well, your method of hiding the files is not completely secure. There are sqlite admin apps that can open up the SQLite database and view all entries, no matter what file extension you give it. They could open the database, decode the Base64 code and create an image from that. So your method is security through obscurity and will not protect your images 100% though I think it will be good enough for 99% of people.


Download the Base64 class here (http://dynamicflash.com/goodies/base64/) and put it with the rest of your class files.
I have never used the urlStream class so, I hope you know what you are doing there.
Anyways, read the streamed data into a ByteArray and then encode to Base64 using the Base64.encodeByteArray() method.

var ba:ByteArray = new ByteArray();
//read the urlStream data into the byteArray. leaving that part out here
var base64String:String = Base64.encodeByteArray(ba);
//insert base64String into the database


then put this in an sqlite database. i hope you know how to do this part, if not i can help you with that as well.

to retrieve the stored data you would load the data from the sqlite database and decode the string into a bytearray.

HW2002
07-29-2008, 06:07 AM
well, your method of hiding the files is not completely secure. There are sqlite admin apps that can open up the SQLite database and view all entries, no matter what file extension you give it. They could open the database, decode the Base64 code and create an image from that. So your method is security through obscurity and will not protect your images 100% though I think it will be good enough for 99% of people.


Download the Base64 class here (http://dynamicflash.com/goodies/base64/) and put it with the rest of your class files.
I have never used the urlStream class so, I hope you know what you are doing there.
Anyways, read the streamed data into a ByteArray and then encode to Base64 using the Base64.encodeByteArray() method.

var ba:ByteArray = new ByteArray();
//read the urlStream data into the byteArray. leaving that part out here
var base64String:String = Base64.encodeByteArray(ba);
//insert base64String into the database


then put this in an sqlite database. i hope you know how to do this part, if not i can help you with that as well.

to retrieve the stored data you would load the data from the sqlite database and decode the string into a bytearray.

thank you very much...

do you have any other methods to secure the images?

maybe encrypt the bytearray using hashkey?

evride
07-29-2008, 06:17 AM
there are encryption classes out there but i haven't played with them enough to help you on that.

http://code.google.com/p/as3crypto/