无法让PHP代码像C#一样对数据进行签名

问题描述 投票:0回答:1

我有以下 C# 代码,运行良好。我需要在 php 中创建相同的函数,该函数将返回与 C# 完全相同的签名许可证。我正在使用 phpseclib v1.0.22,在我的一生中,我无法让 php 函数为给定的许可证和私钥返回相同的签名。我不是 php 程序员,所以我需要一些帮助来找出我的方法的错误。

工作的 C# 代码如下:

私有字符串SignLicense(字符串strLic,字符串strPrivateKey) {

// Create instance of RSACryptoServiceProvider and import private key params into it
System.Security.Cryptography.RSACryptoServiceProvider rsaCSP = new System.Security.Cryptography.RSACryptoServiceProvider();
rsaCSP.FromXmlString(strPrivateKey);
RSAParameters rsaPrivateParams; // stores private key
rsaPrivateParams = rsaCSP.ExportParameters(true);
rsaCSP.ImportParameters(rsaPrivateParams);

// Convert the string to a Byte array
byte[] userData = Encoding.UTF8.GetBytes(strLic);

// Setup to sign the userData using SHA1 and RSAPKCS1
AsymmetricSignatureFormatter asf = new RSAPKCS1SignatureFormatter(rsaCSP);
HashAlgorithm algorithm = new SHA1Managed();
asf.SetHashAlgorithm(algorithm.ToString);

// Hash the user userData now
byte[] myhashedData; // a byte array to store hash value
string myhashedDataString;
myhashedData = algorithm.ComputeHash(userData);
myhashedDataString = BitConverter.ToString(myhashedData).Replace("-", string.Empty);

// Sign the Hashed data and convert it back to a string and return it to the caller
byte[] mysignature; // holds signatures
mysignature = asf.CreateSignature(algorithm);
return Convert.ToBase64String(mysignature);

}

我对 php 等效项的尝试如下,但不起作用:

function SignLicense($strLic, $strPrivateKey) {

    // Decode the Install Code
    $strReq = base64_decode($strLic);

    // Create a new Crypt_RSA object using phpseclib    
    $rsa = new Crypt_RSA();

    // Set the hash to sha1 and the encryption mode to PKCS1        
    $rsa->SetHash("sha1");
    $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);

    //Load the Key file from the stored GCode which is in XML Format
    $rsa->loadKey($strPrivateKey, CRYPT_RSA_PRIVATE_FORMAT_XML);
    
    //utf8 Encode the license string now
    $userdata = utf8_encode($strLic);

    //Sign the stLic now (encrypt it using the loaded Private Key (GCODE))
    //$hasheddata = base64_encode($rsa->sign($userdata))
    $strSig = $rsa->sign($userdata);

    // Add the signature to the license
    $strLicKey = str_replace('=', '', base64_encode($strSig));

    return $strLicKey;
}

当我将相同的 strLic 和 strPrivateKey 传递给每个函数时,我会得到完全不同的签名许可证。以下是我得到的回复:

C# 签名许可证: utlJd3Ce8N1Z2I36FznY8wCtlmmgOtmyv1wdos330Bgk4D6S9GlWl+p+lNKcjamDxyrUrt+jf47wVx777W0zZlie9zufX/UkhMZNxM+ZOkdEU61qltyb122M1GBK6BjzhicwM/BW8aP/ei 3DZwMrZuSvqxLxjT/Q4bC73xbpTGUl9KssGLmIxsYm0p3wbxsDM85nnD3yCzRtZYpdW1S5UU4EHXqudLPCSDIJ7DCPDft8k7iRsaKhpiloWax9AD2a5y5Hxhkrq4KH58ZcQ1aYCc+eEbfVn OS822EOY7aTOVH8FmiwXgaLgm5/3XsKayJam0GH4th7MXzcn77OPQOQeQ==

php 签名许可证: 0ITgqYxK/clH6VbzP6R+oDdD8bfcKrOeurySCRTHnJmjyCYa4WPnNUw63uhEpcnyQOCJjx7YoNqQeeWwHn6URrTymk3ck8XEQ2w6wGvZtgACfpJ/vCgAhd/MYNDyhauaxKqqY2qTjslbaoBi8Ne0chv rgN4iImA3qETX2kwrI+3DZUSW8Zr78dVv8abRjPc8kjG87TK3Mtxf0joT+2/XMB0Dh6A2xCf2Hoz+P4lMNLR85j2X4xyBlW/Kumg6woAgJUhaeQfPbSuSQPrB9N08v7cCGUNUT+YaAjAC1Ud BoDovYQ2bZRFSiZ73+Muejk9/m2uHbv6mAsDdVmgUaky9SQ

它们完全不同,我一生都无法弄清楚为什么。有人可以帮我解决这个问题吗?

谢谢!

c# php rsa phpseclib
1个回答
-1
投票

不。 PHP 是一种 Web 脚本语言,用于编写大量 Web 应用程序的后端组件。它有一个重点突出、定义明确的工作,而且确实做得很好。

另一方面,C# 是一种通用语言,它是作为 .NET 框架的标准、事实上的语言而开发的。 C# 的范围、影响力和能力自然远远超出 PHP。

这并不是说 PHP 是一种劣等语言,只是这两种语言来自不同的领域。这就像比较橙子和苹果一样。

© www.soinside.com 2019 - 2024. All rights reserved.