Importing XML key from .NET to Crypt_RSA

From: Date: Tue, 18 Jul 2006 09:33:14 +0000
Subject: Importing XML key from .NET to Crypt_RSA
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-24674@lists.php.net to get a copy of this message
Hi readers, Now going to next issue about importing a public key created with .NET application to Crypt_RSA package's Crypt_RSA_Key class. I have created a keypair with .NET framework and exported the public key as an XML string. The result is following: <RSAKeyValue><Modulus>0xY6YB8ToRi8oQ6qWIPXVWxrXnEVsFopnoijDjA6nVlnUk4piBISCis1U8BwAN5uzrt9GbiPYvc6HxGMQ2wDFMyX9vGpUnUoFsNVKVGKHNahZMEDt0UN5Hs32GpuQLqoNm9In/3xBetIQ0/fgFfyxejzIkm5NcwkDSJlUtpFZ5U=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue> As you can see, it includes the modulus and the exponent both in base64 decoded format. However, when I try to create a Crypt_RSA_Key instance from decoded string, there happens something strange. For debugging purposes I'm taking print_r($key) after creating a new instance of Crypt_RSA_Key. That print doesn't include the exponent at all. The result is that I won't get any decoded data from ->decrypt method. Maybe this issue has something to do with padding? .NET uses PKCS#1 v1.5 padding as a default, in XP and Windows 2000 it allows only PKCS#1 v2 padding. How that should be noted at PHP side? Thanks for help, Ville ============================== <? require "Crypt/RSA.php"; // XML Exported RSA public key (from .NET) $rsaxml='<RSAKeyValue><Modulus><RSAKeyValue><Modulus>0xY6YB8ToRi8oQ6qWIPXVWxrXnEVsFopnoijDjA6nVlnUk4piBISCis1U8BwAN5uzrt9GbiPYvc6HxGMQ2wDFMyX9vGpUnUoFsNVKVGKHNahZMEDt0UN5Hs32GpuQLqoNm9In/3xBetIQ0/fgFfyxejzIkm5NcwkDSJlUtpFZ5U=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>'; ereg("<RSAKeyValue><Modulus>(.*)</Modulus><Exponent>(.*)</Exponent></RSAKeyValue>", $rsaxml, $regs); $modulus = base64_decode($regs[1]); echo "modulus (".strlen($modulus)."):$modulus<br />"; $exp = base64_decode($regs[2]); echo "exponent (".strlen($exp)."):$exp<br />"; $key = new Crypt_RSA_Key($modulus, $exp, 'public'); print_r($key); $crypt = new Crypt_RSA; // Decrypt the data $crypt_data=base64_decode('aW8mEQQJWhsgM2nELmTu2m632FN3MJb8N12qIrYSUCjG43BwOZKFHqgLbWHAgmMqv9Fm9eh6rDt6OOHDxl6w9yF6/LNzFDTkYHUAQSG5657lV0+dqcTo2dnVCvLWf+ms6wnkO1I4U+wjiCNyhnx2J1Aswmn+pGA3g5vWMB2u/0M='); $plain_data = $crypt->decrypt($crypt_data, $key); print_r($plain_data); ?>

« previous php.pear.general (#24674) next »