class Cryptography {
public function encrypt($string) {
$ivlen = openssl_cipher_iv_length($cipher='AES-128-CBC');
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($string, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
return base64_encode($iv.$hmac.$ciphertext_raw);
}
public function decrypt($encryptedString) {
$c = $encryptedString;
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len=32);
$ciphertext_raw = substr($c, $ivlen+$sha2len);
$decryptedString = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
if (hash_equals($hmac, $calcmac)) {
return $decryptedString;
}
}
}
это только один из результатов шифрования.
3cV/FVGk/gpIofUs5GL3DTid8FOZTXPivxat7+SZG+ARqPWiRUMazeq2cOJpCjzmVzN8arFcl7VDSC6nFvh1CA==
как мне уменьшить его примерно наполовину, но шифрование и дешифрование все еще работает.