Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Create JPK VAT metadata XML

Demonstrates how to create the JPK VAT metadata XML (InitUpload) that will be signed using XADES.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoXml
    Variant vBdXml
    Handle hoBdXml
    Handle hoCrypt
    Handle hoZip
    Variant vE
    Handle hoE
    Variant vBdZip
    Handle hoBdZip
    Handle hoPrng
    Variant vBdAesKey
    Handle hoBdAesKey
    String sIvBytes
    Handle hoCert
    Variant vPubKey
    Handle hoPubKey
    Handle hoRsa
    String sFinalXml
    String sTemp1
    Integer iTemp1

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

    // First build an InitUpload XML template

    // Use this online tool to generate the code from the sample XML below: 
    // Generate Code to Create XML

    // <InitUpload xmlns="http://e-dokumenty.mf.gov.pl">
    //     <DocumentType>JPK</DocumentType>
    //     <Version>01.02.01.20160617</Version>
    //     <EncryptionKey algorithm="RSA" encoding="Base64" mode="ECB" padding="PKCS#1">F9EhKFec...uWqAWUIg==</EncryptionKey>
    //     <DocumentList>
    //         <Document>
    //             <FormCode schemaVersion="1-1" systemCode="JPK_VAT (3)">JPK_VAT</FormCode>
    //             <FileName>JPK_VAT_3_v1-1_20181201.xml</FileName>
    //             <ContentLength>8736</ContentLength>
    //             <HashValue algorithm="SHA-256" encoding="Base64">JFDI1pItwh6dj/Xe1uts/x61qnjZ4DLHpkZMhmf1oKQ=</HashValue>
    //             <FileSignatureList filesNumber="1">
    //                 <Packaging>
    //                     <SplitZip mode="zip" type="split"/>
    //                 </Packaging>
    //                 <Encryption>
    //                     <AES block="16" mode="CBC" padding="PKCS#7" size="256">
    //                         <IV bytes="16" encoding="Base64">z64oN9zXHt1+S3XACRSCYw==</IV>
    //                     </AES>
    //                 </Encryption>
    //                 <FileSignature>
    //                     <OrdinalNumber>1</OrdinalNumber>
    //                     <FileName>JPK_VAT_3_v1-1_20181201-000.xml.zip.aes</FileName>
    //                     <ContentLength>16</ContentLength>
    //                     <HashValue algorithm="MD5" encoding="Base64">5NX0q1935fvMjLFV7E1yDw==</HashValue>
    //                 </FileSignature>
    //             </FileSignatureList>
    //         </Document>
    //     </DocumentList>
    // </InitUpload>

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "InitUpload"
    Get ComAddAttribute Of hoXml "xmlns" "http://e-dokumenty.mf.gov.pl" To iSuccess
    Send ComUpdateChildContent To hoXml "DocumentType" "JPK"
    Send ComUpdateChildContent To hoXml "Version" "01.02.01.20160617"
    Get ComUpdateAttrAt Of hoXml "EncryptionKey" True "algorithm" "RSA" To iSuccess
    Get ComUpdateAttrAt Of hoXml "EncryptionKey" True "encoding" "Base64" To iSuccess
    Get ComUpdateAttrAt Of hoXml "EncryptionKey" True "mode" "ECB" To iSuccess
    Get ComUpdateAttrAt Of hoXml "EncryptionKey" True "padding" "PKCS#1" To iSuccess
    Send ComUpdateChildContent To hoXml "EncryptionKey" "TO BE DETERMINED"
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FormCode" True "schemaVersion" "1-1" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FormCode" True "systemCode" "JPK_VAT (3)" To iSuccess
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FormCode" "JPK_VAT"
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FileName" "JPK_VAT_3_v1-1_20181201.xml"
    Send ComUpdateChildContent To hoXml "DocumentList|Document|ContentLength" "9999"
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|HashValue" True "algorithm" "SHA-256" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|HashValue" True "encoding" "Base64" To iSuccess
    Send ComUpdateChildContent To hoXml "DocumentList|Document|HashValue" "TO BE DETERMINED"
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList" True "filesNumber" "1" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|Packaging|SplitZip" True "mode" "zip" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|Packaging|SplitZip" True "type" "split" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|Encryption|AES" True "block" "16" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|Encryption|AES" True "mode" "CBC" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|Encryption|AES" True "padding" "PKCS#7" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|Encryption|AES" True "size" "256" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|Encryption|AES|IV" True "bytes" "16" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|Encryption|AES|IV" True "encoding" "Base64" To iSuccess
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FileSignatureList|Encryption|AES|IV" "TO BE DETERMINED"
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber" "1"
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FileSignatureList|FileSignature|FileName" "JPK_VAT_3_v1-1_20181201-000.xml.zip.aes"
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FileSignatureList|FileSignature|ContentLength" "9999"
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|FileSignature|HashValue" True "algorithm" "MD5" To iSuccess
    Get ComUpdateAttrAt Of hoXml "DocumentList|Document|FileSignatureList|FileSignature|HashValue" True "encoding" "Base64" To iSuccess
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FileSignatureList|FileSignature|HashValue" "TO BE DETERMINED"

    // ------------------------------------------------------------
    // Step 1: Load our JPK_VAT XML and update the DocumentList|Document|HashValue
    // and DocumentList|Document|ContentLength
    Get Create (RefClass(cComChilkatBinData)) To hoBdXml
    If (Not(IsComObjectCreated(hoBdXml))) Begin
        Send CreateComObject of hoBdXml
    End
    Get ComLoadFile Of hoBdXml "qa_data/xml_dsig/jpk_vat/JPK_VAT_3_v1-1_20181201-000.xml" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed to load XML file."
        Procedure_Return
    End

    Get ComNumBytes Of hoBdXml To iTemp1
    Send ComUpdateChildContentInt To hoXml "DocumentList|Document|ContentLength" iTemp1

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Set ComHashAlgorithm Of hoCrypt To "sha256"
    Set ComEncodingMode Of hoCrypt To "base64"
    Get ComHashBdENC Of hoCrypt     Get pvComObject of hoBdXml to vBdXml
vBdXml To sTemp1
    Send ComUpdateChildContent To hoXml "DocumentList|Document|HashValue" sTemp1

    // ------------------------------------------------------------
    // Step 2: Create a Zip archive containing the XML.
    Get Create (RefClass(cComChilkatZip)) To hoZip
    If (Not(IsComObjectCreated(hoZip))) Begin
        Send CreateComObject of hoZip
    End
    // The filename we pass here doesn't matter because we won't actually be creating a .zip file.
    Get ComNewZip Of hoZip "anything.zip" To iSuccess
    Get pvComObject of hoBdXml to vBdXml
    Get ComAppendBd Of hoZip "JPK_VAT_3_v1-1_20181201-000.xml" vBdXml To vE
    If (IsComObject(vE)) Begin
        Get Create (RefClass(cComChilkatZipEntry)) To hoE
        Set pvComObject Of hoE To vE
    End
    Send Destroy of e
    // Write the .zip file to a BinData object.
    Get Create (RefClass(cComChilkatBinData)) To hoBdZip
    If (Not(IsComObjectCreated(hoBdZip))) Begin
        Send CreateComObject of hoBdZip
    End
    Get pvComObject of hoBdZip to vBdZip
    Get ComWriteBd Of hoZip vBdZip To iSuccess

    // ------------------------------------------------------------
    // Step 3: Generate a random 256-bit AES key (32-bytes)
    Get Create (RefClass(cComChilkatPrng)) To hoPrng
    If (Not(IsComObjectCreated(hoPrng))) Begin
        Send CreateComObject of hoPrng
    End
    Get Create (RefClass(cComChilkatBinData)) To hoBdAesKey
    If (Not(IsComObjectCreated(hoBdAesKey))) Begin
        Send CreateComObject of hoBdAesKey
    End
    Get pvComObject of hoBdAesKey to vBdAesKey
    Get ComGenRandomBd Of hoPrng 32 vBdAesKey To iSuccess
    Get ComGenRandom Of hoPrng 16 "base64" To sIvBytes

    // Store the IV (base64 string) in the XML.
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FileSignatureList|Encryption|AES|IV" sIvBytes

    // ------------------------------------------------------------
    // Step 4: AES encrypt our zip archive (the contents of bdZip)
    Set ComCipherMode Of hoCrypt To "cbc"
    Set ComKeyLength Of hoCrypt To 256
    Set ComCryptAlgorithm Of hoCrypt To "aes"
    Set ComPaddingScheme Of hoCrypt To 0
    Send ComSetEncodedIV To hoCrypt sIvBytes "base64"
    Get ComGetEncoded Of hoBdAesKey "base64" To sTemp1
    Send ComSetEncodedKey To hoCrypt sTemp1 "base64"
    // AES by definition has a block size of 16.
    Get pvComObject of hoBdZip to vBdZip
    Get ComEncryptBd Of hoCrypt vBdZip To iSuccess

    // bdZip now contains the AES encrypted data. 
    // Note: This is NOT the same as a zip where the contents are AES encrypted.
    // In that case, we have an unencrypted zip structure with AES encrypted files within.
    // In our case, the entire zip file image is encrypted.

    // Save the bdZip to a file.  This is what will get sent to e-dokumenty.mf.gov.pl
    Get ComWriteFile Of hoBdZip "qa_output/JPK_VAT_3_v1-1_20181201-000.xml.zip.aes" To iSuccess
    Get ComNumBytes Of hoBdZip To iTemp1
    Send ComUpdateChildContentInt To hoXml "DocumentList|Document|FileSignatureList|FileSignature|ContentLength" iTemp1

    // ------------------------------------------------------------
    // Step 4: RSA Encrypt the AES key using the public key certificate provided by the Ministry of Finance
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromFile Of hoCert "qa_data/pem/mf_public_rsa.pem" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComExportPublicKey Of hoCert To vPubKey
    If (IsComObject(vPubKey)) Begin
        Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey
        Set pvComObject Of hoPubKey To vPubKey
    End
    Get Create (RefClass(cComChilkatRsa)) To hoRsa
    If (Not(IsComObjectCreated(hoRsa))) Begin
        Send CreateComObject of hoRsa
    End
    Get ComImportPublicKeyObj Of hoRsa vPubKey To iSuccess
    Send Destroy of hoPubKey

    Set ComEncodingMode Of hoRsa To "base64"
    Set ComLittleEndian Of hoRsa To False
    // in-place RSA encrypt the contents of bdAesKey.
    Get pvComObject of hoBdAesKey to vBdAesKey
    Get ComEncryptBd Of hoRsa vBdAesKey False To iSuccess
    Get ComGetEncoded Of hoBdAesKey "base64" To sTemp1
    Send ComUpdateChildContent To hoXml "EncryptionKey" sTemp1

    // Step 5: We forgot to get the MD5 hash of the AES encrypted zip.
    // (I'm assuming we need the MD5 of the encrypted zip as opposed to the MD5 of the pre-encrypted zip..)
    Set ComHashAlgorithm Of hoCrypt To "md5"
    Get ComHashBdENC Of hoCrypt     Get pvComObject of hoBdZip to vBdZip
vBdZip To sTemp1
    Send ComUpdateChildContent To hoXml "DocumentList|Document|FileSignatureList|FileSignature|HashValue" sTemp1

    // At this point, the XML is prepared and the AES encrypted image of the zip file is written
    // to a file (and also in bdZip).
    Get ComGetXml Of hoXml To sFinalXml
    Showln sFinalXml

    Get ComSaveXml Of hoXml "qa_output/jpk_vat.xml" To iSuccess

    Showln "Finished."


End_Procedure

 

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