Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issues trying to get openssl_encrypt to work

Tags:

php

openssl

I'm writing a class to handle encrypted data, essentially it will be used to encrypt data to be stored in a DB and then again to decrypt it on retrieval.

Here's what I've written:

    class dataEncrypt {

        private $encryptString;
        private $decryptString;
        private $encryptionMethod;
        private $key;

        public function __construct() {

            /* IMPORTANT - DONT CHANGE OR DATA WILL DAMAGE */
            $this->key = sha1('StringToHash');

            // Set the encryption type
            $this->encryptionMethod = "AES-256-CBC";

        }

        // Generate the IV key
        private function generateIV() {

            $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
            return mcrypt_create_iv($ivSize, MCRYPT_RAND);
        }

        // Retrieve the key
        private function retrieveKey() {

            return $key;
        }

        // Encrypt a string
        public function encryptString($string) {

            // Return the encrypted value for storage
            return openssl_encrypt($string, $this->encryptionMethod, $this->retrieveKey(), 0, $this->generateIV());
        }

        // Decrypt a string
        public function decryptString($data) {

            // return the decrypted data
            return openssl_decrypt($data, $this->encryptionMethod, $this->retrieveKey(), 0, $this->generateIV());

            return false;

        }

    }

I'm trying to encrypt a string before storing, and I get the following PHP warning:

Warning: openssl_encrypt(): IV passed is 32 bytes long which is longer than the 16 expected by selected cipher, truncating in /var/www/blahblah... on line xxx

I've googled this, Ive googled the IV functions, I can't find sweetheat on either. Any advice is welcomed here.

Thanks

like image 813
ThePHPUnicorn Avatar asked Jul 22 '13 16:07

ThePHPUnicorn


People also ask

What is openssl_ encrypt in php?

In PHP, Encryption and Decryption of a string is possible using one of the Cryptography Extensions called OpenSSL function for encrypt and decrypt. openssl_encrypt() Function: The openssl_encrypt() function is used to encrypt the data.

How does OpenSSL generate salt and IV?

OpenSSL uses a salted key derivation algorithm. The salt is a piece of random bytes generated when encrypting, stored in the file header; upon decryption, the salt is retrieved from the header, and the key and IV are re-computed from the provided password and salt.


1 Answers

I was able to get it working by passing MCRYPT_CAST_256 rather than MCRYPT_RIJNDAEL_256 into mcrypt_get_iv_size

Encrypt:

$iv_size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

$encrypted = openssl_encrypt($string, "AES-256-CBC", $key, 0, $iv);
$encrypted = $iv.$encrypted;

Decrypt

$iv_size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CBC);
$iv = substr($string, 0, $iv_size);

$decrypted = openssl_decrypt(substr($string, $iv_size), "AES-256-CBC", $key, 0, $iv);
like image 94
ChrisKnowles Avatar answered Oct 06 '22 01:10

ChrisKnowles