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();
Signature dsa = Signature.getInstance("SHA1withRSA");
dsa.initSign (priv);
dsa.update ("Hello, World".getBytes(), 0, "Hello, World".length());
byte[] out = dsa.sign();
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, , , .. , .
!