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==
我怎样才能减少它的一半,但加密和解密仍然有效。
答案 0 :(得分:2)
有几种方法可以减小密文的大小:
除了可能的第一个选项之外,任何一个选项都不会将密文减少到较大邮件原始文件的50%。只创建一个较小的明文 - 结果当然是未知的 - 或者删除基数64会直接影响实际密文占用的字节数。密文与CBC的明文和大多数其他加密模式线性增长。
删除base 64将使密文大小减少25%,并可能减少填充/开销的几个字节。但是你必须处理由具有随机值的字节组成的二进制密文。
估计节省的表格:
注意: