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

 

 

 

 

 

 

 

Sign a Hash to create PKCS7 Output

Uses a digital certificate to sign a SHA-1 hash to produce PKCS7 output.

 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[])
  {
    CkCrypt2 crypt = new CkCrypt2();

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

    //  First, create an SHA-1 hash to be signed:
    String toBeHashed;
    toBeHashed = "This is a test";

    CkByteData hash = new CkByteData();
    crypt.put_HashAlgorithm("sha1");

    success = crypt.HashString(toBeHashed,hash);
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    //  Locate a digital certificate from the registry-based
    //  Current User certificate store.
    CkCreateCS ccs = new CkCreateCS();
    CkCertStore certStore;
    certStore = ccs.OpenCurrentUserStore();
    CkCert cert;
    cert = certStore.FindCertBySubjectCN("Chilkat Software, Inc.");
    if (cert == null ) {
        System.out.println(cert.lastErrorText());

        return;
    }

    //  Tell the encryption component to use this cert.
    crypt.SetSigningCert(cert);

    //  Tell the encryption component to use public-key encryption:
    crypt.put_CryptAlgorithm("pki");

    CkByteData pkcs7 = new CkByteData();

    success = crypt.SignBytes(hash,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.