- /* Create folder. */
- $id;
- do
- {
- $id = jirafeau_gen_random (32);
- $p = VAR_BLOCK . s2p ($id);
- } while (file_exists ($p));
- @mkdir ($p, 0755, true);
- if (!file_exists ($p))
- {
- echo "Error";
- return;
+ /* Prepare module. */
+ $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
+ /* Generate key. */
+ $crypt_key = jirafeau_gen_random(10);
+ $md5_key = md5($crypt_key);
+ $iv = jirafeau_crypt_create_iv($md5_key, mcrypt_enc_get_iv_size($m));
+ /* Init module. */
+ mcrypt_generic_init($m, $md5_key, $iv);
+ /* Crypt file. */
+ $r = fopen($fp_src, 'r');
+ $w = fopen($fp_dst, 'c');
+ while (!feof($r)) {
+ $enc = mcrypt_generic($m, fread($r, 1024));
+ if (fwrite($w, $enc) === false) {
+ return '';
+ }
+ }
+ fclose($r);
+ fclose($w);
+ /* Cleanup. */
+ mcrypt_generic_deinit($m);
+ mcrypt_module_close($m);
+ return $crypt_key;
+}
+
+/**
+ * Decrypt file.
+ * @param $fp_src file path to the file to decrypt.
+ * @param $fp_dst file path to the file to write decrypted file (could be the same).
+ * @param $k string composed of the key and the iv separated by a point ('.')
+ * @return key used to decrypt. a string of length 0 is returned if failed.
+ */
+function jirafeau_decrypt_file($fp_src, $fp_dst, $k)
+{
+ $fs = filesize($fp_src);
+ if ($fs === false || $fs == 0 || extension_loaded('mcrypt') == false) {
+ return false;