Java Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Java Examples

Quick Start
Java Unicode
Java Certs
Java Email
Java Encryption
Java FTP
HTML-to-XML
Java HTTP
Java IMAP
Java MHT
Java MIME
Java RSA
Java S/MIME
Java Signatures
Java Socket
Java Spider
Java Tar
Java Upload
Java XML
Java XMP
Java Zip

More Examples...
Email Object
POP3
SMTP
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
SSH Key
SSH
SSH Tunnel
SFTP

Unreleased...
Bzip2
LZW
Bz2
Icon

 

 

 

 

 

 

 

PKCS7 SignedData Signature (embeds Signed Data)

How to create a PKCS7 signature. The resulting PKCS7 structure is encoded to a printable string using hex or base64 encoding. The data being signed is embedded within the PKCS#7 structure.

Demonstrates how to verify the signature and recover the original data.

Download Chilkat Java Library

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 component unlock failed");
        return;
    }

    CkCert cert = new CkCert();
    success = cert.LoadByCommonName("Chilkat Software");
    if (success != true) {
        System.out.println(cert.lastErrorText());
        return;
    }

    //  Make sure this certificate has a private key available:
    boolean bHasPrivateKey;
    bHasPrivateKey = cert.HasPrivateKey();
    if (bHasPrivateKey != true) {
        System.out.println("No private key available for signing.");
        return;
    }

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

    String strData;
    strData = "This is the data to be signed.";

    //  Indicate that the PKCS7 signature should be returned
    //  as a base64 encoded string:
    crypt.put_EncodingMode("base64");

    //  The EncodingMode may be set to other values such as
    //  "hex", "url", "quoted-printable", etc.

    String strSignatureWithData;
    strSignatureWithData = crypt.opaqueSignStringENC(strData);

    System.out.println(strSignatureWithData);

    //  Now verify the signature against the original data.

    //  Tell the component what certificate to use for verification.
    crypt.SetVerifyCert(cert);

    String originalData;
    originalData = crypt.opaqueVerifyStringENC(strSignatureWithData);
    if (originalData == null ) {
        //  The signature verification failed.
        System.out.println(crypt.lastErrorText());
    }
    else {
        System.out.println(originalData);
        System.out.println("Signature verified!");
    }

  }
}

 

Need a specific example? Send a request to support@chilkatsoft.com

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