Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
|
Sign a Hash to create PKCS7 OutputUses a digital certificate to sign a SHA-1 hash to produce PKCS7 output.
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.