After taking into account answers for my questions here and here I created (well may-be) improved version of my wrapper. The key issue was what if an attacker is knowing what is encoded - he might then find the key and encode another messages. So I added XOR before encryption. I also in this version prepend IV to the data as was suggested.
SHA256 on key is only for making sure the key is as long as needed for the AES algorithm, but I know that key should not be plain text but calculated with many iterations to prevent dictionary attack
function aes192ctr_en($data,$key) {
$iv = mcrypt_create_iv(24,MCRYPT_DEV_URANDOM);
$xor = mcrypt_create_iv(24,MCRYPT_DEV_URANDOM);
$key = hash_hmac('sha256',$key,$iv,true);
$data = $xor.((string)$data ^ (string)str_repeat($xor,(strlen($data)/24)+1));
$data = hash('md5',$data,true).$data;
return $iv.mcrypt_encrypt('rijndael-192',$key,$data,'ctr',$iv);
}
function aes192ctr_de($data,$key) {
$iv = substr($data,0,24);
$data = substr($data,24);
$key = hash_hmac('sha256',$key,$iv,true);
$data = mcrypt_decrypt('rijndael-192',$key,$data,'ctr',$iv);
$md5 = substr($data,0,16);
$data = substr($data,16);
if (hash('md5',$data,true)!==$md5) return false;
$xor = substr($data,0,24);
$data = substr($data,24);
$data = ((string)$data ^ (string)str_repeat($xor,(strlen($data)/24)+1));
return $data;
}
$encrypted = aes192ctr_en('secret text','password');
echo $encrypted;
echo aes192ctr_de($encrypted,'password');
Another question is: Is CTR mode is okay in this context, would it be better if I use CBC mode instead?
Again, by safe I mean, could an attacker derive the password if he knows which exact text was encrypted, and knows above method. I assume a random and long password here.
Maybe instead of XOR, will it be safer to use random initial data with another run of AES or other simpler algorithm like TEA or trivium?