RSA encryption in Java, decryption in PHP

Suppose I have the following Java code to generate a public key pair:

KeyPairGenerator generator = KeyPairGenerator.getInstance ("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

generator.initialize (1024, random);

KeyPair pair = generator.generateKeyPair();
RSAPrivateKey priv = (RSAPrivateKey)pair.getPrivate();
RSAPublicKey pub = (RSAPublicKey)pair.getPublic();

// Sign a message
Signature dsa = Signature.getInstance("SHA1withRSA");
dsa.initSign (priv);
dsa.update ("Hello, World".getBytes(), 0, "Hello, World".length());

byte[] out = dsa.sign();
/* save the signature in a file */
FileOutputStream sigfos = new FileOutputStream("sig");
sigfos.write(out);
sigfos.close();

How could I decrypt the sig file in PHP? I read the post: https://stackoverflow.com/a/312960/ which provides a function to convert the file DERto PEM(suppose I also save the public key from Java).

I tried something like:

$key = openssl_pkey_get_public ("file://pub_key.pem");
$data = null;
openssl_public_decrypt ( file_get_contents ("sig"), $data, $key);
echo $data, "\n";

It successfully decrypts the message, but it is a lot of strange characters.

Our script is a Java client that sends messages to a PHP server, but encrypts the data using a private key. PHP knows about the public key that it must use to decrypt and verify the message.

, , SO, , , .. , .

!

+5
2

" RSA" , " , ", , PKCS # 1, , , . - , java- , PKCS # 1, , .

, openssl_verify PHP, . 0 1, .

, Java , , , , , ( Java, SHA -1 -). PHP - sha1 , sha1 $raw_output, true, , .

+3

$key = openssl_pkey_get_public ("file://pub_key.pem");

, , . ?

-1

All Articles