public static function encrypt($encryptionKey, $textInput, $blockType = 'CBC', $urlSafe = false) { switch ($blockType) { case 'CBC': $cipher = new Rijndael(Rijndael::MODE_CBC); $cipher->setKey($encryptionKey); $iv = Random::string($cipher->getBlockLength() >> 3); $cipher->setIV($iv); break; case 'ECB': $cipher = new Rijndael(Rijndael::MODE_ECB); $cipher->setKey($encryptionKey); $iv = ''; break; default: throw new \Exception('Unknown encryption blocktype: ' . $blockType, 500); break; } $encryptedResult = $iv . $cipher->encrypt($textInput); if ($urlSafe) { return Base64URLSafe::urlsafe_b64encode($encryptedResult); } else { return base64_encode($encryptedResult); } }
/** * Encrypt the plain text using Rijndael algorithm * * @param string $plaintext Plain text * @param string $key Paytm Merchant Key * @return string base64 encoded encrypted string */ public function encryptString($plaintext, $key) { $cipher = new Rijndael(); $cipher->setKey($key); $cipher->iv = $this->iv; $encryptedString = $cipher->encrypt($plaintext); return base64_encode($encryptedString); }
/** * @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')); }