C#RSA加密 – > PHP RSA解密

我试图在C#中使用RSA加密AES密钥和IV,并使用phpseclib用PHP解密它们。 我试图解决这个问题大约4个小时,但我总是得到’2495行的解密错误’。 PKCS似乎有问题。

临时私钥

 

临时公钥

  

在C#中使用RSA加密后的密文(decrypted =“testkey”,因为我删除了用于测试的aes加密)

 Cd/RsiVqKnEP2T9oTgnvRuHVKY09VfynLHIlinIGtW4PFrB2kKffIrIqRQKhob6bPIR4efjxhCn43AQ2gE5P/AMG/EDWk9HMJF8XuhdtsWfPmnqxVV4crpA2FZwh4BWdXq4N70ieWbuk+pRJ1dHGhLgFfphp4sVVopn3bPKw2VKI0O+MT4nUCHFac25owoFnMULzuxj60I9Qa/TIlCKwMNcv2r7ili/LvplPZIEnH2p/bR62TAUvty0yo9NTHZm+wlqyIUmA1/GrM0VHjmcnRjFQHp1zQreRspvRsbk= 

PHP(如果strrev与否,则无变化):

 public function RSADecryption($key, $iv) { $PrivateKeyFile = RSA_Private; $rsa = new Crypt_RSA(); $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); $rsa->loadKey($PrivateKeyFile); // decrypt key and iv for aes decryption $aes_key = base64_encode($rsa->decrypt(strrev((base64_decode($key))))); $aes_iv = base64_encode($rsa->decrypt(base64_decode($iv))); echo $aes_key; echo "
"; echo $aes_iv; }

C#加密:

 public static string RSAEncryption(string aes_key, string aes_iv, string publickey) { if (publickey.Contains("-----")) { // Get public key without -----.... publickey = publickey.Split(new string[] { "-----" }, StringSplitOptions.RemoveEmptyEntries)[1]; } // Remove "new line" characters publickey.Replace("\n", ""); byte[] KeyToEncrypt = Encoding.Default.GetBytes(aes_key); byte[] IVToEncrypt = Encoding.Default.GetBytes(aes_iv); byte[] PublicKey = Encoding.Default.GetBytes(publickey); //Values to store encrypted symmetric keys. byte[] EncryptedKey; byte[] EncryptedIV; //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //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); //Encrypt the symmetric key and IV. EncryptedKey = RSA.Encrypt(KeyToEncrypt, false); EncryptedIV = RSA.Encrypt(IVToEncrypt, false); System.IO.File.WriteAllText(@"C:\WriteTextCryptKey.txt", Convert.ToBase64String(EncryptedKey)); System.IO.File.WriteAllText(@"C:\WriteTextCryptIV.txt", Convert.ToBase64String(EncryptedIV)); return Convert.ToBase64String(EncryptedKey); } 

函数在phpseclib中调用:

 function _rsaes_pkcs1_v1_5_decrypt($c) { // Length checking if (strlen($c) != $this->k) { // or if k _os2ip($c); $m = $this->_rsadp($c); if ($m === false) { user_error('Decryption error'); return false; } $em = $this->_i2osp($m, $this->k); // EME-PKCS1-v1_5 decoding if (ord($em[0]) != 0 || ord($em[1]) > 2) { user_error('Decryption error'); return false; } $ps = substr($em, 2, strpos($em, chr(0), 2) - 2); $m = substr($em, strlen($ps) + 3); if (strlen($ps) < 8) { user_error('Decryption error'); return false; } // Output M return $m; } 

if (ord($em[0]) != 0 || ord($em[1]) > 2)是第2495行

解:

C#:

 public static string RSAEncryption(string aes_key, string aes_iv) { //encode key and iv to byte array byte[] KeyToEncrypt = Encoding.Default.GetBytes(aes_key); byte[] IVToEncrypt = Encoding.Default.GetBytes(aes_iv); //get RSA public key from xml file TextReader reader = new StreamReader("publicKey.xml"); string publicKey = reader.ReadToEnd(); reader.Close(); MessageBox.Show(publicKey); //Values to store encrypted symmetric keys. byte[] EncryptedKey; byte[] EncryptedIV; //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //set xml string as public key RSA.FromXmlString(publicKey); //Encrypt the symmetric key and IV. EncryptedKey = RSA.Encrypt(KeyToEncrypt, false); EncryptedIV = RSA.Encrypt(IVToEncrypt, false); System.IO.File.WriteAllText(@"C:\WriteTextCryptKey.txt", Convert.ToBase64String(EncryptedKey)); System.IO.File.WriteAllText(@"C:\WriteTextCryptIV.txt", Convert.ToBase64String(EncryptedIV)); return Convert.ToBase64String(EncryptedKey); } 

PHP:

 public function RSADecryption($key, $iv) { $PrivateKeyFile = RSA_Private; $rsa = new Crypt_RSA(); $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); $rsa->loadKey($PrivateKeyFile); //private key in xml // decrypt key and iv for aes decryption $aes_key = $rsa->decrypt(base64_decode($key)); $aes_iv = $rsa->decrypt(base64_decode($iv)); }