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

RSA Sign using Base64 Private Key

See more RSA Examples

Signs a string using a non-encrypted RSA private key in base64 encoding. Returns the RSA signature as a base64 string.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.PrivateKey;
import com.chilkatsoft.chilkat2.Rsa;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

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

        PrivateKey privKey = new PrivateKey();

        StringBuilder sbPem = new StringBuilder();
        sbPem.appendLine("-----BEGIN RSA PRIVATE KEY-----", true);
        sbPem.appendLine("MIIC .... j5A==", true);
        sbPem.appendLine("-----END RSA PRIVATE KEY-----", true);

        try {
            privKey.loadPem(sbPem.getAsString());
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Rsa rsa = new Rsa();

        try {
            rsa.usePrivateKey(privKey);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        BinData bd = new BinData();
        bd.appendString("12345678", "utf-8");

        try {
            rsa.signRawBd(bd);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the base64 RSA signature.
        System.out.println(bd.getEncoded("base64"));

        try {
            rsa.verifyRawBd(bd);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        String strOriginal = bd.getString("utf-8");
        System.out.println(strOriginal);
    }
}