Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#C# WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicPythonRubySQL ServerSwiftUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET WinRTVBScript

Java Examples

Async
Certificates
Email Object
Encryption
FTP
HTML-to-XML/Text
Gzip
HTTP
IMAP
MHT / HTML Email
PEM
PFX/P12
Java KeyStore (JKS)
POP3
RSA Encryption
MIME
SCP
SMTP
Socket/SSL/TLS
SSH Key
SSH
SFTP
Tar Archive
XML
XMP
Zip

More Examples...
ASN.1
PRNG
Amazon S3
Bounced Email
CSV
Diffie-Hellman
DKIM / DomainKey
NTLM

 

 

 

 

 

 

 

(Java) Create S/MIME with Detached Signature

Java example to create a simple S/MIME message with a detached digital signature.

Important: This example is specific to the Windows operating system because it obtains the certificate and private key from the Windows registry-based certificate store. For other operating systems, see the PKCS7 signature examples that use PFX (.p12) files.

Chilkat Java Downloads

Java Libs for Windows, Linux, MAC OS X, Solaris,
ARM Embedded Linux, and PowerLinux

Java Libs for Android

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

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

    //  Find our digital certificate from the 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;
    }

    //  Our MIME object is currently empty.  Add some header fields
    //  and a body:
    mime.AddHeaderField("subject","this is a test");
    mime.AddHeaderField("test123","this is a test 123");
    mime.put_ContentType("text/plain");
    mime.put_Charset("iso-8859-1");
    mime.SetBodyFromPlainText("This is the body");

    System.out.println("Original MIME message:");
    System.out.println(mime.getMime());

    //  The original MIME looks like this:
    //  ----------------------------------
    //  subject: this is a test
    //  test123: this is a test 123
    //  content-type: text/plain;
    //  	 charset="iso-8859-1"
    // 
    //  This is the body
    //  ----------------------------------

    //  Add a detached signature.  Transfer the header fields
    //  from our existing MIME to what will become the new outer
    //  envelope.
    boolean transferHeaderFields;
    transferHeaderFields = true;
    success = mime.AddDetachedSignature2(cert,transferHeaderFields);
    if (success != true) {
        System.out.println(mime.lastErrorText());
        return;
    }

    System.out.println("----------------------------------");
    System.out.println("S/MIME with detached signature:");
    System.out.println(mime.getMime());

    System.out.println("Success!");

    //  The signed MIME looks like this:
    //  --------------------------------
    //  content-type: multipart/signed;
    //  	boundary="----=_NextPart_e7c_2b68_282042d9.6ba23d56";
    //  	protocol="application/x-pkcs7-signature";
    //  	micalg=SHA1
    //  subject: this is a test
    //  test123: this is a test 123
    // 
    //  This is a multi-part message in MIME format.
    // 
    //  ------=_NextPart_e7c_2b68_282042d9.6ba23d56
    //  content-type: text/plain;
    //  	 charset="iso-8859-1"
    // 
    //  This is the body
    //  ------=_NextPart_e7c_2b68_282042d9.6ba23d56
    //  content-transfer-encoding: base64
    //  content-type: application/x-pkcs7-signature;
    //  	name="smime.p7s"
    //  content-disposition: attachment;
    //  	 filename="smime.p7s"
    // 
    //  MIIGAAYJKoZIhvcNAQcCoIIF8TCCBe0CAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHAaCC
    //  A9kwggPVMIIDPqADAgECAhB4ouTcAmLszrGi170k1deSMA0GCSqGSIb3DQEBBQUAMFUxCzAJ
    //  BgNVBAYTAlpBMSUwIwYDVQQKExxUaGF3dGUgQ29uc3VsdGluZyAoUHR5KSBMdGQuMR8wHQYD
    //  VQQDExZUaGF3dGUgQ29kZSBTaWduaW5nIENBMB4XDTA3MDExNTAwMDAwMFoXDTA4MDExNTIz
    //  NTk1OVowgZ0xCzAJBgNVBAYTAlVTMREwDwYDVQQIEwhJbGxpbm9pczEQMA4GA1UEBxMHV2hl
    //  YXRvbjEfMB0GA1UEChMWQ2hpbGthdCBTb2Z0d2FyZSwgSW5jLjEnMCUGA1UECxMeU2VjdXJl
    //  IEFwcGxpY2F0aW9uIERldmVsb3BtZW50MR8wHQYDVQQDExZDaGlsa2F0IFNvZnR3YXJlLCBJ
    //  bmMuMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwR2t6b/Kn4VzNCOlj1YGkHxD
    //  YPVnfZNIeO7hmutreK6kr94xm9RlQn+vkN1uRMhvSbOdds+B25o+AyAeuBmLsuxoNRCC9AUb
    //  a37pY8eL04AaTEAm350tfnHGT44joSdFPoW5D91TDare6q5UaaccdEtXU4/LGqh+nHB9Idft
    //  0S/rIpP2PK9TAwQ5V8dlMuBQQA+BUCg5MY9oqZgtnkX7O6OJSk8MT0lUhVg7EAge9iUx7Eu4
    //  oWK88fkOo81pqHogTpGfy4nibWg72XkPk135leMw/Jme37q7OR/zm6HceFlyyO0WL1fYx09P
    //  pYbj1lYNvVo8n38ohT8T/7h3N4GUlQIDAQABo4HYMIHVMAwGA1UdEwEB/wQCMAAwPgYDVR0f
    //  BDcwNTAzoDGgL4YtaHR0cDovL2NybC50aGF3dGUuY29tL1RoYXd0ZUNvZGVTaWduaW5nQ0Eu
    //  Y3JsMB8GA1UdJQQYMBYGCCsGAQUFBwMDBgorBgEEAYI3AgEWMB0GA1UdBAQWMBQwDjAMBgor
    //  BgEEAYI3AgEWAwIHgDAyBggrBgEFBQcBAQQmMCQwIgYIKwYBBQUHMAGGFmh0dHA6Ly9vY3Nw
    //  LnRoYXd0ZS5jb20wEQYJYIZIAYb4QgEBBAQDAgQQMA0GCSqGSIb3DQEBBQUAA4GBAAUMcxUb
    //  eo5HTDKMaxMFFy3iA9i/ke4VLFw97yEG3raLTHZUlRwVxwLGLmW81dY/64nEDEgKi3Ifuohf
    //  A0a3N3Ld7zwL1f/hdG2pQigLcvMymP9jj6DEQxOs03iADFguMpqkb79hbbURzsKX7pvWuNjQ
    //  VGiowTuo1JXuSbsqaW7vMYIB7zCCAesCAQEwaTBVMQswCQYDVQQGEwJaQTElMCMGA1UEChMc
    //  VGhhd3RlIENvbnN1bHRpbmcgKFB0eSkgTHRkLjEfMB0GA1UEAxMWVGhhd3RlIENvZGUgU2ln
    //  bmluZyBDQQIQeKLk3AJi7M6xote9JNXXkjAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsG
    //  CSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMDcwNDA0MTkzMDUyWjAjBgkqhkiG9w0BCQQx
    //  FgQUevptGkutc0lb6gI3NPHovx4Di34wDQYJKoZIhvcNAQEBBQAEggEAk4jqKMsn7D+MjGtY
    //  M9bnLdekBSxMVWPpKxk6hWg0NVOoiBOd/OOyH8FO/r7trmzr2Z9eJf1zpdjzpSPCwnGJzIy9
    //  RunBBTJ+BxCbDXfjwJJTYVy1Uhsedv+o7Gfj9LlAVysNLjda5VcU7x/OOBVst034X+map3ds
    //  85yBrfcJ/LyQs/lyZYmSPgOpFIBSy3oULPzYt45EUVsuk5qSZ1MA4HeIDW37b8OfQOW8pLEL
    //  cBRducU19lpDOp5hBRZDR3zu+/9ddWKxKvcH04WitYRBi5TtqhQxpep+vRhJX8EPY6cgenJ+
    //  lbDURg552YftF4EpDGALADR41j6sUMzb0QOz4A==
    // 
    //  ------=_NextPart_e7c_2b68_282042d9.6ba23d56--
    // 
    // 

  }
}

 

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