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); }
/** * 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; }