Skip to content

Instantly share code, notes, and snippets.

@sadhasivam
Created December 21, 2015 20:03
Show Gist options
  • Save sadhasivam/fe0a40c8283c4316c5d9 to your computer and use it in GitHub Desktop.
Save sadhasivam/fe0a40c8283c4316c5d9 to your computer and use it in GitHub Desktop.
encryp_decrypt.php
<?php
class ShopRunnerCrypt {
protected $config;
public function __construct(array $parameters = []) {
$this->config = $parameters;
}
/**
* Decrypt CC Number from PayRunner payload
*
* @param string
* @return string
*/
protected function decryptEncryptedCc($ccNumber)
{
return $this->config->isApiUseEncryption() ? $this->decrypt($ccNumber) : $ccNumber;
}
/**
* Decrypt encrypted credit card number.
*
* @param string
* @return string
*/
protected function decrypt($encryptedCcNumber)
{
/** @var string */
$decryptedCcNumber = mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
base64_decode($this->config->getApiDecryptionKey()), //Get SR Key
base64_decode($encryptedCcNumber),
MCRYPT_MODE_CBC,
$this->config->getApiDecryptionSeed()
);
return $this->cleanRawCCNumber($decryptedCcNumber);
}
/**
* Clean the decrypted CC Number.
*
* @param string
* @return string
*/
protected function cleanRawCCNumber($ccNumber)
{
$number = '';
for ($i = 0; $i < strlen($ccNumber); $i++) {
$digit = $ccNumber[$i];
if (!is_numeric($digit)) {
break;
}
$number .= $digit;
}
return $number;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment