Java Requires Chilkat v10.1.2+
Chilkat2 Java
RSA Sign String using Private Key of Certificate Type A3 (smart card / token)
See more RSA Examples
Demonstrates RSA signing a string using the private key of a certificate type A3 (smart card, token).Note: This is a Windows-only example.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.CertStore;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rsa;
public class ChilkatExample {
public static void main(String[] args) {
// First get the A3 certificate that was installed on the Windows system.
CertStore certStore = new CertStore();
String thumbprint = "12c1dd8015f3f03f7b1fa619dc24e2493ca8b4b2";
// This is specific to Windows because it is opening the Windows Current-User certificate store.
boolean bReadOnly = true;
try {
certStore.openCurrentUserStore(bReadOnly);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Find the certificate with the desired thumbprint
// (There are many ways to locate a certificate. This example chooses to find by thumbprint.)
JsonObject json = new JsonObject();
json.updateString("thumbprint", thumbprint);
Cert cert = new Cert();
try {
certStore.findCert(json, cert);
} catch (ChilkatException e) {
System.out.println("Failed to find the certificate.");
return;
}
System.out.println("Found: " + cert.getSubjectCN());
Rsa rsa = new Rsa();
// Provide the cert's private key
boolean bUsePrivateKey = true;
String sigBase64;
try {
rsa.setX509Cert(cert, bUsePrivateKey);
// Return the RSA signature in base64 encoded form.
rsa.setEncodingMode("base64");
// Sign the utf-8 byte representation of the string.
rsa.setCharset("utf-8");
// You can also choose other hash algorithms, such as SHA-1.
sigBase64 = rsa.signStringENC("text to sign", "SHA-256");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Base64 signature: " + sigBase64);
}
}