doukui9491 2014-05-04 22:38
浏览 184
已采纳

使用c#中的公钥加密数据,并使用php中的私钥解密数据

I'm trying to encrypt a JSON string in C# en decrypt it in PHP by using OpenSSL RSA.

The following code is in C#:

public static string EncryptData(string data)
{
    string key = @"-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/J/txQvYBm5iOHRRRtgdXd5hq
    DEavxy5OExSiwjJgOgT72A9GX7+E7QYfhUZJTGtf+/J84tuJPx6/Ff1dZa1XRW84
    /n5m7fw93CTVwpfRFi0Owenor44zgk6ABSfMlCzwGfdcS8AFiWCFUBpatKte6PaO
    /WfYK7qz/5faSp3FawIDAQAB
    -----END PUBLIC KEY-----";

    OpenSSL.Crypto.CryptoKey pKey = OpenSSL.Crypto.CryptoKey.FromPublicKey(key, null);
    OpenSSL.Crypto.RSA rsa = pKey.GetRSA();

    byte[] encryptedData = rsa.PublicEncrypt (Encoding.UTF8.GetBytes(data), OpenSSL.Crypto.RSA.Padding.PKCS1);
    rsa.Dispose();
    return Convert.ToBase64String (encryptedData);
}

then I send the encoded encryptedData to PHP and put it into the PrivateKeyDecrypt function.

Code in PHP:

<?php
// include the lib
//require_once('OpenSSL.php');

$ssl = new OpenSSL();
$ssl->loadPrivateKeyWithoutPath('-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC+wVuIECnd7dvt7yqoElg4DL8jSKFpUavKfviSBE2Z+5z5YWnf
i4mp/r0P0jVbE+dfqReaIr4UOt//hPvwvioAJ576KPOCTYZiIGehkqafGWS+01wH
nAPSj/C7Xt7F9Mh5McH4CnBR5VN93KKZtQtfNsNPPBdXwQuquONOUgUgTwIDAQAB
AoGAIQkvpvLVrV/CVQS0qIL00FA00hGEEs9YJyuyNOeV4PMYjn/2bAaogs5hQ9ot
72YVDhYkxkb6EFrZ7FDFqT3l2/BpQEsJ6/GQq2gX0rYGfJ5sWrN+KIuSJr4FWSN+
LrvIEr35hKHY6mytiQg9D58b5kSjaB9ea/qhh/hJL2VfWGECQQDlBBJsccR2ZzzH
QN5omwFrUuxnqraY5LyZB8xUnqo9iDULO+GTcW/5eO9TBoWuDf1Ul3Snts9Tb+uD
UYL1y5onAkEA1Ts182anQHCjUtp2hooLjDcOvIK3gi5TchqHW6T4K3tNtnbfOB9P
gMNgcKljjXlQXl3ornvLTYBC483MS4RpmQJAZMiS9whmBhlOpP1GI1C7oih/Auwc
qVJYMRw+bqrYjnWnqkby8XkFZwsyfx4qrDtZ1eVFIB2SRczGHyc688JjFwJBAIxa
lx6J2VlfSwIEbrQMNaGrs/V9jVERMTgQIjy+j+P/G54ZDMEiSvCUqew+cxryWWZk
Jf6D2cQ/wsez9N9YXzECQQDMH6jS9vp7aabI2UKy5RhTqvP6kRmi28/GHb/8QrFx
tldiw2THvc4+4QT9tuv5OA6xRnEO5mt5u3vFHciqeRR7
-----END RSA PRIVATE KEY-----');

public function PrivateKeyDecrypt($raw_data, $base64 = TRUE)
{
    if ($this->privateKey == NULL)
        trigger_error("Private key has not been specified.", E_USER_ERROR);

    if (!openssl_private_decrypt(($base64) ? base64_decode($raw_data) : $raw_data, $decrypted_data, $this->privateKey, $this->padding))
        trigger_error("Unable to decrypt data.", E_USER_ERROR);

    return $decrypted_data;
}

The error I get when I call the PrivateKeyDecrypt function is:

FATAL ERROR: unable to decrypt data in ...

Does anyone have any idea what causes this error?

  • 写回答

1条回答 默认 最新

  • dongxu3029 2014-05-05 08:42
    关注

    Make your padding match OpenSSL.Crypto.RSA.Padding.PKCS1 in your C# code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么
  • ¥15 Linux下安装搜狗拼音不能显示中文