I am designing an application that allows end users to take quizzes from their browsers. Part of the requirement is that when the quiz start time is around, the question should be displayed to every participant at once. This makes serving questions to end users from the server less reasonable because this will lead to sudden burst of request so I intend to serve questions to them as soon as they connect and its less than 2 hours to quiz start time. The problem is that since it is a competition, questions should not be seen before start time hence there is need to encrypt it.
I have decided to encrypt in two stages, the first stage of encryption using asymmetric RSA encryption for the exchange of keys which I have successfully done. This key will be used to encrypt any other data that that will be sent between server and client.
The problem now is the symmetric encryption part. I am trying to use openssl_encrypt method to encrypt on the server side and trying to decrypt with pidcrypt (a javascript encryption/decryption library) on the clientside. Turns out the pidcrypt requires your iv (initialization vector) to be eight bytes long however openssl_encrypt using the AES-256-CBC mode doesn't allow eight bytes instead is constantly insisting on 16 bytes. I have done a lot of permutations and experimentation with no luck. It was stated in the documentation of pidcrypt that it is openssl compatible so my question is - What am I doing wrong? Below is the code that encrypts on the server side using PHP
$iv_len = openssl_cipher_iv_length("AES-256-CBC");
$key='My very secret key.......';
$iv = openssl_random_pseudo_bytes($iv_len);
$enc = openssl_encrypt('Hello', "AES-256-CBC", $key, 0, $iv);
$encryptedMessage = base64_encode("Salted__".bin2hex($iv).$enc);
echo json_encode(array('key'=>$key, 'encrypt'=>$encryptedMessage,));
Please is there a way to make $iv_len 8bytes long rather than the 16 bytes that this code constantly return and Am I approaching this whole setup in the right way. Thanks