PayPalSecurityCipher

Class Cipher.

Defined (1)

The class is defined in the following location(s).

/classes/lib/PayPal/Security/Cipher.php  
  1. class Cipher 
  2. private $secretKey; 
  3.  
  4. /** 
  5. * Fixed IV Size 
  6. */ 
  7. const IV_SIZE = 16; 
  8.  
  9. function __construct($secretKey) 
  10. $this->secretKey = $secretKey; 
  11.  
  12. /** 
  13. * Encrypts the input text using the cipher key 
  14. * @param $input 
  15. * @return string 
  16. */ 
  17. function encrypt($input) 
  18. // Create a random IV. Not using mcrypt to generate one, as to not have a dependency on it. 
  19. $iv = substr(uniqid("", true), 0, Cipher::IV_SIZE); 
  20. // Encrypt the data 
  21. $encrypted = openssl_encrypt($input, "AES-256-CBC", $this->secretKey, 0, $iv); 
  22. // Encode the data with IV as prefix 
  23. return base64_encode($iv . $encrypted); 
  24.  
  25. /** 
  26. * Decrypts the input text from the cipher key 
  27. * @param $input 
  28. * @return string 
  29. */ 
  30. function decrypt($input) 
  31. // Decode the IV + data 
  32. $input = base64_decode($input); 
  33. // Remove the IV 
  34. $iv = substr($input, 0, Cipher::IV_SIZE); 
  35. // Return Decrypted Data 
  36. return openssl_decrypt(substr($input, Cipher::IV_SIZE), "AES-256-CBC", $this->secretKey, 0, $iv);