mysql – Read BLOB to string in PHP (Symfony2)-ThrowExceptions

Exception or error:

I have a MYSQL DB where I store encrypted credentials for an IMAP account.
The field for the password is a BLOB as recommended.

        $encrypter = new TripleDES(CRYPT_DES_MODE_ECB);
        $encrypter->setKey($this->container->getParameter('secure_token'));
        $encrytped_pw = $encrypter->encrypt("MYPASSWORD");

        $mailbox->setUrl('{outlook.office365.com:993/imap/ssl}');
        $mailbox->setUsername('user@domain.tld');
        $mailbox->setPassword(base64_decode($encrytped_pw));
        $em->persist($mailbox);
        $em->flush();

Unfortunately I am not able to decrypt this field with this:

    $mailboxPw = $this->getUser()->getMailbox()->getPassword();

    $decrypter = new TripleDES(CRYPT_DES_MODE_ECB);
    $decrypter->setKey($this->container->getParameter('secure_token'));
    $decrytped_pw = $decrypter->decrypt(base64_encode($mailboxPw));

as it gives me

Warning: base64_encode() expects parameter 1 to be string, resource given

I cannot find a way to convert a blob resource to string, is there anotther way to achieve what I want?

Any hint appreciated

How to solve:

The resource might be read with stream_get_contents:

$mailboxPwResource = $this->getUser()->getMailbox()->getPassword();
$mailboxPw = stream_get_contents($mailboxPwResource);

$decrypter = new TripleDES(CRYPT_DES_MODE_ECB);
$decrypter->setKey($this->container->getParameter('secure_token'));
$decrytped_pw = $decrypter->decrypt(base64_encode($mailboxPw));

Leave a Reply

Your email address will not be published. Required fields are marked *