[英]Convert C# API Request to PHP
I need connect to an external API and the provider has only supplied example in C#.我需要连接到外部 API 并且提供商仅在 C# 中提供了示例。
Here is the C# code to generate a auth token.这是用于生成身份验证令牌的 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);
}
I have trued with the RSA.php class我已经验证了 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));
The output gives me a Auth Key to use in the header request. output 给了我一个 Auth Key 用于 header 请求。 I hope I am on the right track, I get a API response saying the "Agent not found"我希望我走在正确的轨道上,我收到 API 回复说“找不到代理”
This was the C# conversion to PHP这是 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));
The PHP's example is inconsistent to me: PHP的例子对我来说是不一致的:
you have:你有:
$json = json_encode([
'Agent' => 'XXX',
'Group:' => 'XXXXXXX'
]);
there is colon :
for Group but there is no colon for Agent, you probably should have a colon for both or not have it at all.有冒号:
对于组,但没有代理的冒号,你可能应该有一个冒号或者根本没有冒号。
also there are in C# example multiple quotes (I don't know C#) but perhaps the right JSON for PHP would be:在 C# 示例中也有多个引号(我不知道 C#),但 PHP 的正确 JSON 可能是:
$json = json_encode([
'Agent' => '"XXX"',
'Group' => '"XXXXXXX"'
]);
or或者
$json = json_encode([
'Agent:' => '"XXX"',
'Group:' => '"XXXXXXX"'
]);
most likely working:最有可能工作:
$json = json_encode([
'Agent' => 'XXX',
'Group' => 'XXXXXXX'
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.