【发布时间】:2020-03-06 00:03:26
【问题描述】:
我有一个 RSA 公钥/私钥 xml 文件。我想用它来签署一些数据,有以下类,这是我使用类的方式:
$processor = new RSAProcessor("certificate.xml", RSAKeyType:: XMLFile);
$data = $processor->sign($data);
print(base64_encode($data));
但我收到错误:WARNING: str_repeat(): Second argument has to be greater than or equal to 0 in RSA.php on line 81。如何解决?我猜有些东西对于 php 7.2 已经过时了?感谢您的帮助,我应该在课堂上做些什么来避免这个错误?
Rsa 类:
define("BCCOMP_LARGER", 1);
class RSA {
static function rsa_encrypt($message, $public_key, $modulus, $keylength) {
$padded = RSA::add_PKCS1_padding($message, true, $keylength / 8);
$number = RSA::binary_to_number($padded);
$encrypted = RSA::pow_mod($number, $public_key, $modulus);
$result = RSA::number_to_binary($encrypted, $keylength / 8);
return $result;
}
static function rsa_decrypt($message, $private_key, $modulus, $keylength) {
$number = RSA::binary_to_number($message);
$decrypted = RSA::pow_mod($number, $private_key, $modulus);
$result = RSA::number_to_binary($decrypted, $keylength / 8);
return RSA::remove_PKCS1_padding($result, $keylength / 8);
}
static function rsa_sign($message, $private_key, $modulus, $keylength) {
$padded = RSA::add_PKCS1_padding($message, false, $keylength / 8);
$number = RSA::binary_to_number($padded);
$signed = RSA::pow_mod($number, $private_key, $modulus);
$result = RSA::number_to_binary($signed, $keylength / 8);
return $result;
}
static function rsa_verify($message, $public_key, $modulus, $keylength) {
return RSA::rsa_decrypt($message, $public_key, $modulus, $keylength);
}
static function rsa_kyp_verify($message, $public_key, $modulus, $keylength) {
$number = RSA::binary_to_number($message);
$decrypted = RSA::pow_mod($number, $public_key, $modulus);
$result = RSA::number_to_binary($decrypted, $keylength / 8);
return RSA::remove_KYP_padding($result, $keylength / 8);
}
static function pow_mod($p, $q, $r) {
$factors = array();
$div = $q;
$power_of_two = 0;
while(bccomp($div, "0") == BCCOMP_LARGER) {
$rem = bcmod($div, 2);
$div = bcdiv($div, 2);
if($rem) array_push($factors, $power_of_two);
$power_of_two++;
}
$partial_results = array();
$part_res = $p;
$idx = 0;
foreach($factors as $factor) {
while($idx < $factor)
{
$part_res = bcpow($part_res, "2");
$part_res = bcmod($part_res, $r);
$idx++;
}
array_push($partial_results, $part_res);
}
$result = "1";
foreach($partial_results as $part_res)
{
$result = bcmul($result, $part_res);
$result = bcmod($result, $r);
}
return $result;
}
static function add_PKCS1_padding($data, $isPublicKey, $blocksize)
{
$pad_length = $blocksize - 3 - strlen($data);
if($isPublicKey)
{
$block_type = "\x02";
$padding = "";
for($i = 0; $i < $pad_length; $i++)
{
$rnd = mt_rand(1, 255);
$padding .= chr($rnd);
}
}
else
{
$block_type = "\x01";
$padding = str_repeat("\xFF", $pad_length);
}
return "\x00" . $block_type . $padding . "\x00" . $data;
}
static function remove_PKCS1_padding($data, $blocksize)
{
assert(strlen($data) == $blocksize);
$data = substr($data, 1);
if($data{0} == '\0')
die("Block type 0 not implemented.");
assert(($data{0} == "\x01") || ($data{0} == "\x02"));
$offset = strpos($data, "\0", 1);
return substr($data, $offset + 1);
}
static function remove_KYP_padding($data, $blocksize)
{
assert(strlen($data) == $blocksize);
$offset = strpos($data, "\0");
return substr($data, 0, $offset);
}
static function binary_to_number($data)
{
$base = "256";
$radix = "1";
$result = "0";
for($i = strlen($data) - 1; $i >= 0; $i--)
{
$digit = ord($data{$i});
$part_res = bcmul($digit, $radix);
$result = bcadd($result, $part_res);
$radix = bcmul($radix, $base);
}
return $result;
}
static function number_to_binary($number, $blocksize)
{
$base = "256";
$result = "";
$div = $number;
while($div > 0)
{
$mod = bcmod($div, $base);
$div = bcdiv($div, $base);
$result = chr($mod) . $result;
}
return str_pad($result, $blocksize, "\x00", STR_PAD_LEFT);
}
}
RSAProcessor 类:
class RSAProcessor
{
private $public_key = null;
private $private_key = null;
private $modulus = null;
private $key_length = "1024";
public function __construct($xmlRsakey=null,$type=null)
{
$xmlObj = null;
if ($xmlRsakey==null) {
$xmlObj = simplexml_load_file("xmlfile/RSAKey.xml");
} elseif ($type==RSAKeyType::XMLFile) {
$xmlObj = simplexml_load_file($xmlRsakey);
} else {
$xmlObj = simplexml_load_string($xmlRsakey);
}
$this->modulus = RSA::binary_to_number(base64_decode($xmlObj->Modulus));
$this->public_key = RSA::binary_to_number(base64_decode($xmlObj->Exponent));
$this->private_key = RSA::binary_to_number(base64_decode($xmlObj->D));
$this->key_length = strlen(base64_decode($xmlObj->Modulus))*8;
}
public function getPublicKey() {
return $this->public_key;
}
public function getPrivateKey() {
return $this->private_key;
}
public function getKeyLength() {
return $this->key_length;
}
public function getModulus() {
return $this->modulus;
}
public function encrypt($data) {
return base64_encode(RSA::rsa_encrypt($data,$this->public_key,$this->modulus,$this->key_length));
}
public function dencrypt($data) {
return RSA::rsa_decrypt($data,$this->private_key,$this->modulus,$this->key_length);
}
public function sign($data) {
return RSA::rsa_sign($data,$this->private_key,$this->modulus,$this->key_length);
}
public function verify($data) {
return RSA::rsa_verify($data,$this->public_key,$this->modulus,$this->key_length);
}
}
class RSAKeyType{
const XMLFile = 0;
const XMLString = 1;
}
证书.xml:
<RSAKeyValue>
<Modulus>tCZiqDS5BVQQZDBUYbyeoP4rENN4mX5FZJjjMNfGbyzfzH45RY2/YsMaY0yI1jMCOpukvkUyl153tcn0LXhMCDdsEhhZPoKbPUGMniKtFGjs18rv/b5FFUUW1utgwoL8+WJqjOqhQGgvbja63X9+WMFP0nM3d8yudn9C/X55KyM=</Modulus>
<Exponent>AQAB</Exponent>
<P>5HXvmU4IfqUG2jFLSqi/BMEQ3x1NsUDvx3zN5O1p9yLLspJ4sqAt4RUkxzcGodYgBSdXsR9IGcPwjQfbx3a7nQ==</P>
<Q>yd2hDCF/5Zqtz9DXjY1NRYGvBjTS4AQn83ERR46Y5eBSnLjpVjv6gPfARuhsUP44nikrQPvwPnjxQcOhJaOlvw==</Q>
<DP>ztuqUplBP8qU5cN0dOlN7DQT3rFdw30Unv/2Pa5qIAc1gT72YmZ+pCrM3kSIkMicvY3d7NZyJkIv8MKI0ZZEUQ==</DP>
<DQ>QFLJ5YarLWubZPQEK4vSCornTY/5ff51CIKH4ghTOjS/vkbBu4PDL+NCNpYLJcfMHMG7kap2BEIfhjgjGk5KGw==</DQ>
<InverseQ>WE6TqpcexQJwt9Mnp1FbeLtarBcFkXVdBauouFKHcbHCfQjA3IjUrGTxgSO74O/4QSKqaF2gnlL6GI7gKuGbzQ==</InverseQ> <D>czYtWDfHsFGv3fNOs+cGaB3E+xDTiw7HYGuquJz2qjk/s69x/zqFEKuIH8Ndq+eZYFQUCx+EGGxxENDkmYPa0z8wbfFI6JEHpxaLmQfpkkbSL1BJIp9Z5BNM2gy6jJqgbWwQPcN/4jpiMefHZWAqhMKqenUu1KIq1ZX6Bz5xKYk=</D>
</RSAKeyValue>
感谢您的帮助,我应该在课堂上做些什么来避免这个错误?
【问题讨论】:
-
只是想知道,为什么在 PHP 有 OpenSLL 的情况下实现代码?对读者来说,神奇的数字 81 是什么?
-
我不知道openssl,你能提供openssl的等效代码来用这个密钥签署数据吗?该类文件中的第 81 行是
$padding = str_repeat("\xFF", $pad_length); -
我猜由于我的私钥/公钥格式是特殊的,而不是标准的 RSA 文件格式,我不能使用 openssl,我应该只修复这个代码?
-
RSA 是一种算法。 PKCS#1 是 RSA 的通用标准。这使用 ASN.1 定义了 RSA 私钥结构,该结构通常是 DER 编码的(也有一种称为 XER 的 XML 格式,但它几乎不受任何地方的支持,以上不是 XER)。通常私钥使用 PKCS#8(内部)格式存储,然后包含 PKCS#1 中定义的密钥。通常这种功能是由好的加密库提供的(OpenSSL 当然支持它)。
标签: php encryption rsa public-key-encryption private-key