重点:在PHP中,需要使7及以上版本才行
两种方式如下:
第一种:使用系统自带方法做sm4加密,代码如下:
// 使用PHP系统方法,对字符串做sm4加密处理// 要加密的明文$plaintext = 'QCzqMc5n';// 双方约定好的密钥,转换16进制的密钥为二进制格式后,参与加密$key = hex2bin('这里是十六进制的字符串');$ciphertext = openssl_encrypt($plaintext, 'sm4-ecb', $key, OPENSSL_RAW_DATA);// 加密后的字符串,一定要将二进制的转成十六进制的,才与java加密后的一制$str = bin2hex($ciphertext);echo $str;exit;
第二种:使用自定义类做sm4加密
sM4KeySchedule($key); $bytes = $this->pad($data, $this->_block_size); $chunks = array_chunk($bytes, $this->_block_size); $ciphertext = ""; foreach ($chunks as $chunk) { $ciphertext .= $this->sM4Encrypt($chunk); } return base64_encode($ciphertext); } public function decrypt($key, $data) { $data = base64_decode($data); if (strlen($data) < 0 || strlen($data) % $this->_block_size != 0) { return false; } $this->sM4KeySchedule($key); $bytes = unpack("C*", $data); $chunks = array_chunk($bytes, $this->_block_size); $plaintext = ""; foreach ($chunks as $chunk) { $plaintext .= substr($this->sM4Decrypt($chunk), 0, 16); } $plaintext = $this->un_pad($plaintext); return $plaintext; } private function sM4Decrypt($cipherText) { $x = []; for ($j = 0; $j < 4; $j++) { $x[$j] = ($cipherText[$j * 4] << 24) | ($cipherText[$j * 4 + 1] << 16) | ($cipherText[$j * 4 + 2] << 8) | ($cipherText[$j * 4 + 3]); } for ($i = 0; $i < 32; $i++) { $tmp = $x[$i + 1] ^ $x[$i + 2] ^ $x[$i + 3] ^ $this->_rk[31 - $i]; $buf = (self::SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 | (self::SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::SM4_SBOX[$tmp & 0xFF]); $x[$i + 4] = $x[$i] ^ ($buf ^ $this->sm4Rotl32(($buf), 2) ^ $this->sm4Rotl32(($buf), 10) ^ $this->sm4Rotl32(($buf), 18) ^ $this->sm4Rotl32(($buf), 24)); } $plainText = []; for ($k = 0; $k < 4; $k++) { $plainText[4 * $k] = ($x[35 - $k] >> 24) & 0xFF; $plainText[4 * $k + 1] = ($x[35 - $k] >> 16) & 0xFF; $plainText[4 * $k + 2] = ($x[35 - $k] >> 8) & 0xFF; $plainText[4 * $k + 3] = ($x[35 - $k]) & 0xFF; } return $this->bytesToString($plainText); } private function sM4Encrypt($plainText) { $x = []; for ($j = 0; $j < 4; $j++) { $x[$j] = ($plainText[$j * 4] << 24) | ($plainText[$j * 4 + 1] << 16) | ($plainText[$j * 4 + 2] << 8) | ($plainText[$j * 4 + 3]); } for ($i = 0; $i < 32; $i++) { $tmp = $x[$i + 1] ^ $x[$i + 2] ^ $x[$i + 3] ^ $this->_rk[$i]; $buf = (self::SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 | (self::SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::SM4_SBOX[$tmp & 0xFF]); $x[$i + 4] = $x[$i] ^ ($buf ^ $this->sm4Rotl32(($buf), 2) ^ $this->sm4Rotl32(($buf), 10) ^ $this->sm4Rotl32(($buf), 18) ^ $this->sm4Rotl32(($buf), 24)); } $cipherText = []; for ($k = 0; $k < 4; $k++) { $cipherText[4 * $k] = ($x[35 - $k] >> 24) & 0xFF; $cipherText[4 * $k + 1] = ($x[35 - $k] >> 16) & 0xFF; $cipherText[4 * $k + 2] = ($x[35 - $k] >> 8) & 0xFF; $cipherText[4 * $k + 3] = ($x[35 - $k]) & 0xFF; } return $this->bytesToString($cipherText); } private function stringToBytes($string) { return unpack('C*', $string); } private function bytesToString($bytes) { return vsprintf(str_repeat('%c', count($bytes)), $bytes); } private function pad($data) { $bytes = $this->stringToBytes($data); $rem = $this->_block_size - count($bytes) % $this->_block_size; for ($i = 0; $i < $rem; $i++) { array_push($bytes, $rem); } return $bytes; } private function un_pad($data) { $bytes = $this->stringToBytes($data); $rem = isset($bytes[count($bytes)]) ? $bytes[count($bytes)] : ''; $bytes = array_slice($bytes, 0, count($bytes) - $rem); return $this->bytesToString($bytes); } private function sm4Rotl32($buf, $n) { return (($buf << $n) & 0xffffffff) | ($buf >> (32 - $n)); } private function sM4KeySchedule($key) { $this->_rk = []; $key = array_values(unpack("C*", $key)); $k = []; for ($i = 0; $i < 4; $i++) {$a = isset($key[4 * $i]) ? $key[4 * $i] : '';$b = isset($key[4 * $i + 1]) ? $key[4 * $i + 1] : '';$c = isset($key[4 * $i + 2]) ? $key[4 * $i + 2] : '';$d = isset($key[4 * $i + 3]) ? $key[4 * $i + 3] : ''; $k[$i] = self::SM4_FK[$i] ^ (($a << 24) | ($b << 16) | ($c << 8) | $d); } for ($j = 0; $j < 32; $j++) { $tmp = $k[$j + 1] ^ $k[$j + 2] ^ $k[$j + 3] ^ self::SM4_CK[$j]; $buf = (self::SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 | (self::SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::SM4_SBOX[$tmp & 0xFF]); $k[$j + 4] = $k[$j] ^ (($buf) ^ ($this->sm4Rotl32(($buf), 13)) ^ ($this->sm4Rotl32(($buf), 23))); $this->_rk[$j] = $k[$j + 4]; } }}// 参与加密的密钥,最终为二进制的$key = hex2bin('这里是双方约定好的十六进制的密钥');// 需要加密的明文$data = 'QCzqMc5n';$sm4 = new SM4();echo "加密key:" . $key . "
";echo "明文:" . $data . "
"; $a = $sm4->encrypt($key, $data);// 得到的结果得先反转义base64编码,得到二进制,再转十六进制,才会与java的sm4加密后一制echo "加密结果:" . bin2hex(base64_decode($a)) . "
"; $b = $sm4->decrypt($key, $a);echo "解密结果:" . $b . "
";?>