views:

291

answers:

3

Can any one please let me know the way, how can i encrypt/decrypt a file instead of string.

A: 

I'm a little confused, can't you just read/write the string to a file using functions like file_get_contents and file_put_contents?

If you need an encryption-class there are some over at PHP classes. There is also a paid solution here: phpAES.

mqchen
+2  A: 

instead of string.

That rather implies that you already know how to encrypt the string - and since you're being specific about the algorithm, that you can create an appropriate representation for the other tools being used to operate on the data. But you haven't said what mode of operation you need to use - implementing this using CBC is trivial.

It's also not stated - but implied in your question, that the data is too large to load into a string (otherwise its simply a case of encrypting file_get_contents()).

There doesn't seem to be much in the way of documentation, but I would expect the modificed key required for ECB is updated in the resource created by mcrypt_module_open() and modified by mcrypt_generic_init(). Then its just a matter of feeding in parts from the file sized as a multiple of the block size (see mcrypt_get_block_size)

See http://www.php.net/manual/en/function.mcrypt-module-open.php

C.

symcbean
A: 

I guess it is better to create your own library for it and expose an API that just accepts a filepath instead of it content. It can open read the file and do the encryption / decryption.

You can use your own or pre-existing algo for encrypt/decrypt. Also you can have an argument in that API to accept the filepath to store the decrypted data or replace with the same file or whatever.

AKN