Sample code for 30+ languages & platforms
AutoIt

Sign JSON to Create CAdES P7S Bytes

See more CAdES Examples

Demonstrates how to sign JSON using a certificate + private key from a .p12/.pfx to create a CAdES P7S byte array.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oCrypt = ObjCreate("Chilkat.Crypt2")

$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
If ($bSuccess <> True) Then
    ConsoleWrite($oCert.LastErrorText & @CRLF)
    Exit
EndIf

;  Tell the crypt component to use this cert.
$bSuccess = $oCrypt.SetSigningCert($oCert)
If ($bSuccess <> True) Then
    ConsoleWrite($oCrypt.LastErrorText & @CRLF)
    Exit
EndIf

;  The CadesEnabled property applies to all methods that create PKCS7 signatures. 
;  To create a CAdES-BES signature, set this property equal to true. 
$oCrypt.CadesEnabled = True

$oCrypt.HashAlgorithm = "sha256"

$oJsonSigningAttrs = ObjCreate("Chilkat.JsonObject")
$oJsonSigningAttrs.UpdateInt("contentType",1)
$oJsonSigningAttrs.UpdateInt("signingTime",1)
$oJsonSigningAttrs.UpdateInt("messageDigest",1)
$oJsonSigningAttrs.UpdateInt("signingCertificateV2",1)
$oCrypt.SigningAttributes = $oJsonSigningAttrs.Emit()

;  By default, all the certs in the chain of authentication are included in the signature.
;  If desired, we can choose to only include the signing certificate:
$oCrypt.IncludeCertChain = False

;  Create the CAdES-BES attached signature, which contains the original data.
$oCrypt.Charset = "utf-8"
Local $oCadesP7s
$oCadesP7s = $oCrypt.OpaqueSignString("{ ""abc"": 123}")
If ($oCrypt.LastMethodSuccess = False) Then
    ConsoleWrite($oCrypt.LastErrorText & @CRLF)
    Exit
EndIf

;  Verify the signature and extract the original JSON:
Local $sOriginalJson = $oCrypt.OpaqueVerifyString($oCadesP7s)
If ($oCrypt.LastMethodSuccess = False) Then
    ConsoleWrite($oCrypt.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Original JSON: " & $sOriginalJson & @CRLF)

ConsoleWrite("Success!" & @CRLF)