Java
Java
Duplicating OpenSSL rsautl (creating RSA signatures)
See more RSA Examples
Demonstrates how to duplicate OpenSSL rsautil RSA signatures.The Chilkat RSA component's methods for creating RSA signatures (SignBytes, SignBytesENC, SignString, and SignStringENC) are very different from OpenSSL's rsautl command. First, we'll explain what Chilkat's signing methods do, and then what OpenSSL's rsautl does. New signing methods have been added to Chilkat RSA to duplicate OpenSSL rsautl: OpenSslSignBytes, OpenSslSignBytesENC, OpenSslSignString, and OpenSslSignStringENC.
Here's what Chilkat's RSA Sign* methods do:
(Note: Chilkat RSA's Sign* methods generate signatures according to RSA Laboratories' "PKCS #1 v2.0: RSA Cryptography Standard".)
- Input data is hashed using the hashing algorithm specified.
- The hash is padded using either PKCS1 v1.5 or PKCS1 PSS padding. PKCS v1.5 involves encoding to ASN.1 before padding.
- The padded hash is finally RSA signed (i.e. modular exponentiation) and the signature is returned.
OpenSSL rsautl is very different. Here's what it does:
- The input data is not hashed. It must be a small enough amount of data such that it can be padded and signed. PKCS v1.5 padding is always used. The data is not ASN.1 encoded before padding.
- The PKCS v1.5 padded data is RSA signed and the signature is returned.
Chilkat Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkPrivateKey privKey = new CkPrivateKey();
// Load the private key from an RSA PEM file:
success = privKey.LoadPemFile("private.pem");
if (success == false) {
System.out.println(privKey.lastErrorText());
return;
}
CkRsa rsa = new CkRsa();
success = rsa.UsePrivateKey(privKey);
if (success == false) {
System.out.println(rsa.lastErrorText());
return;
}
String strData = "secret";
CkBinData bd = new CkBinData();
bd.AppendString(strData,"utf-8");
success = rsa.SignRawBd(bd);
String hexSig = bd.getEncoded("hex");
System.out.println(hexSig);
// Recover the data using the corresponding public key:
CkPublicKey pubKey = new CkPublicKey();
// Load the public key from a PEM file:
success = pubKey.LoadFromFile("public.pem");
if (success == false) {
System.out.println(pubKey.lastErrorText());
return;
}
CkRsa rsa2 = new CkRsa();
success = rsa2.UsePublicKey(pubKey);
if (success == false) {
System.out.println(rsa2.lastErrorText());
return;
}
CkBinData bd2 = new CkBinData();
bd2.AppendEncoded(hexSig,"hex");
// Recover the original data.
success = rsa2.VerifyRawBd(bd2);
if (success == false) {
System.out.println(rsa2.lastErrorText());
return;
}
String originalData = bd2.getString("utf-8");
System.out.println(originalData);
}
}