0

I have some data which has been signed using something like the following:

openssl rsautl -sign -in file -inkey PrivateKey.pem -out sig

The signed data can then be recovered using the following:

openssl rsautl -pubin -inkey PublicKey.pem -verify -in sig -out file

I've converted the public key into a Java PublicKey object and looked at using that with the Signature class in java.security, but that doesn't appear to have a method that allows recovering the original signed data.

Is there a way to recover the signed data using Java?

jab351w
  • 1
  • 2
  • Possible duplicate of [RSA Encryption / Decryption using Java](https://stackoverflow.com/questions/19623367/rsa-encryption-decryption-using-java) – Denis S. Aug 02 '19 at 22:36
  • 2
    @DenisS.+ RSA sign/verify is NOT the same as encrypt/decrypt; this is discussed repeatedly on crypto.SX and security.SX. But due to a mathematical symmetry that initially misled people, Java (AFAICT undocumented) allows a SunJCE `Cipher.getInstance("RSA")` to be `.init(DECRYPT_MODE, RSAPublicKey)` and then does the partial PKCS1v1.5 'recover' operation wanted here. (`openssl rsautl` does _not_ do the ASN.1 encoding/decoding required for standard RSASSA-PKCS1v1_5; `openssl pkeyutl (RSAkey) -pkeyopt digest:$dig` or `openssl dgst -sign/verify (RSA)` does. See many existing Qs.) – dave_thompson_085 Aug 03 '19 at 04:45

1 Answers1

0

As suggested in the comment by dave_thompson_085, the following can be used to recover the data:

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);

byte[] extractedBytes = cipher.doFinal(sig);
jab351w
  • 1
  • 2