Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

openssl cannot decrypt the "RSA/ECB/PKCS1Padding" from java

843811Jun 5 2009 — edited Jun 5 2009
Dear,
Please give me an advice , I don't sure problem is by from OPENSSL or Java JCE

My problem is strange , java (private key) talk with php (public key )
in 2 way.
1. java encrypt -> php decrypt
2. php encrypt -> java decrypt

for (2) operation successfully.
but (1): php decryption get the NULL string

if I change from PKCS1Padding -> NoPadding then php can decypt message.

--------------

Java:
Cipher rsa_cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsa_cipher.init(Cipher.ENCRYPT_MODE,rsaPublicKey); // auto selects block type 2
AlgorithmParameters pm = rsa_cipher.getParameters();

byte[] enc_data = rsa_cipher.doFinal(ciphertext.getBytes());

BASE64Encoder based64 = new BASE64Encoder();
s_out = based64.encode(enc_data); //ciphertext
---------------
PHP:

$split_ctext=substr($cipher_text,$i,$blocksize);
$split_ctext=str_replace( "\r\n", "", $split_ctext );
$split_ctext=str_replace( "\n", "", $split_ctext );
$split_ctext=base64_decode($split_ctext);
openssl_public_decrypt( $split_ctext, $tmp, $public_key);

------------
if I change to
Cipher rsa_cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
and
openssl_public_decrypt( $split_ctext, $tmp, $public_key,OPENSSL_NO_PADDING);

Then I can success get plain text


---------------
Plain Text:
tmp = "eitweb123|WEB|20090512104236||cpg_test|23022006145859|3199900071658|100|100|0|7||eitweb123|WEB|20090512104236||cpg_te";

Result:
Fail Case:
[null message]
Success Case:
�����������eitweb123|WEB|20090512104236||cpg_test|23022006145859|3199900071658|100|100|0|7||eitweb123|WEB|20090512104236||cpg_te
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 3 2009
Added on Jun 5 2009
8 comments
3,723 views