<?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>Ankur&#039;s PHP and Javascript blog &#187; MCrypt</title>
	<atom:link href="http://www.ankur.com/blog/tag/mcrypt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ankur.com/blog</link>
	<description>PHP and javascript blog</description>
	<lastBuildDate>Tue, 10 Nov 2009 14:28:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AES wrapper class with pure php and MCrypt extension support</title>
		<link>http://www.ankur.com/blog/76/php/aes-wrapper-class-pure-php-mcrypt-extension-support/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=aes-wrapper-class-pure-php-mcrypt-extension-support</link>
		<comments>http://www.ankur.com/blog/76/php/aes-wrapper-class-pure-php-mcrypt-extension-support/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 16:57:49 +0000</pubDate>
		<dc:creator>Ankur Motreja</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[MCrypt]]></category>

		<guid isPermaLink="false">http://www.ankur.com/blog/?p=76</guid>
		<description><![CDATA[The code below is a wrapper class around the AES-128 implementation at http://www.phpclasses.org/browse/package/3650.html and the MCrypt extension. It use the MCrypt extension if it is enabled and use the pure PHP AES-128 class otherwise.


function hex2bin($hex) {

	return pack ( &#34;H*&#34;, $hex );

}

class Cipher {

	public $password;

	public $plainText;

	public $encryptedText;

	public $iv;

	public $mode;

	const AES_128_ECB = &#34;AES_128_ECB&#34;;

	const AES_128_CBC = &#34;AES_128_CBC&#34;;

	public function [...]


Related posts:<ol><li><a href='http://www.ankur.com/blog/68/php/curl-wrapper-class-cli-php-extension-support/' rel='bookmark' title='Permanent Link: cURL wrapper class with executable and PHP extension support'>cURL wrapper class with executable and PHP extension support</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>The code below is a wrapper class around the AES-128 implementation at <a rel="nofollow" rel="nofollow" href="http://www.phpclasses.org/browse/package/3650.html">http://www.phpclasses.org/browse/package/3650.html</a> and the MCrypt extension. It use the MCrypt extension if it is enabled and use the pure PHP AES-128 class otherwise.</p>
<p><span id="more-76"></span></p>
<pre class="brush: php; collapse: true; light: false; toolbar: true;">
function hex2bin($hex) {

	return pack ( &quot;H*&quot;, $hex );

}

class Cipher {

	public $password;

	public $plainText;

	public $encryptedText;

	public $iv;

	public $mode;

	const AES_128_ECB = &quot;AES_128_ECB&quot;;

	const AES_128_CBC = &quot;AES_128_CBC&quot;;

	public function __construct() {

		$this-&gt;mode = self::AES_128_ECB;

	}

	public function encrypt($useSuppliedIV = false) {

		if (!$this-&gt;plainText) {
			return &quot;&quot;;
		}

		switch ($this-&gt;mode) {

			case self::AES_128_ECB :
			case self::AES_128_CBC :
				if (strlen($this-&gt;password) &gt; 16) {
					$this-&gt;password = substr($this-&gt;password, 0, 16);
				}
				break;

		}

		if (function_exists ( &quot;mcrypt_module_open&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_create_iv&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_generic_init&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_generic&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_generic_deinit&quot; )) {

			switch ($this-&gt;mode) {

				case self::AES_128_ECB :
					$td = mcrypt_module_open ( 'rijndael-128', '', 'ecb', '' );
					break;

				case self::AES_128_CBC :
					$td = mcrypt_module_open ( 'rijndael-128', '', 'cbc', '' );
					break;

			}

			if ($useSuppliedIV) {
				if (strlen ( $this-&gt;iv ) != mcrypt_enc_get_iv_size ( $td )) {
					return null;
				}
			} else {
				$this-&gt;iv = mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $td ), MCRYPT_RAND );
			}

			mcrypt_generic_init ( $td, $this-&gt;password, $this-&gt;iv );
			$this-&gt;encryptedText = mcrypt_generic ( $td, $this-&gt;plainText );
			mcrypt_generic_deinit ( $td );

		} else {

			$aes = new AES128 ( );
			$key = $aes-&gt;makeKey ( $this-&gt;password );

			$plainTextArray = str_split ( $this-&gt;plainText, 16 );

			$lastIndex = count($plainTextArray) - 1;
			if (strlen($plainTextArray[$lastIndex]) != 16) {

				for ($i = strlen($plainTextArray[$lastIndex]); $i &lt; 16; $i++) {
					$plainTextArray[$lastIndex] .= chr (0);
				}

			}

			$this-&gt;encryptedText = &quot;&quot;;

			if ($useSuppliedIV) {
				if (strlen ( $this-&gt;iv ) != 16) {
					return null;
				}
			} else {
				$this-&gt;iv = &quot;&quot;;
				for ($i = 0; $i &lt; 16; $i++) {
					$this-&gt;iv .= chr (rand(0, 255));
				}
			}

			switch ($this-&gt;mode) {

				case self::AES_128_ECB :

					for($i = 0; $i &lt; count ( $plainTextArray ); $i ++) {
						$this-&gt;encryptedText .= $aes-&gt;blockEncrypt ( $plainTextArray [$i], $key );
					}

					break;

				case self::AES_128_CBC :
					$encryptedTextArray = array();
					for ($i = 0; $i &lt; count ( $plainTextArray ); $i ++) {
						if ($i == 0) {
							$input = $this-&gt;iv ^ $plainTextArray[$i];
						} else {
							$input = $encryptedTextArray[$i - 1] ^ $plainTextArray[$i];
						}

						$encryptedTextArray[$i] = $aes-&gt;blockEncrypt ( $input, $key );
					}

					$this-&gt;encryptedText = join(&quot;&quot;, $encryptedTextArray);

					break;
			}
		}

	}

	public function decrypt() {
		if (!$this-&gt;encryptedText) {
			return &quot;&quot;;
		}

		if (function_exists ( &quot;mcrypt_module_open&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_create_iv&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_enc_get_iv_size&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_generic_init&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_generic&quot; ) &amp;&amp; function_exists ( &quot;mcrypt_generic_deinit&quot; )) {

			switch ($this-&gt;mode) {

				case self::AES_128_ECB :
					$td = mcrypt_module_open ( 'rijndael-128', '', 'ecb', '' );
					$this-&gt;iv = mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $td ), MCRYPT_RAND );
					break;

				case self::AES_128_CBC :
					$td = mcrypt_module_open ( 'rijndael-128', '', 'cbc', '' );
					if (strlen ( $this-&gt;iv ) != mcrypt_enc_get_iv_size ( $td )) {
						return null;
					}
					break;

			}

			mcrypt_generic_init ( $td, $this-&gt;password, $this-&gt;iv );
			$this-&gt;plainText = mdecrypt_generic ( $td, $this-&gt;encryptedText );
			mcrypt_generic_deinit ( $td );

		} else {

			$aes = new AES128 ( );
			$key = $aes-&gt;makeKey ( $this-&gt;password );

			$encryptedTextArray = str_split ( bin2hex ( $this-&gt;encryptedText ), 32 );

			$this-&gt;plainText = &quot;&quot;;

			switch ($this-&gt;mode) {

				case self::AES_128_ECB :
					for($i = 0; $i &lt; count ( $encryptedTextArray ); $i ++) {
						$this-&gt;plainText .= $aes-&gt;blockDecrypt ( hex2bin ( $encryptedTextArray [$i] ), $key );
					}
					break;

				case self::AES_128_CBC :
					if (strlen ( $this-&gt;iv ) != 16) {
						return null;
					}

					$plainTextArray = array();
					for ($i = 0; $i &lt; count ( $encryptedTextArray ); $i ++) {
						$output = $aes-&gt;blockDecrypt ( hex2bin($encryptedTextArray[$i]), $key );

						if ($i == 0) {
							$plainTextArray[$i] = $this-&gt;iv ^ $output;
						} else {
							$plainTextArray[$i] = hex2bin($encryptedTextArray[$i - 1]) ^ $output;
						}

						$this-&gt;plainText = join(&quot;&quot;, $plainTextArray);
					}
					break;

			}

		}

		$this-&gt;plainText = trim ( $this-&gt;plainText );

	}

}
</pre>
<p>Save the code above to cipher.php.  In addition to this, you will need the AES128.php file from <a rel="nofollow" rel="nofollow" href="http://www.phpclasses.org/browse/package/3650.html">http://www.phpclasses.org/browse/package/3650.html</a></p>
<p>Here&#8217;s an example of AES-128 ECB mode encryption:</p>
<pre class="brush: php; collapse: true; light: false; toolbar: true;">
include_once (&quot;AES128.php&quot;);
include_once (&quot;cipher.php&quot;);

$encrypt = new Cipher;
$encrypt-&gt;password = &quot;password&quot;;
$encrypt-&gt;plainText = &quot;this is a very long string that we would like to encrypt using AES128&quot;;
$encrypt-&gt;encrypt();

echo &quot;encrypted: &quot; . bin2hex( $encrypt-&gt;encryptedText ) . &quot;&lt;br&gt;&quot;;

$decrypt = new Cipher;
$decrypt-&gt;password = &quot;password&quot;;
$decrypt-&gt;encryptedText = $encrypt-&gt;encryptedText;
$decrypt-&gt;decrypt();

echo &quot;plain text: &quot; . $decrypt-&gt;plainText;
</pre>
<p>The default mode is ECB. So, we don&#8217;t need to set $encrypt-&gt;mode and $decrypt-&gt;mode to Cipher::AES_128_ECB .</p>
<p>Here&#8217;s an example of AES-128 CBC mode encryption using an initialization vector:</p>
<pre class="brush: php; collapse: true; light: false; toolbar: true;">
include_once (&quot;AES128.php&quot;);
include_once (&quot;cipher.php&quot;);

$encrypt = new Cipher;
$encrypt-&gt;mode = Cipher::AES_128_CBC;
$encrypt-&gt;password = &quot;password&quot;;
$encrypt-&gt;plainText = &quot;this is a very long string that we would like to encrypt using AES128&quot;;
$encrypt-&gt;encrypt();

echo &quot;iv: &quot; . bin2hex( $encrypt-&gt;iv ) . &quot;&lt;br&gt;&quot;;
echo &quot;encrypted: &quot; . bin2hex( $encrypt-&gt;encryptedText ) . &quot;&lt;br&gt;&quot;;

$decrypt = new Cipher;
$decrypt-&gt;mode = Cipher::AES_128_CBC;
$decrypt-&gt;iv = $encrypt-&gt;iv;
$decrypt-&gt;password = &quot;password&quot;;
$decrypt-&gt;encryptedText = $encrypt-&gt;encryptedText;
$decrypt-&gt;decrypt();

echo &quot;plain text: &quot; . $decrypt-&gt;plainText;
</pre>


<p>Related posts:<ol><li><a href='http://www.ankur.com/blog/68/php/curl-wrapper-class-cli-php-extension-support/' rel='bookmark' title='Permanent Link: cURL wrapper class with executable and PHP extension support'>cURL wrapper class with executable and PHP extension support</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.ankur.com/blog/76/php/aes-wrapper-class-pure-php-mcrypt-extension-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
