使用带有RSA XML格式的公共和私有密钥的phpseclib在php中的代码示例:
include 'vendor/autoload.php';
$rsa = new \phpseclib\Crypt\RSA();
//extract($rsa->createKey());
$publickey1 = "<RSAKeyValue><Modulus>sNBrh+HL35mYAxWwd7nGOWkrj7If39+kzlUv3L+OSIHkZd0zocyyt8qkhLoN5RsgrqFyHXHScyZPA7A/eGjRYOGrpQZt8BU9PIv7CN6vDdSXyhb60xyhBGDLkVD0Z0898oCYEmQpHs+8KPNOIVbOJUqH38S+/G3zzJrEUUijU877Gr21E/0sqzZGWsuZdmccPGQeiyFcyghzVL5R5jwJNsNO+lm5VW0YmknMrbg2JOGV8Iv6olcrdskRYD9iZkJrWq4y/jES/1tjvIaaI16HUDSCSoico/xbWLNgQaIAGs9tf2nspyaPT3h17MCR0BkzgMkhhv/DYywYp5AQ7o8HdomO8PnlouV2yESu65hNNvEjHxsTt4iA+avxYX1YB2kjSg0aiinRBhoqW/65iW6+yQm2//LfSw0fzfowhS207vvX1b+nFHsZTLlhMrAfH7V5pSS2ProrgXIGduKTfmTmfNz3X3DKeOuGRbA4gcA20gNN1smj/LPT1LlaIiwytKbdvn4evBYJIJsf2+9xnYeqrPuJDROLrdaCXX/rsAbKEID3zds/nZevpE9jN0DJSCwsr+2eDMz3AgYA0Ihn8RIj46ba/GRCd8TGs/O0Q78bB41nvBVgZ3xPVZgL+ObJiIw0n3OGDAfMR7yabsk3aMYSfKLuFWtqooGei5PKZ7LXnL+8OG/fy8KIsNSPEHu0t+SutI2LNIfkk0iOw9G1DPFdwY5VwPfMf1ao3ZXnJxrixuP0w4lHpKMhckPyoqYrblVkMHXP8Vdpc5YwamXMXb/Zo2xxjpY8cPi1oUjJRMt7GzuKDFK9wH1CSIXZlAyLzB4ijK3kK81um9plUguCJHYzrw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
$privatekey1 = "<RSAKeyValue><Modulus>sNBrh+HL35mYAxWwd7nGOWkrj7If39+kzlUv3L+OSIHkZd0zocyyt8qkhL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QYvplmwj6u+Vkmx/RUFyvQllhwsam+Dr12JNp5WLzf9z0y/v++Qfyg1KxULGu3rDazSF99Ptp7oF1uWRQXexbKVyawB225l5hdjDWU5KcTKhU8EtoI1H+I8XFRLEa8CI6UFo3ihWMv3AwircGt5A+0bdFCm6JZHXLtVNekPPaZzN+RAxhi1yPy8aMJPrs5h/tgMWdzW9Gl1aR362h8d6jKtQCTIXl50txiRFaOvZTRgdf5dZQAzcTv1/kIbqptebQWShTRGAX/CIERT0Bq4+U+lgY4a8f9rGAU5W9w8JliJd307cF8JqTrGzqoeNCEP05LpK2a3GTgKtb0Zsmp8gW/jkbZ5Ir3UgpJcbM9Rd5wUYLRXVUsUHTRWMjjNzoWAm0UCc8Bk/QJPR96eOecaoTRjyTgieC1ipTfHydv8g2Z1EX/Q3Q9qWa/UefV1rMI5HFaCt5/FmzZMMvt748psbYD2hOhVj2iwVcOWsT/mJE9WG6jWLT2KQl0wIYV7qzP+sSmk63QLCAavzUlKCCwyRA9kaukgsgoXll854RQXz92qxTxzdEj4Y+v5VpSBRiK8MU9I/BUfYX43+z9Q4e3B/x9UXFrnkmNkJdkQ==</D></RSAKeyValue>";
$plaintext = "invoice_number=1,100.00";
$rsa->setEncryptionMode(2); // encryption mode
$rsa->loadKey($publickey1);
$ciphertext = $rsa->encrypt($plaintext);
$rsa->loadKey($privatekey1);
echo $rsa->decrypt($ciphertext);
接下来,使用c#.net
创建的非xml rsa公钥和私钥进行数据加密include 'Crypt/RSA.php';
$rsa = new Crypt_RSA();
//extract($rsa->createKey());
// when upper line make uncomment and below $privatekey and $publickey variable commented then this code encrypt and decrypt data successfully.
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$publickey = "-----BEGIN PUBLIC KEY----- BgIAAACkAABSU0ExAAQAAAEAAQAnh6FFs6kYe/gmb9dzqsQKmtjFE9mxNAe9mEU3OwOEEfyIwkAx0/8dwh12fuP4wzNbdZAq4mmqCE6Lo8wTNNIJVNYEhKq5chHg1+hPDgfETFgtEO54JZSg3cBZWEV/Tq3LHEX8CaLvHZxMEfFXbTfliFYMLoJ+YK1mpg9GYcmbrQ== -----END PUBLIC KEY-----";
$privatekey = "-----BEGIN RSA PRIVATE KEY----- BwIAAACkAABSU0EyAAQAAAEAAQAnh6FFs6kYe/gmb9dzqsQKmtjFE9mxNAe9mEU3OwOEEfyIwkAx0/8dwh12fuP4wzNbdZAq4mmqCE6Lo8wTNNIJVNYEhKq5chHg1+hPDgfETFgtEO54JZSg3cBZWEV/Tq3LHEX8CaLvHZxMEfFXbTfliFYMLoJ+YK1mpg9GYcmbrVmMAKSoOgETkkiJJzYmXftO3KOveBtvkAzjHxxSS1yP/Ba10BzeIleH96SbTuQtQRLXwRykdX9uazK+YsiSud9/PyLbgy5TI+o28OHq5P+0y5+a9IaAQ/92UwlrkHUYfhN/xTVlUIxKlTEdUQTIf+iHif8d4ABb3OdYJXZOW6fGeUP10jMyvbnrEoPDsYy9qfNk++0/8UP2NeO1IATszuZYg1nEXOW/5jmUxMCdiFydp9ES211kpEZ4XcvjGaDlaQ+bLWj05i2m/9aHYcBrfcxxvlMa/9ZvrX4DfPWeydUDDDQ4+ntpT50BunSvmyf7cUk76Bf2sPgLXUQFoufEQ5g1Qo/v1uyhWBJzh6OSUO/DDXN/s8ec/tN05RQQFZQ0na+v0hOCrV9IuRqtBuj4WAj1I/A1JjwyyP9Y/6yWFPM6EcS/6lyPy30lJPoULh7G29zkn7NVdTEkDtthdDjtX7Qhgd9qWvm5ADlmnvsS9A5m7ToOgQyOxtJoSlLitLbf/09LRycl/cdIzoMOCEdPe3DQcyEKqUPsghAq+DKw3uZpXwHzwTdfqlHSWAnHDggFKV1HZuWc1c4rV4k4b513TqE= -----END RSA PRIVATE KEY-----";
$plaintext = 'terrafrost';
$rsa->loadKey($publickey);
$ciphertext = $rsa->encrypt($plaintext);
$rsa->loadKey($privatekey);
echo $rsa->decrypt($ciphertext);
C#.NET 生成非xml rsa公钥和私钥:
CspParameters cspParams = new CspParameters { ProviderType = 1 };
System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(1024, cspParams);
// Generate a new 2048 bit RSA key
String public_key = Convert.ToBase64String(rsa.ExportCspBlob(false));
String private_key = Convert.ToBase64String(rsa.ExportCspBlob(true));
请帮助我,为什么数据不使用c#公钥和私钥在php中加密和解密..
答案 0 :(得分:0)
您正在使用两种不同版本的phpseclib。对于解密,您使用的是1.0分支,对于加密,您使用的是2.0或主分支。
所以我建议首先使用相同的版本。
使用常量而不是它们对应的整数也是好的。例如。不要$rsa->setEncryptionMode(2)
- 做$rsa->setEncryptionMode(Crypt\RSA::ENCRYPTION_PKCS1)
。至少是您的第一个包含工作代码的代码段。
对于您的第二个代码段,您使用的phpseclib版本与第一个代码段不同...您的密钥无效。
它们以BgIAAACkAAB
和BwIAAACkA
...键开头,以-----BEGIN PUBLIC KEY-----
或-----BEGIN RSA PRIVATE KEY-----
开头,以{{}开头1}}或MIICXAIBAAKB
。 https://stackoverflow.com/a/21289989/569976提供了更详细的解释原因。
您的密钥似乎所处的格式(并且您似乎已尝试改变为phpseclib支持的格式)实际上是Microsoft Key BLOB。我在这里写了一个初步解码器:
https://stackoverflow.com/a/32486709/569976
将它扩展到phpseclib核心会更好,但我们会看到。