[英]Convert C# API Request to PHP
我需要连接到外部 API 并且提供商仅在 C# 中提供了示例。
这是用于生成身份验证令牌的 C# 代码。
public static void Main()
{
string json = @"{
Agent : "XXX",
Group: "XXXXXXX"
}";
string publicKey = "XXXXXXXXXXXXXXXXXX"
byte[] agentBytes = Encoding.UTF8.GetBytes(json);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
var rsa2Params = rsa.ExportParameters(false);
rsa2Params.Modulus = Convert.FromBase64String(publicKey);
rsa.ImportParameters(rsa2Params);
byte[] hashValue = rsa.Encrypt(agentBytes, true);
string output = Convert.ToBase64String(hashValue);
Console.WriteLine(output);
}
我已经验证了 RSA.php class
$rsa = new phpseclib\Crypt\RSA;
$keys = $rsa->createKey();
$json = json_encode([
'Agent' => 'XXX',
'Group' => 'XXXXXXX'
]);
$rsa->loadKey($keys['privatekey']);
$output = base64_encode($rsa->encrypt($json));
output 给了我一个 Auth Key 用于 header 请求。 我希望我走在正确的轨道上,我收到 API 回复说“找不到代理”
这是 C# 转换为 PHP
$rsa = new \phpseclib\Crypt\RSA();
$json = json_encode([
'Agent' => 'XXX',
'Group' => 'XXXXXX'
]);
$publicKey = 'XXXXXXXXXXXXXXXXXX';
$exponent = 'AQAB';
$public = [
'n' => new \phpseclib\Math\BigInteger(base64_decode($publicKey), 256),
'e' => new \phpseclib\Math\BigInteger(base64_decode($exponent), 256),
];
$rsa->loadKey($public);
$token = base64_encode($rsa->encrypt($json));
PHP的例子对我来说是不一致的:
你有:
$json = json_encode([
'Agent' => 'XXX',
'Group:' => 'XXXXXXX'
]);
有冒号:
对于组,但没有代理的冒号,你可能应该有一个冒号或者根本没有冒号。
在 C# 示例中也有多个引号(我不知道 C#),但 PHP 的正确 JSON 可能是:
$json = json_encode([
'Agent' => '"XXX"',
'Group' => '"XXXXXXX"'
]);
或者
$json = json_encode([
'Agent:' => '"XXX"',
'Group:' => '"XXXXXXX"'
]);
最有可能工作:
$json = json_encode([
'Agent' => 'XXX',
'Group' => 'XXXXXXX'
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.