public static function decrypt($encryptionKey, $encryptedString, $blockType = 'CBC', $urlSafe = false) { if ($urlSafe) { $data = Base64URLSafe::urlsafe_b64decode($encryptedString); } else { $data = base64_decode($encryptedString); } switch ($blockType) { case 'CBC': $cipher = new Rijndael(Rijndael::MODE_CBC); $cipher->setKey($encryptionKey); // Split the IV from the ciphertext $iv = substr($data, 0, $cipher->getBlockLength() >> 3); $cipherText = substr($data, $cipher->getBlockLength() >> 3, strlen($encryptedString)); $cipher->setIV($iv); break; case 'ECB': $cipher = new Rijndael(Rijndael::MODE_ECB); $cipher->setKey($encryptionKey); $cipherText = $data; break; default: throw new \Exception('Unknown encryption blocktype: ' . $blockType, 500); break; } return $cipher->decrypt($cipherText); }
/** * @group github451 */ public function testKeyPaddingRijndael() { // this test case is from the following URL: // https://web.archive.org/web/20070209120224/http://fp.gladman.plus.com/cryptography_technology/rijndael/aesdvec.zip $aes = new Rijndael(); $aes->setPreferredEngine($this->engine); $aes->disablePadding(); $aes->setKey(pack('H*', '2b7e151628aed2a6abf7158809cf4f3c762e7160')); // 160-bit key. Valid in Rijndael. //$this->_checkEngine($aes); // should only work in internal mode $ciphertext = $aes->encrypt(pack('H*', '3243f6a8885a308d313198a2e0370734')); $this->assertEquals($ciphertext, pack('H*', '231d844639b31b412211cfe93712b880')); }
/** * Sets the key. * * Rijndael supports five different key lengths, AES only supports three. * * @see Crypt_Rijndael:setKey() * @see setKeyLength() * @access public * @param string $key */ function setKey($key) { parent::setKey($key); if (!$this->explicit_key_length) { $length = strlen($key); switch (true) { case $length <= 16: $this->key_length = 16; break; case $length <= 24: $this->key_length = 24; break; default: $this->key_length = 32; } $this->_setEngine(); } }
/** * Decrypt the encrypted string using Rijndael algorithm * * @param string $encryptedString Encrypted string * @param string $key Paytm Merchant Key * @return string Plain text */ public function decryptString($encryptedString, $key) { $cipher = new Rijndael(); $cipher->setKey($key); $cipher->iv = $this->iv; $encryptedString = base64_decode($encryptedString); $plaintext = $cipher->decrypt($encryptedString); if (!$plaintext) { throw new InvalidResponseException("Invalid checksum."); } return $plaintext; }