function cbc_mac($data, $k, $n) { $padding_size = strlen($data) % 16 == 0 ? 0 : 16 - strlen($data) % 16; $data .= str_repeat("", $padding_size); $chunks = get_chunks(strlen($data)); $file_mac = array(0, 0, 0, 0); foreach ($chunks as $pos => $size) { $chunk_mac = array($n[0], $n[1], $n[0], $n[1]); for ($i = $pos; $i < $pos + $size; $i += 16) { $block = str_to_a32(substr($data, $i, 16)); $chunk_mac = array($chunk_mac[0] ^ $block[0], $chunk_mac[1] ^ $block[1], $chunk_mac[2] ^ $block[2], $chunk_mac[3] ^ $block[3]); $chunk_mac = aes_cbc_encrypt_a32($chunk_mac, $k); } $file_mac = array($file_mac[0] ^ $chunk_mac[0], $file_mac[1] ^ $chunk_mac[1], $file_mac[2] ^ $chunk_mac[2], $file_mac[3] ^ $chunk_mac[3]); $file_mac = aes_cbc_encrypt_a32($file_mac, $k); } return $file_mac; }
function encrypt_key($a, $key) { $x = array(); for ($i = 0; $i < count($a); $i += 4) { $x = array_merge($x, aes_cbc_encrypt_a32(array_slice($a, $i, 4), $key)); } return $x; }