123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- <?php
- if (!class_exists('Crypt_Rijndael')) {
- require_once 'Rijndael.php';
- }
- define('CRYPT_AES_MODE_CTR', -1);
- define('CRYPT_AES_MODE_ECB', 1);
- define('CRYPT_AES_MODE_CBC', 2);
- define('CRYPT_AES_MODE_CFB', 3);
- define('CRYPT_AES_MODE_OFB', 4);
- define('CRYPT_AES_MODE_INTERNAL', 1);
- define('CRYPT_AES_MODE_MCRYPT', 2);
- class Crypt_AES extends Crypt_Rijndael {
-
- var $enmcrypt;
-
- var $demcrypt;
-
- var $ecb;
-
- function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
- {
- if ( !defined('CRYPT_AES_MODE') ) {
- switch (true) {
- case extension_loaded('mcrypt') && in_array('rijndael-128', mcrypt_list_algorithms()):
- define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
- break;
- default:
- define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
- }
- }
- switch ( CRYPT_AES_MODE ) {
- case CRYPT_AES_MODE_MCRYPT:
- switch ($mode) {
- case CRYPT_AES_MODE_ECB:
- $this->paddable = true;
- $this->mode = MCRYPT_MODE_ECB;
- break;
- case CRYPT_AES_MODE_CTR:
-
-
-
- $this->mode = 'ctr';
-
- break;
- case CRYPT_AES_MODE_CFB:
- $this->mode = 'ncfb';
- break;
- case CRYPT_AES_MODE_OFB:
- $this->mode = MCRYPT_MODE_NOFB;
- break;
- case CRYPT_AES_MODE_CBC:
- default:
- $this->paddable = true;
- $this->mode = MCRYPT_MODE_CBC;
- }
- break;
- default:
- switch ($mode) {
- case CRYPT_AES_MODE_ECB:
- $this->paddable = true;
- $this->mode = CRYPT_RIJNDAEL_MODE_ECB;
- break;
- case CRYPT_AES_MODE_CTR:
- $this->mode = CRYPT_RIJNDAEL_MODE_CTR;
- break;
- case CRYPT_AES_MODE_CFB:
- $this->mode = CRYPT_RIJNDAEL_MODE_CFB;
- break;
- case CRYPT_AES_MODE_OFB:
- $this->mode = CRYPT_RIJNDAEL_MODE_OFB;
- break;
- case CRYPT_AES_MODE_CBC:
- default:
- $this->paddable = true;
- $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
- }
- }
- if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
- parent::Crypt_Rijndael($this->mode);
- }
- }
-
- function setBlockLength($length)
- {
- return;
- }
-
- function setIV($iv)
- {
- parent::setIV($iv);
- if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
- $this->changed = true;
- }
- }
-
- function encrypt($plaintext)
- {
- if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
- $this->_mcryptSetup();
-
-
-
- if ($this->mode == 'ncfb' && $this->continuousBuffer) {
- $iv = &$this->encryptIV;
- $pos = &$this->enbuffer['pos'];
- $len = strlen($plaintext);
- $ciphertext = '';
- $i = 0;
- if ($pos) {
- $orig_pos = $pos;
- $max = 16 - $pos;
- if ($len >= $max) {
- $i = $max;
- $len-= $max;
- $pos = 0;
- } else {
- $i = $len;
- $pos+= $len;
- $len = 0;
- }
- $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
- $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
- $this->enbuffer['enmcrypt_init'] = true;
- }
- if ($len >= 16) {
- if ($this->enbuffer['enmcrypt_init'] === false || $len > 280) {
- if ($this->enbuffer['enmcrypt_init'] === true) {
- mcrypt_generic_init($this->enmcrypt, $this->key, $iv);
- $this->enbuffer['enmcrypt_init'] = false;
- }
- $ciphertext.= mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % 16));
- $iv = substr($ciphertext, -16);
- $len%= 16;
- } else {
- while ($len >= 16) {
- $iv = mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, 16);
- $ciphertext.= $iv;
- $len-= 16;
- $i+= 16;
- }
- }
- }
- if ($len) {
- $iv = mcrypt_generic($this->ecb, $iv);
- $block = $iv ^ substr($plaintext, -$len);
- $iv = substr_replace($iv, $block, 0, $len);
- $ciphertext.= $block;
- $pos = $len;
- }
- return $ciphertext;
- }
- if ($this->paddable) {
- $plaintext = $this->_pad($plaintext);
- }
- $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
- if (!$this->continuousBuffer) {
- mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
- }
- return $ciphertext;
- }
- return parent::encrypt($plaintext);
- }
-
- function decrypt($ciphertext)
- {
- if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
- $this->_mcryptSetup();
- if ($this->mode == 'ncfb' && $this->continuousBuffer) {
- $iv = &$this->decryptIV;
- $pos = &$this->debuffer['pos'];
- $len = strlen($ciphertext);
- $plaintext = '';
- $i = 0;
- if ($pos) {
- $orig_pos = $pos;
- $max = 16 - $pos;
- if ($len >= $max) {
- $i = $max;
- $len-= $max;
- $pos = 0;
- } else {
- $i = $len;
- $pos+= $len;
- $len = 0;
- }
-
- $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
- $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
- }
- if ($len >= 16) {
- $cb = substr($ciphertext, $i, $len - $len % 16);
- $plaintext.= mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
- $iv = substr($cb, -16);
- $len%= 16;
- }
- if ($len) {
- $iv = mcrypt_generic($this->ecb, $iv);
- $plaintext.= $iv ^ substr($ciphertext, -$len);
- $iv = substr_replace($iv, substr($ciphertext, -$len), 0, $len);
- $pos = $len;
- }
- return $plaintext;
- }
- if ($this->paddable) {
-
-
- $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));
- }
- $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
- if (!$this->continuousBuffer) {
- mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
- }
- return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
- }
- return parent::decrypt($ciphertext);
- }
-
- function _mcryptSetup()
- {
- if (!$this->changed) {
- return;
- }
- if (!$this->explicit_key_length) {
-
- $length = strlen($this->key) >> 2;
- if ($length > 8) {
- $length = 8;
- } else if ($length < 4) {
- $length = 4;
- }
- $this->Nk = $length;
- $this->key_size = $length << 2;
- }
- switch ($this->Nk) {
- case 4:
- $this->key_size = 16;
- break;
- case 5:
- case 6:
- $this->key_size = 24;
- break;
- case 7:
- case 8:
- $this->key_size = 32;
- }
- $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0));
- $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));
- if (!isset($this->enmcrypt)) {
- $mode = $this->mode;
-
- $this->demcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
- $this->enmcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
- if ($mode == 'ncfb') {
- $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
- }
- }
- mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
- mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
- if ($this->mode == 'ncfb') {
- mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
- }
- $this->changed = false;
- }
-
- function enableContinuousBuffer()
- {
- parent::enableContinuousBuffer();
- if (CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT) {
- $this->enbuffer['enmcrypt_init'] = true;
- $this->debuffer['demcrypt_init'] = true;
- }
- }
-
- function disableContinuousBuffer()
- {
- parent::disableContinuousBuffer();
- if (CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT) {
- mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
- mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
- }
- }
- }
|