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

RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature

See more RSA Examples

Demonstrates how to RSA sign something using a private key loaded from a .pfx/.p12. The RSA signature is returned in Base64 encoded format.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Pfx;
import com.chilkatsoft.chilkat2.PrivateKey;
import com.chilkatsoft.chilkat2.Rsa;

public class ChilkatExample {

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

        Rsa rsa = new Rsa();

        // Load the .pfx/.p12
        Pfx pfx = new Pfx();
        try {
            pfx.loadPfxFile("qa_data/pfx/myKey.p12", "myPassword");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the default private key.

        PrivateKey privKey = new PrivateKey();
        try {
            pfx.privateKeyAt(0, privKey);

            // Import the private key into the RSA component:
            rsa.usePrivateKey(privKey);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the signature in base64
        rsa.setEncodingMode("base64");

        String strData = "This is the string to be signed.";

        // Sign the string using the sha256 hash algorithm.
        // Other valid choices are "sha384", "sha512", "sha-1", "md2" and "md5".
        String base64Sig = rsa.signStringENC(strData, "sha256");

        System.out.println(base64Sig);

        System.out.println("Success!");
    }
}