Monday, 15 August 2011

Encrypt file with PHP OpenSSL -


Usually, I use openssl_encrypt to encrypt simple strings with AES in PHP I am, and it works very well.

Now send me AES-256 is required to encrypt files with CTR mode, but it is the only way to file_get_contents the entire contents of the file and then real The openssl_encrypt function to encrypt the file data The problem is that this method is very "bad" due to the serious waste of memory.

1) Is there a way to work with the data selected with PHP OpenSSL?

For example:

  & lt? Php // .. $ F = fopen ('large.iso', 'r'); While (Fie ($ f)) {$ chunk = fread ($ f, 16); $ Cipher = openssl_encrypt (... $ share ...); // ... code ...} // ... more code ...? & Gt;   

2) The official document has not yet been published openssl_encrypt . Can anyone explain the meaning of the function's parameters for the use of AES-CTR mode? Is the counter controlled automatically? What function is required to implement the manual given XOR?

Note: This is a professional project, so I do not want to use phpseclib or other "anonymous" libraries, nor do I use the command line Wish.

It seems that .php is possible to use AES 256 CTR without this temporary file

But for the next chiper type:

  OPENSSL_CIPHER_RC2_40 OPENSSL_CIPHER_RC2_128 OPENSSL_CIPHER_RC2_64 OPENSSL_CIPHER_DES OPENSSL_CIPHER_3DES OPENSSL_CIPHER_AES_128_CBC OPENSSL_CIPHER_AES_192_CBC OPENSSL_CIPHER_AES_256_CB C   

You can use the output key on the fly:

  $ res = openssl_pkey_new ( 'shipper Arges here'); Openssl_pkey_export ($ res, $ Private_key); $ Public_key = openssl_pkey_get_details ($ res); $ public_key = $ public_key ["key"];   

then encrypted:

  $ crypted_text = Openssl_get_privatekey ($ private_key, 'your data');   

and decrypt:

  openssl_public_decrypt ($ crypted_text, $ decrypted_text, $ public_key);   

So if you do not want to use the files, can you switch to OPENSSL_CIPHER_AES_256_CBC?

No comments:

Post a Comment