views:

313

answers:

4

Can I RSA-encrypt in J2ME or J2SE, and decrypt in PHP?

I assume RSA is a specification, and has nothing to do with the languages.

+3  A: 

in a word, yes.

the only problem you might run into is character encoding or line-endings, but chances are you'll be fine.

oedo
+3  A: 

RSA is an algorithm; the other thing you need in order to interoperate is a specification of the message format. RFC 3447 defines one possible message format, there are others, for example RFC 3110 DNS key format. Pick a message format as well, and there should be no problem at all.

Andrew McGregor
+1  A: 

If you are encrypting anything larger than a key, you may want to consider encrypting your data with a symmetric cipher then encrypting the symmetric key using RSA. The standard mechanism for doing this (used in S/MIME etc) is CMS (The IETF's update to PKCS#7). I'm not sure what support there is for CMS/PKCS#7 in PHP but on the java side you can use the excellent bouncy castle crypto library. You will have no interoperability problems as all the data structures are defined in ASN.1. Don't worry if it sounds a bit complicated as these details are abstracted away for you by the crypto library.

bignum
A: 

Yes. I do that. In Java, any JCE should do. In PHP, please use OpenSSL extension.

As others mentioned, you can only encrypt up to key_length - 11 bytes. For example, for a 1024-bit key, you can only encrypt 117 bytes. If your data may exceed that, please use PKCS#7 which is supported by OpenSSL extension.

ZZ Coder
J2ME does not include the JCE.
GregS
In most J2ME profiles, you can use JCE like BouncyCastle.
ZZ Coder