質問

I'd like to pass the public key generated using Crypt_RSA (phpseclib) to Java.

1 way of of doing it is to pass the PEM public key to Java. The next step is to get the public key from the PEM file in Java. 1 way is to remove the -----BEGIN RSA KEY----- and -----END RSA KEY----- but is there any cleaner way?
Updates
This won't work actually! The only way I know of to construct RSAPublicKeySpec in Android or Java is through a modulus and an exponent. Of course, I can try a to kind of parse the same PEM public key to get them but I don't think that's one straightforward/ smart way of doing things here.

I'm thinking of getting the modulus and exponent of the public key and then pass them to Java. Java will then reconstruct the public key from the modulus and the exponent. However, how do I get the modulus and the exponent using Crypt_RSA?

役に立ちましたか?

解決 2

After

$rsa = new Crypt_RSA();
$rsa->loadKey($privateKey);
$rsa->loadKey($rsa->getPublicKey());

We can get the modulus and exponent through:

$pubMod = $rsa->modulus->toBytes();// this gives base 256 encoded string, toString() gives base 10 etc.
$pubExp = $rsa->exponent->toBytes();

the toBytes(), toString() and so on methods are found in Math_BigInteger class

Now it becomes easy for me to base64 encode the modulus and the public exponent and concatenate them into 1 string and send to my Java application

他のヒント

You could do this:

$publickey = $rsa->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_RAW);
echo $publickey['e'];
echo "\r\n";
echo $publickey['n'];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top