As I understand, a digital signature is obtained by encrypting (the message digest) with your own private key and then the recipient’s public key.
In Java, I can’t figure out how to use a public key to perform the second stage of signing.
Signature sig = Signature.getInstance("MD5WithRSA");
sig.initSign(privateKey);
sig.update(data);
byte[] signatureBytes = sig.sign();
I checked the API, the only methods that use a public key are for verifying…
There’s no recipient involved in a message signature. You can encrypt the signature to the recipient’s public key if you want to prevent anyone else from verifying it, but that’s not generally considered part of signing the message.