2

我有一个私钥和加密字符串。实际上,字符串已使用 .NET 模块加密。现在我需要解密字符串。我在 php 中尝试过,但它引发了以下错误

 openssl_private_decrypt(): key parameter is not a valid private key  

我在 perl 中尝试过,但它需要自己的私钥,但它需要使用我自己的私钥,该私钥已在我们的服务器中生成。

请建议我解决这个问题。PHP ,Ruby , Perl 解决方案会更有帮助。

$crypttext="gKL/n5hkBg4jyjrLRqjQbf9gAS3xnbp1xmCmamPO33fW21JAJtlVQHYR6O1dOw3tfobMe/0uXm/kgivae9zHNey4Wt3UGzPwosUrx7V8zhC97AXya2tuENO1Fmc4Z8l9+UalwtUZxMGtl3Ua9DYuvxLP/TuavgRNpmG6eemGPag=";
$fp=fopen("private.pem","r");
$priv_key=fread($fp,8192);
fclose($fp);
openssl_private_decrypt(base64_decode($crypttext ),$newsource,false ) ;
echo "String decrypt : $newsource"**;

私钥

<BitStrength>1024</BitStrength><RSAKeyValue><Modulus>t2G2WWIal1EinPn54ZPc3S1UgGTDxr6RFc+XEMR723VSg9toU8lSfTD7C26bUcbDxBwP1/1MbdQcx/dKX+7UlB5z79vrwfT89rUZGWeH7VZvuAawtHURgucyGMhqAZ9NxDEAl5Uo3nsNL9j1JlSBfeZf8pU5sf70KezqJTRsfrE=</Modulus><Exponent>AQAB</Exponent><P>82dZbOjQCJ7NV6EuVJXqPlh4FB65LBL1w9696sKFZuIr8refGwTZOY05se6oHbT9mn8OFXVA6A/wmz7oWNPk9w==</P><Q>wN8uixNk73DIF2SHb0aunnW5XxAIq3KxeQKoUTBAzL7BqXmKjk6XDnfxDbybmcT51wGhiO20lGg51zuxnsPXlw==</Q><DP>Kv4+VXZqCJvEOY5G2LoCPjDyRNuIabiPoKFfenARkDKzAJReji81D21am4tENrsZcIiwvCmR5WurXECoWchT0Q==</DP><DQ>qGRzW4O0VYVvfVUNFi9tF/aKwR/boe0CXDfgwvnRKbHGnfP67+JX6o73zFmGtQuQYpMO+OEpD4WsMmnw2z/7ww==</DQ><InverseQ>czq4+xiiVxb63ZtKwkxyJoDLFH0f18YlfFQTrEoAx7UE9HdjOjsJFpZ54g0yK3/S/yVgIXPwMcw6LU1QvqazPg==</InverseQ><D>Ktp/tWWSlzfToeFcvpVCMMGOFK73fTM9Tl6Di9yOoRtKnBuixqmuSCkxEVvYmgSb7PEt1qiPur6ttyEX1VFHhaugTr3aVhUpF+k7ULaHrCb8UymXXW3pp/yl/QOMPWuNKVv/GU3aQ3VTc3WUaYuOnaIkJk7uoYDQn0QqWtxtT60=</D></RSAKeyValue>
4

3 回答 3

4

您需要转换密钥并实际使用它:

$crypttext="gKL/n5hkBg4jyjrLRqjQbf9gAS3xnbp1xmCmamPO33fW21JAJtlVQHYR6O1dOw3tfobMe/0uXm/kgivae9zHNey4Wt3UGzPwosUrx7V8zhC97AXya2tuENO1Fmc4Z8l9+UalwtUZxMGtl3Ua9DYuvxLP/TuavgRNpmG6eemGPag=";
$priv_key = openssl_pkey_get_private("file://path/to/private.pem");
openssl_private_decrypt(base64_decode($crypttext ), $newsource, $priv_key ) ;
echo "String decrypt : $newsource"**;
于 2012-07-13T12:52:57.570 回答
4

phpseclib 是一个纯 PHP RSA 实现,支持这种格式的 XML 私钥。使用示例:

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey('...'); // private key

echo $rsa->decrypt($ciphertext);
?>
于 2012-07-16T16:45:11.633 回答
2

.pem一种解决方案是使用 openssl (通常在 linux 下)以 PHP 期望的格式(文件,我认为该格式被调用DER ASN.1但我不确定)生成一个密钥,然后将其转换为仍然使用 OpenSSL 的格式.NET可以阅读。有关更多详细信息,请参阅此 SO 答案

在 Linux 下:

openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -out public.pem -pubout
openssl req -nodes -x509 -days 3650 -subj '/CN=www.example.com/emailAddress=info@example.com' -new -key private.pem -out certificate.crt
openssl pkcs12 -export -out certificate.pfx -inkey private.pem -in certificate.crt

在 .NET 下:

// Get the public key
X509Certificate2 pubCertificate = new X509Certificate2("certificate.crt", "passphrase", X509Certificates.X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider public = (RSACryptoServiceProvider)(pubCertificate.PublicKey.Key);
System.Console.WriteLine(public.ToXmlString(false));

// Get the private key
X509Certificate2 privCertificate = new X509Certificate2("certificate.pfx", "passphrase", X509Certificates.X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider private = (RSACryptoServiceProvider)(privCertificate .PrivateKey);
System.Console.WriteLine(private.ToXmlString(true));
于 2013-05-02T09:18:20.233 回答