我正在尝试使用C#中的RSA解密已在PHP中加密的密码。这是我的C#代码:
go build -race
PHP代码是:
public byte[] Decrypt(byte[] dataToDecrypt)
{
byte[] plain;
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.PersistKeyInCsp = false;
rsa.ImportParameters(PrivateKey);
plain = rsa.Decrypt(dataToDecrypt, false);
}
return plain;
}
我在C#中没有异常或错误。问题是,如果我将Decrypt函数的字节数组输出转换为字符串(即使用System.Text.UTF8Encoding.GetString()),则解密的字符串与传递给PHP中的“encrypt”函数的原始纯文本非常不同
知道为什么他们不同吗?我错过了什么?
P.S。我想补充一点,我有代码来加密C#中的数据并以非常类似的方式在PHP中解密,没有任何问题。如上所述,我不能让它以相反的方式工作。谢谢
===更新===
输入数据 - 即$ dataToEncrypt:
public static function encrypt($dataToEncrypt, $userPublicKey) {
if (openssl_public_encrypt($dataToEncrypt, $encrypted, $userPublicKey, OPENSSL_PKCS1_PADDING)) {
return $encrypted;
} else {
throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');
}
}
公钥和私钥是:
$sessionKey = mcrypt_create_iv(32, MCRYPT_DEV_URANDOM);
因此,例如,如果base64字符串中的输入($ dataToEncrypt)为:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAzMzT6e4CVC11E1w9bXhnIVgZIAfs5McBeewW3bZtS8NBelHs
HyxiZjkHgMZp6BGSsux0gl0e+DkeISHe5dOQEX7WJBaARxiGJxTzhEo556rrmSXz
7h5TSht/ivTWPZTYfAaYwLC71C/IKWfRPGSVuqHTlflVc0JSUF3M6dgWPqLRF7G3
qqYFdx3+2CoVyCQkNogvkICZNbGGtx7RlGHJ1LCeCyEdC+6AIyNItBbXGtBOf/IZ
bdL+QioWCV/VzQAhm01QVn8g6CHiCMvAIMMj2RXUg/1tL2vw0QNS2ZJgfsM3x67s
M47T1zHj/OTaX6lAp77FHkof1tKmTwlV1RSj7wIDAQABAoIBAGHMbmYHgpUwH/he
0/ayyIzjxRvGrRmS7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qyFQQ+Sy
EX/w3HADQI4PRE90iauZ0DYGJyxY1jHqa0X64W/5nZqH/uv58+Hf6uv47l3n3n0T
58WFfKgXY5AaLCbi1UERdeHKPXNXDFkTKuuOb190M+RNbGgzwN9fcQ==
-----END RSA PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA498EWxuZK/KsUgIEusEt
QOJulgTHwb8C4avtzJnzhosTeKooXvyGFPpex6HcQGSRqrWpNr2yhw1BvJvH2UyE
Jisl5BJA5Za+ofmbGifCFwCllZ37U1YpOmqpB2Yt+yYElGh5dp+lqs5Q3u3nPknd
nLS3bxH7qlZBvR9YPWj9x7IuSXJyopAmdJato8xeNHzmBxWD8FgQKICFpLtGsPXq
XRwT0imTs6/EcMqq6fdlp0OyBKyZjw6t47gMeqiuSYz6k41Nf/SbtIC4snUyoUgI
TvnHjWe1cY7js4kY62A9ZpHX0NpG7JXctxVb+aZOv1rS36bUjcP+bug1W3ZKrTG6
hwIDAQAB
-----END PUBLIC KEY-----
我在base64字符串中得到此输出(plain)是:
VPA13tGd8HMlaB3yKxlFBb8/dAXlCjX5Rr1SzE4L6TY=