note 109598 added to function.openssl-encrypt

From: Date: Wed, 01 Aug 2012 02:35:56 +0000
Subject: note 109598 added to function.openssl-encrypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-189542@lists.php.net to get a copy of this message
Might be useful to people trying to use 'aes-256-cbc' cipher (and probably other cbc ciphers) in collaboration with other implementations of AES (C libs for example) that the openssl extension has a strict implementation regarding padding bytes. I found the solution only by manually going through the openssl source. In C, you would want to pad plaintexts the following way (assuming all mem allocations are proper): nPadding = ( 16 - ( bufferSize % 16 ) ) ? ( 16 - ( bufferSize % 16 ) ) : 16; for( index = bufferSize; index < bufferSize + nPadding; index++ ) { plaintext[ index ] = (char)nPadding; } while decryptions are validated like: isSuccess = TRUE; for( index = bufferSize - 1; index > ( bufferSize - nPadding ); index-- ) { if( plaintext[ index ] != nPadding ) { isSuccess = FALSE; break; } } decryptedSize = bufferSize - nPadding; In plain english, the buffer must be padded up to blockSize. If the buffer is already a multiple of blockSize, you add an entire new blockSize bytes as padding. The value of the padding bytes MUST be the number of padding bytes as a byte... So 5 bytes of padding will result in the following bytes added at the end of the ciphertext: [ 0x05 ][ 0x05 ][ 0x05 ][ 0x05 ][ 0x05 ] Hope this saves someone else a few hours of their life. ---- Server IP: 72.55.140.6 Probable Submitter: 76.10.149.139 ---- Manual Page -- http://www.php.net/manual/en/function.openssl-encrypt.php Edit -- https://master.php.net/note/edit/109598 Del: integrated -- https://master.php.net/note/delete/109598/integrated Del: useless -- https://master.php.net/note/delete/109598/useless Del: bad code -- https://master.php.net/note/delete/109598/bad+code Del: spam -- https://master.php.net/note/delete/109598/spam Del: non-english -- https://master.php.net/note/delete/109598/non-english Del: in docs -- https://master.php.net/note/delete/109598/in+docs Del: other reasons-- https://master.php.net/note/delete/109598 Reject -- https://master.php.net/note/reject/109598 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#189542) next »