Java Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

Java Examples

Quick Start
Unicode
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML Conversion
HTTP
IMAP
MHT
MIME
POP3
RSA
S/MIME
SFTP
Signatures
SMTP
Socket / SSL
Spider
SSH
SSH Key
SSH Tunnel
Tar
Upload
XML
XMP
Zip

More Examples...
Amazon S3
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

 

Create PKCS7 Signature using .cer and .key Files

Uses a digital certificate (.cer file) and private key file to create a PKCS7 signature.

 Chilkat Java Library Downloads for Windows, Linux, and MAC OS X

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[])
  {
    //  First, load the .cer and .key files into Chilkat objects...
    CkCert cert = new CkCert();

    boolean success;
    success = cert.LoadFromFile("myCert.cer");
    if (success != true) {
        System.out.println(cert.lastErrorText());
        return;
    }

    CkPrivateKey privKey = new CkPrivateKey();
    String password;
    password = "myPassword";
    //  The private key object provides different methods for
    //  loading keys of many different formats.
    //  This example loads a PKCS8 encrypted private key.
    success = privKey.LoadPkcs8EncryptedFile("myPrivateKey.key",password);
    if (success != true) {
        System.out.println(privKey.lastErrorText());
        return;
    }

    //  NOTE:  In this example, the .cer should contain the public key
    //  that corresponds to the private key.

    CkCrypt2 crypt = new CkCrypt2();

    //  Any string argument automatically begins the 30-day trial.
    success = crypt.UnlockComponent("30-day trial");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    //  Set the certifcate + private key to be used for signing:
    crypt.SetSigningCert2(cert,privKey);

    CkByteData pkcs7 = new CkByteData();

    String textToSign;
    textToSign = "This is the text to be signed.";
    success = crypt.SignString(textToSign,pkcs7);
    if (success == false) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    //  Save the PKCS7 signature to a file.
    success = pkcs7.saveFile("out_pkcs7.p7s");
    if (success == false) {
        System.out.println("Failed to save output file.");
    }
    else {
        System.out.println("Success.");
    }

  }
}

 

© 2000-2010 Chilkat Software, Inc. All Rights Reserved.