Android RSA decryption (fails) / server-side encryption (openssl_public_encrypt) -
i trying decrypt string in android application using rsa keys generated on device. encryption done php service, using public rsa key provided application. problem decryption, fails.
i doing following :
generating keypair on android (with keypairgenerator.getinstance("rsa")) -> ok
both keys (public , private) saved files after being "base64" encoded base64.encode(pubkey.getencoded()) , same private key. -> ok
when calling webservice, pass public key (in base 64) in post variable -> ok
the web service (a php service), uses public key encrypt short string, openssl_public_encrypt function. encrypted string converted base64. -> seems ok, function not return false.
the application retrieves result of service, , decodes (base64.decode()) -> ok (i have check, bytes received matches 1 generated openssl_public_encrypt() function)
the last thing decrypt string, doing following : -> not ok
cipher cipher = cipher.getinstance("rsa");
cipher.init(cipher.decrypt_mode, privatekey);
byte[] decryptedbytes = cipher.dofinal(cryptedbytes);
string decryptedstring = new string(decryptedbytes);
system.out.println(decryptedstring);
the result of decryption not match original string.
i missing ?
openssl uses padding = openssl_pkcs1_padding
default. have same padding mechanism @ both sides should use cipher.getinstance("rsa/ecb/pkcs1padding")
. use in java se.
note dangerous depend on default modes of operation in cryptography. many implementations have different defaults, , can hard up. try specify algorithm/mode use.
you try other rsa padding modes, note - unfortunately - android has disabled lot of algorithms , aliases bouncy castle source code adapted.
[edit] old answer, oaep padding advised now, or hybrid cryptography using rsa-kem.
Comments
Post a Comment