解决PHP中sm4加密算法与JAVA中sm4加密后的值不一致的问题
•
Jave
重点:在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) _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 _rk[31 - $i];
$buf = (self::SM4_SBOX[($tmp >> 24) & 0xFF]) <> 16) & 0xFF]) <> 8) & 0xFF]) <sm4Rotl32(($buf), 2) ^ $this->sm4Rotl32(($buf), 10) ^ $this->sm4Rotl32(($buf), 18) ^ $this->sm4Rotl32(($buf), 24));
}
$plainText = [];
for ($k = 0; $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 _rk[$i];
$buf = (self::SM4_SBOX[($tmp >> 24) & 0xFF]) <> 16) & 0xFF]) <> 8) & 0xFF]) <sm4Rotl32(($buf), 2) ^ $this->sm4Rotl32(($buf), 10) ^ $this->sm4Rotl32(($buf), 18) ^ $this->sm4Rotl32(($buf), 24));
}
$cipherText = [];
for ($k = 0; $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 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 <> (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 > 24) & 0xFF]) <> 16) & 0xFF]) <> 8) & 0xFF]) <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 . "
";
?>
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/6b5a5574c0.html
