Sunday, 15 June 2014

php - Incompatible AES implementation between Botan and phpseclib -



php - Incompatible AES implementation between Botan and phpseclib -

i'm using botan library aes encryption/decryption in c++. cannot utilize output of botan in phpseclib accurate results. appreciate if points me working code interoperability between botan , phpseclib or other php encryption library. thanks!

example of encryption botan in c++

// key std::auto_ptr<botan::hashfunction> thash ( botan::get_hash("sha-256") ); std::string mykey = "test"; botan::securevector<botan::byte> tsecvector(32); tsecvector.set(thash->process(mykey)); //the hash key - same size botan::symmetrickey key(tsecvector); // iv botan::initializationvector iv(mrng, 16); // encryption & encode botan::pipe pipe(botan::get_cipher("aes-256/cbc", key, iv, botan::encryption) ); pipe.process_msg(pstdstringtexttoencrypt); botan::pipe pipeb64enc(new botan::base64_encoder ); pipeb64enc.process_msg(pipe.read_all(0)); std::string strbase64encoded = pipeb64enc.read_all_as_string(0); // homecoming preturnencryptedtext = iv.as_string() + strbase64encoded;

example of decryption in php using phpseclib library:

include('crypt/aes.php'); $aes = new crypt_aes(crypt_aes_mode_cbc); //mcrypt used //decrypt request application. [iv 32 chars in hex] [base64 encrypted text] $aes->setkeylength(256); $key = hash('sha256','test', true) ; // true output raw binary output $aes->setkey($key); //iv $iv = hex2bin (substr($_post['enc'],0,32) ); $aes->setiv( $iv ); // encrypted text in binary $encryptedtextbin = base64_decode(substr($_post['enc'],32)); $decryptedrequest = $aes->decrypt( $encryptedtextbin ); echo $decryptedrequest; //no match

i tried mcrypt in php straight no success:

$decrypted_data=""; //128 hack shown on: http://kix.in/2008/07/22/aes-256-using-php-mcrypt/ $td = mcrypt_module_open(mcrypt_rijndael_128, '', mcrypt_mode_cbc, ''); mcrypt_generic_init($td, $key, $iv); $decrypted_data = mdecrypt_generic($td, $encryptedtext); mcrypt_generic_deinit($td); mcrypt_module_close($td); edit:

i tested in 128 bit both botan , phpseclib , proper decryption in 50% of cases. weird. tested different padding modes in botan (cts,pkcs7,oneandzeros,x9.23) 1 time again success in 50% of attempts.

it'd help if posted sample of key you're using botan, password (ie. pre-hashing), iv you're using , plaintext you're using / ciphertext you're getting. that'd allow people test various possibilities instead of having on behalf.

anyway, first guess botan maybe doesn't pad default whereas phpseclib assumes, default, plaintext has been padded.

php cryptography aes phpseclib botan

No comments:

Post a Comment