<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-diesel &#187; zend</title>
	<atom:link href="http://www.codediesel.com/tag/zend/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Encrypting uploaded files in PHP</title>
		<link>http://www.codediesel.com/php/encrypting-uploaded-files-in-php/</link>
		<comments>http://www.codediesel.com/php/encrypting-uploaded-files-in-php/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 13:19:07 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2740</guid>
		<description><![CDATA[During a recent project, the client requested that uploaded files be encrypted for security reasons. As I already had the uploaded code ready and tested I just needed to add some extra encryption capability to the code. As earlier I&#8217;d encountered Zends wonderful Zend_Filter class, I decided to go with it and use the Zend_Filter_Encrypt [...]]]></description>
			<content:encoded><![CDATA[<p>During a recent project, the client requested that uploaded files be encrypted for security reasons. As I already had the uploaded code ready and tested I just needed to add some extra encryption capability to the code. As earlier I&#8217;d encountered Zends wonderful <a target="_blank" href="http://framework.zend.com/manual/en/zend.filter.set.html">Zend_Filter</a> class, I decided to go with it and use the <em>Zend_Filter_Encrypt</em> and <em>Zend_Filter_Decrypt</em> to accomplish the work. The Zend_Filter component provides a set of common useful data filters, among which are the encryption filters. Although my project was not developed in Zend, I could easily integrate the required classes in the code. Note that Zend has a great upload library, <a target="_blank" href="http://framework.zend.com/manual/en/zend.file.transfer.introduction.html">Zend_File_Transfer</a>, that lets you easily manage file uploading and also encryption, but as I already had the upload code tested, I decided to just add the encryption part.<br />
<span id="more-2740"></span></p>
<h4>Downloading the Zend framework</h4>
<p>As the following code requires the Zend framework make sure you <a target="_blank" href="http://www.zend.com/community/downloads">download</a> it first. For this code I used Zend 1.11.0 Full version. You can also download the required files at the end of this post.</p>
<h4>Which Zend framework files do I need</h4>
<p>You only need some selected files to make the below code work. The following is a list of files and directories that I used from the &#8216;ZendFramework-1.11.0\library\Zend&#8217; directory. The top three in the list are directories. There are also many files in the &#8216;Filter&#8217; directory that are not required, but for keeping it simple we will use the whole thing.</p>
<p>File (dir)<br />
Filter (dir)<br />
Loader (dir)<br />
Loader.php<br />
Filter.php<br />
Exception.php</p>
<h4>Encrypting uploaded files</h4>
<p>For the following example I&#8217;ve not included the file uploading code. I assume you already have the upload code ready. You can also use the following code by itself to encrypt/decrypt files. The program for file encryption is shown below.</p>

<div class="wp_codebox"><table><tr id="p274010"><td class="code" id="p2740code10"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Load the Zend file encrypting filter */</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./Zend/Filter/File/Encrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  Set various encryption options. */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009933; font-style: italic;">// Encryption type - Openssl or Mcrypt</span>
                <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Initialization vector</span>
                <span style="color: #0000ff;">'vector'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'236587hgtyujkirtfgty5678'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption algorithm</span>
                <span style="color: #0000ff;">'algorithm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption key</span>
                <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'KFJGKDK$$##^FFS345678FG2'</span> 
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the library and pass the options */</span>
<span style="color: #000088;">$encrypt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
   Set output filename, where the encrypted file will be stored.
   If we omit this, the encrypted file will overwrite the original file.
*/</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.enc.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now encrypt a file */</span>
<span style="color: #000088;">$encrypt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Decrypting the encrypted files is as simple as above. Note that you need to keep the &#8216;vector&#8217; and &#8216;key&#8217; values the same as you used for encryption, or you will not be able to correctly decrypt the file.</p>

<div class="wp_codebox"><table><tr id="p274011"><td class="code" id="p2740code11"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Load the Zend file decrypting filter */</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./Zend/Filter/File/Decrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  Set various decryption options. */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009933; font-style: italic;">// Encryption type - Openssl or Mcrypt</span>
                <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Initialization vector</span>
                <span style="color: #0000ff;">'vector'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'236587hgtyujkirtfgty5678'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Decryption algorithm</span>
                <span style="color: #0000ff;">'algorithm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Decryption key</span>
                <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'KFJGKDK$$##^FFS345678FG2'</span> 
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the library and pass the options */</span>
<span style="color: #000088;">$decrypt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
   Set output filename, where the decrypted file will be stored.
*/</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now decrypt the previously encrypted file */</span>
<span style="color: #000088;">$decrypt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.enc.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Selecting a encryption algorithm</h4>
<p>In the example given I&#8217;ve used the <a target="_blank" href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard">rijndael-192 (AES)</a> algorithm but you can choose some other according to the availability. &#8216;rijndael&#8217; is fine for most requirements. First you will need to know what algorithms are supported by your installation. A quick way to find out is to use the following code.</p>

<div class="wp_codebox"><table><tr id="p274012"><td class="code" id="p2740code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000088;">$algorithms</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_list_algorithms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$algorithms</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$cipher</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$cipher</span>&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>On my system it lists the following algorithms.</p>
<p>cast-128 , gost , rijndael-128 , twofish , arcfour , cast-256 , loki97 , rijndael-192 , saferplus , wake , blowfish-compat , des , rijndael-256 , serpent , xtea , blowfish , enigma , rc2 , tripledes.</p>
<h4>Selecting a random Initialization vector</h4>
<p>Before we continue our discussion, a brief overview of Initialization vector (IV). An IV is a random string that can be used along with a key for data encryption. IV is used to prevent a sequence of text that is identical to a previous sequence from producing the same exact cipher-text when encrypted. </p>
<p>As you can see from the example code we have used a fixed IV. The disadvantage of this is that there is a possibility that a committed hacker would be able to guess the IV by studying the pattern in the encrypted files and thus break the encryption. The best way then is to let the class generate a random IV for each new encryption. This requires a little change of code, as shown below. The only extra step we now require is to store the generated random IV in a database which will then be used for decryption.</p>
<p>Encryption:</p>

<div class="wp_codebox"><table><tr id="p274013"><td class="code" id="p2740code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Load the Zend file encrypting filter */</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./Zend/Filter/File/Encrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  Set various encryption options. */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009933; font-style: italic;">// Encryption type - Openssl or Mcrypt</span>
                <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption algorithm</span>
                <span style="color: #0000ff;">'algorithm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption key</span>
                <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'KFJGKDK$$##^FFS345678F54'</span> 
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the library and pass the options */</span>
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Generate a random vector */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setVector</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set output filename, where the encrypted file will be stored. */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.enc.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now encrypt a file */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
    Save the vector in a DB or somewhere else,
    we will need this during decryption
*/</span>
<span style="color: #000088;">$vector</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getVector</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Decryption:</p>

<div class="wp_codebox"><table><tr id="p274014"><td class="code" id="p2740code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Load the Zend file decrypting filter */</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./Zend/Filter/File/Decrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  Set various encryption options. */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009933; font-style: italic;">// Encryption type - Openssl or Mcrypt</span>
                <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption algorithm</span>
                <span style="color: #0000ff;">'algorithm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption key</span>
                <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'KFJGKDK$$##^FFS345678F54'</span> 
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the library and pass the options */</span>
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
   Use the saved vector for decryption.
   Note that using a wrong vector will result in a incorrect decryption.
*/</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setVector</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'your-saved-vector'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set output filename, where the decrypted file will be stored. */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.dec.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now decrypt the previously encrypted file */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.enc.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Selecting the correct &#8216;vector&#8217; and &#8216;key&#8217; size</h4>
<p>Before you run the code make sure that you set your PHP error reporting to &#8216;E_ALL&#8217;. Mcrypt requires that you use a correct IV and key length, which depends on which algorithm is used. Selecting a wrong IV (if you are using a fixed IV) or key length can generate the following error, which if the errors are disabled will be hidden and you will keep wondering as to why the files are not getting encrypted.:</p>
<p>- in case of a wrong vector length:</p>
<blockquote><p>
Fatal error: Uncaught exception &#8216;Zend_Filter_Exception&#8217; with message &#8216;The given vector has a wrong size for the set algorithm&#8217;
</p></blockquote>
<p>- in case of a wrong key length:</p>
<blockquote><p>
Fatal error: Uncaught exception &#8216;Zend_Filter_Exception&#8217; with message &#8216;The given key has a wrong size for the set algorithm&#8217;
</p></blockquote>
<p>To get the correct sizes use the following code. I&#8217;ve used &#8216;rijndael-192&#8242; algorithm here, but you need to substitute whatever algorithm you have selected instead.</p>

<div class="wp_codebox"><table><tr id="p274015"><td class="code" id="p2740code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$cipher</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_module_open</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ofb'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$vector_size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_enc_get_iv_size</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cipher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$key_size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_enc_get_supported_key_sizes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cipher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vector_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Which on my machine returns the following.</p>

<div class="wp_codebox"><table><tr id="p274016"><td class="code" id="p2740code16"><pre class="text" style="font-family:monospace;">24
&nbsp;
Array
(
    [0] =&gt; 16
    [1] =&gt; 24
    [2] =&gt; 32
)</pre></td></tr></table></div>

<p>So if the IV size returns &#8217;24&#8242; then you need to use a random string of 24 character for the Initialization Vector (IV). The key length can be any from the returned values, 16, 24 or 32.</p>
<h4>Downloading encrypted file</h4>
<p>To round-off the post, I&#8217;ve included code to download a encrypted file via a link, which will be decrypted and passed to the user. For example you can call the download link as below:</p>

<div class="wp_codebox"><table><tr id="p274017"><td class="code" id="p2740code17"><pre class="php" style="font-family:monospace;">http<span style="color: #339933;">:</span><span style="color: #009933; font-style: italic;">//www.site.com/download-file.php?docname=test.enc.pdf</span></pre></td></tr></table></div>

<p>The code for &#8216;download-file.php&#8217; is shown below. The code is a bit simplified, for e.g no security validation is done on the $_GET variable.</p>

<div class="wp_codebox"><table><tr id="p274018"><td class="code" id="p2740code18"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* download-file.php */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
   The constants UPLOAD_PATH, TEMP_PATH, INIT_VECTOR, ENCRYPTION_KEY
   have to be changed to your particular setup.
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend/Filter/File/Decrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$docname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'docname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> UPLOAD_PATH <span style="color: #339933;">.</span> <span style="color: #000088;">$docname</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$destination</span> <span style="color: #339933;">=</span> TEMP_PATH <span style="color: #339933;">.</span> <span style="color: #000088;">$docname</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Error accessing the file.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Copy encrypted file to a temporary folder */</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error accessing file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/* Zend file decryption */</span>
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Decrypt<span style="color: #009900;">&#40;</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'adapter'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">'vector'</span>      <span style="color: #339933;">=&gt;</span> INIT_VECTOR<span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">'algorithm'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">'key'</span>         <span style="color: #339933;">=&gt;</span> ENCRYPTION_KEY
                      <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;MSIE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: &quot;application/octet-stream&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition: attachment; filename=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$docname</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Expires: 0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cache-Control: must-revalidate, post-check=0, pre-check=0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Transfer-Encoding: binary&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pragma: public'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: &quot;application/octet-stream&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition: attachment; filename=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$docname</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Transfer-Encoding: binary&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Expires: 0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pragma: no-cache'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">fpassthru</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Delete the copied decrypted from the temp folder */</span>
<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/encrypting-uploaded-files-in-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Creating a Figlet text in php</title>
		<link>http://www.codediesel.com/php/creating-a-figlet-text-in-php/</link>
		<comments>http://www.codediesel.com/php/creating-a-figlet-text-in-php/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 12:21:27 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=130</guid>
		<description><![CDATA[Zend_Text_Figlet is a Zend component which enables developers to create a FIGlet text. A Figlet is nothing but a technique of creating large letters using ordinary text. I don&#8217;t know how to make a use of this in my daily matters, but its a nice recreation. A short history on Figlet can be found here. [...]]]></description>
			<content:encoded><![CDATA[<p>Zend_Text_Figlet is a Zend component which enables developers to create a <a title="figlet" href="http://en.wikipedia.org/wiki/FIGlet" target="_blank">FIGlet</a> text. A Figlet is nothing but a technique of creating large letters using ordinary text. I don&#8217;t know how to make a use of this in my daily matters, but its a nice recreation. A short history on Figlet can be found <a target="_blank" href="http://www.figlet.org/figlet_history.html">here</a>. An example and the code to generate it is shown below.<br />
<span id="more-130"></span></p>
<pre>
  ____     __   _     ____
 |  _ \\  | || | ||  |  _ \\
 | |_| || | '--' ||  | |_| ||
 | .__//  | .--. ||  | .__//
 |_|--`   |_|| |_||  |_|--`
 `-`      `-`  `-`   `-`
</pre>

<div class="wp_codebox"><table><tr id="p13022"><td class="code" id="p130code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;zend/text/figlet.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$figlet</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Text_Figlet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$figlet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PHP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>If you are displaying the output on a web page make sure to use the &#8216;pre&#8217; tags around the Figlet. The Figlet looks good in a pure text format.<br />
You can also define various options for the Figlet by passing the function an array of options, like say changing the font.</p>

<div class="wp_codebox"><table><tr id="p13023"><td class="code" id="p130code23"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;zend/text/figlet.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* use a different font */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'font'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'graffiti.flf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>               
&nbsp;
<span style="color: #000088;">$figlet</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Text_Figlet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$figlet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PHP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<pre>
__________   ___ ___  __________
\______   \ /   |   \ \______   \
 |     ___//    ~    \ |     ___/
 |    |    \    Y    / |    |
 |____|     \___|_  /  |____|
                  \/
</pre>
<p>You can download more fonts from <a target="_blank" href="http://www.figlet.org/fontdb.cgi">here</a>.<br />
The default width of the Figlet output is set to 80 characters, which you can change with the &#8216;outputWidth&#8217; option.</p>

<div class="wp_codebox"><table><tr id="p13024"><td class="code" id="p130code24"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'outputWidth'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">120</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For more options you can take a look at the <a target="_blank" href="http://framework.zend.com/manual/en/zend.text.html">documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/creating-a-figlet-text-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend framework map for version 1.5</title>
		<link>http://www.codediesel.com/php/zend-framework-map/</link>
		<comments>http://www.codediesel.com/php/zend-framework-map/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 06:12:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=8</guid>
		<description><![CDATA[Zend framework is gaining widespread use everyday. Each new version of the framework includes new component additions. The following shows a visual representation of the Zend framework for version 1.5. The following image was created using Microsoft Visio and then exported to PDF. I&#8217;ll be adding a linked map page soon. Click on the image [...]]]></description>
			<content:encoded><![CDATA[<p>Zend framework is gaining widespread use everyday. Each new version of the framework includes new component additions. The following shows a visual representation of the Zend framework for version 1.5.<br />
The following image was created using Microsoft Visio and then exported to PDF. I&#8217;ll be adding a linked map page soon. Click on the image for a larger view, or download a PDF version <a class="pdf" href="http://www.codediesel.com/data/pdf/zend_framework.pdf" target="_blank">here</a>.<span id="more-8"></span></p>
<p><a title="View Large Image" href="http://www.codediesel.com/data/images/zend_map_large.gif" target="_blank"><br />
<img style="vertical-align: middle;" src="http://www.codediesel.com/data/images/zend_map_small.gif" alt="Zend Framework Map" width="477" height="548" /></a></p>
<p><a class="pdf" title="Download" href="http://www.codediesel.com/data/pdf/zend_framework.pdf" target="_blank">Download PDF version (color)</a><br />
<a class="pdf" title="Download" href="http://www.codediesel.com/data/pdf/zend_framework_blackwhite.pdf" target="_blank">Download PDF version (Grayscale)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/zend-framework-map/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Delicious tag cloud using Zend framework</title>
		<link>http://www.codediesel.com/php/delicious-tag-cloud-using-zend-framework/</link>
		<comments>http://www.codediesel.com/php/delicious-tag-cloud-using-zend-framework/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 05:19:43 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=6</guid>
		<description><![CDATA[Zend framework provides many interesting wrappers for major service APIs like Akismet, Amazon, Audioscrobbler, Delicious, Flickr, Nirvanix, etc. In this post we will use the delicious wrapper to create a tag cloud for your delicious account. . Now that we have all the tags with us we will display them as a cloud with weight [...]]]></description>
			<content:encoded><![CDATA[<p>Zend framework provides many interesting wrappers for major service APIs like Akismet, Amazon, Audioscrobbler, Delicious, Flickr, Nirvanix, etc.</p>
<p>In this post we will use the delicious wrapper to create a tag cloud for your delicious account.<span id="more-6"></span></p>
<hr color="#000000" size="1" /> <img src="http://www.codediesel.com/data/images/delicious.gif" alt="sdsd" align="middle" />.<br />
<hr size="1" /> Now that we have all the tags with us we will display them as a cloud with weight attached to each tag. The font size of each tag will be set depending on the number of posts for that particular tags. For e.g a tag named &#8220;php&#8221; having 45 posts will have the font size set to 30 px while a tag &#8220;flash&#8221; having 2 posts will have the font size set to 10 px.</p>
<hr size="1" /> <img src="http://www.codediesel.com/data/images/delicious2.gif" alt="sdsd" align="middle" />.<br />
<hr size="1" />
Here&#8217;s what the output will look like. The tags visited are in blue.</p>
<hr color="#000000" size="1" /> <img src="http://www.codediesel.com/data/images/delicious3.gif" alt="sdsd" align="middle" />.<br />
<hr size="1" />
<a href="http://www.codediesel.com/data/code/delicious.sphp" class="download">Download code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/delicious-tag-cloud-using-zend-framework/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

