Sample code for 30+ languages & platforms
AutoIt

Duplicate OpensSSL to Sign File and Output Binary DER

See more OpenSSL Examples

This example duplicates the following:
openssl smime -sign -in INPUT.xml -signer SIGN.PEM -passin pass:MYPASS -outform der -binary -nodetach -out SIGNED.P7M

Note: Although "smime" is the OpenSSL command, it's not actually producing S/MIME. The arguments "-outform der -binary" indicates that the output is binary DER (i.e. the PKCS7 binary signature). The input can be any type of file: XML, PDF, JPG, ... *anything*...

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

$oCrypt = ObjCreate("Chilkat.Crypt2")

; Load the PEM containing cert + private key.
$oPem = ObjCreate("Chilkat.Pem")
$bSuccess = $oPem.LoadPemFile("qa_data/pem/myPem.pem","password")
If ($bSuccess = False) Then
    ConsoleWrite($oPem.LastErrorText & @CRLF)
    Exit
EndIf

$oPrivkey = ObjCreate("Chilkat.PrivateKey")
$bSuccess = $oPem.PrivateKeyAt(0,$oPrivkey)
If ($bSuccess = False) Then
    ConsoleWrite($oPem.LastErrorText & @CRLF)
    Exit
EndIf

$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oPem.CertAt(0,$oCert)
If ($bSuccess = False) Then
    ConsoleWrite($oPem.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oCrypt.SetSigningCert2($oCert,$oPrivkey)
If ($bSuccess = False) Then
    ConsoleWrite($oCrypt.LastErrorText & @CRLF)
    Exit
EndIf

; Alternatively, we could use a .pfx/.p12 file.
; (Chilkat also supports other formats/sources for cert/private keys...)
$oCertFromP12 = ObjCreate("Chilkat.Cert")
$bSuccess = $oCertFromP12.LoadPfxFile("qa_data/p12/myP12.p12","password")
If ($bSuccess = False) Then
    ConsoleWrite($oCertFromP12.LastErrorText & @CRLF)
    Exit
EndIf

; The certificate, when loaded from a .pfx/.p12, will automatically 
; include the associated private key, assuming it's present in the .p12.
; We don't have to explicitly provide the private key as in the
; lines of code above that use the PEM file.
$bSuccess = $oCrypt.SetSigningCert($oCertFromP12)
If ($bSuccess = False) Then
    ConsoleWrite($oCrypt.LastErrorText & @CRLF)
    Exit
EndIf

; Create the opaque signature (PKCS7 binary DER that contains both the signature and original file data).
$bSuccess = $oCrypt.CreateP7M("qa_data/infile.anything","qa_output/outfile.p7m")
If ($bSuccess = False) Then
    ConsoleWrite($oCrypt.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Success." & @CRLF)