I got it working. It appears that php's mcrypt uses zero padding of the
plain text that is to be encrypted by default while Crypto++ appears to use
PKCS. After padding the plain text using PKCS in php, I was able to
decrypt my data correctly using Crypto++.
I hope this helps anyone suffering from the same issue in the future, as I
know other have suffered from in the past without solution.
Here's some PHP code that handles the padding which I found online.
function addpadding($string, $blocksize = 16)
{
$len = strlen($string);
$pad = $blocksize - ($len % $blocksize);
$string .= str_repeat(chr($pad), $pad);
return $string;
}
function strippadding($string)
{
$slast = ord(substr($string, -1));
$slastc = chr($slast);
$pcheck = substr($string, -$slast);
if(preg_match("/$slastc{".$slast."}/", $string))
{
$string = substr($string, 0, strlen($string)-$slast);
return $string;
}
else
{
return false;
}
}
Cheers,
André
On Friday, August 9, 2013 4:44:57 PM UTC-7, andre roy wrote:
>
> Hey Folks,
>
> I'm trying to use Crypto++ in conjunction with php's mcrypt on a server.
> I'm using Rijndael 128 bit with CBC in both crypto++ and mcrypt. Using the
> same key and IV, I can encrypt a message in crypto++, pass the encrypted
> string to my php script and have mcrypt decrypt it just fine. However,
> taking a plain string in php, encrypting it, and passing it to crypto++
> causes crypto++ crashes, looks like it's trying to access a bad pointer or
> unallocated memory.
>
> The base64 encoded IV matches in both php and c++, however if I encrypt
> the same string via both crypto++ and mcrypt, the respective base64 encoded
> encrypted strings do not match. I suspect this may be a symptom of my
> problem.
>
> Does anyone know where the incompatibility between taking an mcrypt
> encrypted string and decrypting it with crypto++ comes from? I find it
> strange that it works from crypto++ to mcrypt but not vice versa... On a
> side note, I can take an mcrypt encrypted string and decrypt it in C#, so
> I'm assuming there's some odd issue with my crypto++ setup.
>
> Without posting too much code, in PHP, I'm encrypting using:
>
> $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted,
> MCRYPT_MODE_CBC, $iv);
>
> In C++ I'm encrypting using:
> this->cbcDecryption = new CryptoPP::CBC_Mode< CryptoPP::Rijndael
> >::Decryption( key, CryptoPP::Rijndael::DEFAULT_KEYLENGTH, iv );
> CryptoPP::StreamTransformationFilter stfDecryptor((*this->cbcDecryption),
> new CryptoPP::StringSink( decryptedString ) );
> stfDecryptor.Put( reinterpret_cast<const unsigned char*>(
> encryptedString.c_str() ), encryptedString.size() );
> stfDecryptor.MessageEnd();
>
> Can anyone offer any advice/insight?
>
> Thanks,
>
> André
>
--
--
You received this message because you are subscribed to the "Crypto++ Users"
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at
http://www.cryptopp.com.
---
You received this message because you are subscribed to the Google Groups
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.