繁体   English   中英

使用PHP OpenSSL公钥将C#RSA加密转换为PHP解密

[英]C# RSA Encryption to PHP Decryption using PHP OpenSSL public key

我正在尝试通过Nusoap将来自SOAP服务器的OpenSSL公共密钥加载到C#中,使用公共密钥加密我的数据,然后将数据发送回PHP服务器以使用私有密钥进行解密。

我的C#看起来像这样:

static void Main(string[] args)
{
    PHPRef.AddService test = new PHPRef.AddService();

    var pkey = test.getPublicKey();
    //Console.WriteLine(pkey.ToString());

    byte[] PublicKey = GetBytes(pkey);

    //Values to store encrypted symmetric keys.
    byte[] EncryptedSymmetricKey;
    byte[] EncryptedSymmetricIV;

    //Create a new instance of RSACryptoServiceProvider.
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048);

    //Get an instance of RSAParameters from ExportParameters function.
    RSAParameters RSAKeyInfo = RSA.ExportParameters(false);

    //Set RSAKeyInfo to the public key values. 
    RSAKeyInfo.Modulus = PublicKey;
    //Import key parameters into RSA.
    RSA.ImportParameters(RSAKeyInfo);

    //Create a new instance of the RijndaelManaged class.
    RijndaelManaged RM = new RijndaelManaged();

    //Encrypt the symmetric key and IV.
    EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
    EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);

    Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider.");

    byte[] encryptedData = RSA.Encrypt(GetBytes("password"), false);

    //byte[] returned = (byte[])(Array)test.getDecrypted((sbyte[])(Array)encryptedData);

    //string answer = GetString(returned);

    string answer = test.getDecrypted((sbyte[])(Array)encryptedData);

    Console.WriteLine(answer);

    Console.ReadLine();

}

static byte[] GetBytes(string str)
{
    byte[] bytes = Encoding.ASCII.GetBytes(str);
    return bytes;
}

static string GetString(byte[] bytes)
{
    char[] chars = Encoding.ASCII.GetChars(bytes);
    return new string(chars);
}

我的PHP像这样:

function getPublicKey()
{
    $crt = file_get_contents("public.crt");
    // $publickey = str_ireplace("\r", "", $crt);
    // $publickey = str_ireplace("\n", "", $publickey);
    // $publickey = str_ireplace("-----BEGIN CERTIFICATE-----", "", $publickey);
    // $publickey = str_ireplace("-----END CERTIFICATE-----", "", $publickey);
    return $crt;
}

function getDecrypted($input)
{
    global $privateRSA;
    // $privateRSA = str_ireplace("\r", "", $privateRSA);
    // $privateRSA = str_ireplace("\n", "", $privateRSA);
    // $privateRSA = str_ireplace("-----BEGIN RSA PRIVATE KEY-----", "", $privateRSA);
    // $privateRSA = str_ireplace("-----END RSA PRIVATE KEY-----", "", $privateRSA);

    if(!openssl_private_decrypt($input, $decrypted, $privateRSA))
        return "fail";
    else
        return "success";

    return $decrypted;
}

不用说我每次都会“失败”。 有什么建议么? 我正在尝试使用纯PHP和纯C#,没有特殊的库。 密钥是2048位。

在尝试了整整一天的时间之后,这非常简单。 您不需要BouncyCastle,SecLib,任何第三方库,什么也不需要。

C#:

static void Main(string[] args)
{
    PHPRef.AddService test = new PHPRef.AddService();

    var pkey = test.getPublicKey();
    byte[] pkeybyte = GetBytes(pkey);

    X509Certificate2 cert = new X509Certificate2();
    cert.Import(pkeybyte);

    RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key;

    byte[] encryptedData = rsa.Encrypt(GetBytes("password"), false);

    Console.WriteLine(GetString(encryptedData));

    string answer = test.getDecrypted((sbyte[])(Array)encryptedData);

    Console.WriteLine(answer);

    Console.ReadLine();

}

和PHP:

像这样更改getPublicKey

function getPublicKey()
{
    $crt = file_get_contents("public.crt");
    $publickey = str_ireplace("\r", "", $crt);
    $publickey = str_ireplace("\n", "", $publickey);
    $publickey = str_ireplace("-----BEGIN CERTIFICATE-----", "", $publickey);
    $publickey = str_ireplace("-----END CERTIFICATE-----", "", $publickey);
    return $publickey;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM