记录一次和外部java对接对方提供的aeskey是32位字符串
正常来说aes-128-cbc密钥长度是16位,结果他们提供的是32位,仔细观察发现是16进制,需要转化成二进制后就是16位了,然后就可以使用。
$mingwen = '{"staffNo":"00123456","staffName":"测试人员","timestamp":"1690941587475"}'; $key = hex2bin("7f6bcf26b98d0a447daedef492f705af"); $iv = hex2bin("a1e742db68612d64e4e14fa0c988a0bd"); var_dump($key); var_dump($iv); $method = "AES-128-CBC"; echo $miwen = strToHex(openssl_encrypt($mingwen, $method, $key, OPENSSL_PKCS1_PADDING, $iv)); echo $mingwen = openssl_decrypt(hexToStr($miwen), $method, $key, OPENSSL_PKCS1_PADDING, $iv); die; //函数将输入的十六进制字符串转换为普通字符串 function hexToStr($hex) { $string = ""; for ($i = 0; $i < strlen($hex) - 1; $i += 2) $string .= chr(hexdec($hex[$i] . $hex[$i + 1])); return $string; } // 函数将输入的普通字符串转换为十六进制字符串 function strToHex($string) { $hex = ""; $tmp = ""; for ($i = 0; $i < strlen($string); $i++) { $tmp = dechex(ord($string[$i])); $hex .= strlen($tmp) == 1 ? "0" . $tmp : $tmp; } $hex = strtoupper($hex); return $hex; }
版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。
本文链接:记录一次和外部java对接对方提供的aeskey是32位字符串 - https://www.wlphp.com/?post=408