Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Duplicate openssl dgst -sha256 -verify pubKey.pem -signature signature.sig in.dat

See more OpenSSL Examples

Demonstrates how to duplicate this OpenSSL command:
openssl dgst -sha256 -verify pubKey.pem -signature signature.sig in.dat
The in.dat file contains the original data that was signed, and can contain text or binary data of any type. The above OpenSSL command does the following:
  1. Creates a SHA256 digest of the contents of the input file.
  2. Verifies the SHA256 digest using the public key.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.PublicKey;
import com.chilkatsoft.chilkat2.Rsa;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        PublicKey pubKey = new PublicKey();

        // Load the public key from an PEM file:
        try {
            pubKey.loadFromFile("pubKey.pem");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Load the data of the original file that was signed.
        BinData bdFileData = new BinData();
        bdFileData.loadFile("in.dat");

        // Load the signature.
        BinData bdSig = new BinData();
        bdSig.loadFile("signature.sig");

        Rsa rsa = new Rsa();

        // Import the public key into the RSA component:
        try {
            rsa.usePublicKey(pubKey);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // OpenSSL uses big-endian.
        rsa.setLittleEndian(false);

        try {
            rsa.verifyBd(bdFileData, "sha256", bdSig);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            System.out.println("The signature was invalid.");
            return;
        }

        System.out.println("The signature was verified.");
    }
}