0

I'm trying to encrypt a string in Unity3D and then decrypt it in PHP with phpseclib.

Unity C# code:

/* this is done further up
CspParameters csp = new CspParameters();
csp.KeyContainerName = publicKey;
rsa = new RSACryptoServiceProvider(csp)*/

private string rsaEncrypt(string s)
{
    byte[] b = rsa.Encrypt(System.Text.Encoding.UTF8.GetBytes(s), true);
    return Convert.ToBase64String(b);
}

PHP code:

$encrypted = $_GET["enc"];

$rsa = new Crypt_RSA();
$rsa->loadKey($rsaPrivateKey);
$decrypted = $rsa->decrypt(base64_decode($encrypted)) or die();

echo base64_encode($decrypted);

The problem i get is on the php part where i get this message:

Notice: Decryption error in RSA.php on line 2625

I believe that the problem is the format of $encrypted but i can't seem to figure out how to solve it, any help is much appreciated.

neubert
  • 15,947
  • 24
  • 120
  • 212
user3713080
  • 399
  • 4
  • 17
  • Are you sure you mean Unity3D the *game engine*? – Artjom B. Sep 21 '16 at 21:47
  • Yes 123456789012345678901234567890 – user3713080 Sep 21 '16 at 21:50
  • The problem is likely due to RSA padding. You're requesting OAEP in C# and if I remember correctly phpseclib also uses OAEP by default, but they probably have different defaults for the hash function and MGF1. – Artjom B. Sep 21 '16 at 22:06
  • ArtjomB is correct - you probably need to do `$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);`. OAEP is more secure than PKCS1 but it's also less common. – neubert Sep 22 '16 at 22:00

0 Answers0