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

PowerBuilder 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
Secrets
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

 

 

 

(PowerBuilder) 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

integer li_rc
integer li_Success
oleobject loo_Xml
oleobject loo_BdXml
oleobject loo_Crypt
oleobject loo_Zip
oleobject e
oleobject loo_BdZip
oleobject loo_Prng
oleobject loo_BdAesKey
string ls_IvBytes
oleobject loo_Cert
oleobject loo_PubKey
oleobject loo_Rsa
string ls_FinalXml

// 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>

loo_Xml = create oleobject
// Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_Xml
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Xml.Tag = "InitUpload"
loo_Xml.AddAttribute("xmlns","http://e-dokumenty.mf.gov.pl")
loo_Xml.UpdateChildContent("DocumentType","JPK")
loo_Xml.UpdateChildContent("Version","01.02.01.20160617")
loo_Xml.UpdateAttrAt("EncryptionKey",1,"algorithm","RSA")
loo_Xml.UpdateAttrAt("EncryptionKey",1,"encoding","Base64")
loo_Xml.UpdateAttrAt("EncryptionKey",1,"mode","ECB")
loo_Xml.UpdateAttrAt("EncryptionKey",1,"padding","PKCS#1")
loo_Xml.UpdateChildContent("EncryptionKey","TO BE DETERMINED")
loo_Xml.UpdateAttrAt("DocumentList|Document|FormCode",1,"schemaVersion","1-1")
loo_Xml.UpdateAttrAt("DocumentList|Document|FormCode",1,"systemCode","JPK_VAT (3)")
loo_Xml.UpdateChildContent("DocumentList|Document|FormCode","JPK_VAT")
loo_Xml.UpdateChildContent("DocumentList|Document|FileName","JPK_VAT_3_v1-1_20181201.xml")
loo_Xml.UpdateChildContent("DocumentList|Document|ContentLength","9999")
loo_Xml.UpdateAttrAt("DocumentList|Document|HashValue",1,"algorithm","SHA-256")
loo_Xml.UpdateAttrAt("DocumentList|Document|HashValue",1,"encoding","Base64")
loo_Xml.UpdateChildContent("DocumentList|Document|HashValue","TO BE DETERMINED")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList",1,"filesNumber","1")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Packaging|SplitZip",1,"mode","zip")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Packaging|SplitZip",1,"type","split")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES",1,"block","16")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES",1,"mode","CBC")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES",1,"padding","PKCS#7")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES",1,"size","256")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES|IV",1,"bytes","16")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES|IV",1,"encoding","Base64")
loo_Xml.UpdateChildContent("DocumentList|Document|FileSignatureList|Encryption|AES|IV","TO BE DETERMINED")
loo_Xml.UpdateChildContent("DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber","1")
loo_Xml.UpdateChildContent("DocumentList|Document|FileSignatureList|FileSignature|FileName","JPK_VAT_3_v1-1_20181201-000.xml.zip.aes")
loo_Xml.UpdateChildContent("DocumentList|Document|FileSignatureList|FileSignature|ContentLength","9999")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|FileSignature|HashValue",1,"algorithm","MD5")
loo_Xml.UpdateAttrAt("DocumentList|Document|FileSignatureList|FileSignature|HashValue",1,"encoding","Base64")
loo_Xml.UpdateChildContent("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
loo_BdXml = create oleobject
// Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
li_rc = loo_BdXml.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_BdXml.LoadFile("qa_data/xml_dsig/jpk_vat/JPK_VAT_3_v1-1_20181201-000.xml")
if li_Success <> 1 then
    Write-Debug "Failed to load XML file."
    destroy loo_Xml
    destroy loo_BdXml
    return
end if

loo_Xml.UpdateChildContentInt("DocumentList|Document|ContentLength",loo_BdXml.NumBytes)

loo_Crypt = create oleobject
// Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

loo_Crypt.HashAlgorithm = "sha256"
loo_Crypt.EncodingMode = "base64"
loo_Xml.UpdateChildContent("DocumentList|Document|HashValue",loo_Crypt.HashBdENC(loo_BdXml))

// ------------------------------------------------------------
// Step 2: Create a Zip archive containing the XML.
loo_Zip = create oleobject
// Use "Chilkat_9_5_0.Zip" for versions of Chilkat < 10.0.0
li_rc = loo_Zip.ConnectToNewObject("Chilkat.Zip")

// The filename we pass here doesn't matter because we won't actually be creating a .zip file.
loo_Zip.NewZip("anything.zip")
e = loo_Zip.AppendBd("JPK_VAT_3_v1-1_20181201-000.xml",loo_BdXml)
destroy e
// Write the .zip file to a BinData object.
loo_BdZip = create oleobject
// Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
li_rc = loo_BdZip.ConnectToNewObject("Chilkat.BinData")

loo_Zip.WriteBd(loo_BdZip)

// ------------------------------------------------------------
// Step 3: Generate a random 256-bit AES key (32-bytes)
loo_Prng = create oleobject
// Use "Chilkat_9_5_0.Prng" for versions of Chilkat < 10.0.0
li_rc = loo_Prng.ConnectToNewObject("Chilkat.Prng")

loo_BdAesKey = create oleobject
// Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
li_rc = loo_BdAesKey.ConnectToNewObject("Chilkat.BinData")

loo_Prng.GenRandomBd(32,loo_BdAesKey)
ls_IvBytes = loo_Prng.GenRandom(16,"base64")

// Store the IV (base64 string) in the XML.
loo_Xml.UpdateChildContent("DocumentList|Document|FileSignatureList|Encryption|AES|IV",ls_IvBytes)

// ------------------------------------------------------------
// Step 4: AES encrypt our zip archive (the contents of bdZip)
loo_Crypt.CipherMode = "cbc"
loo_Crypt.KeyLength = 256
loo_Crypt.CryptAlgorithm = "aes"
loo_Crypt.PaddingScheme = 0
loo_Crypt.SetEncodedIV(ls_IvBytes,"base64")
loo_Crypt.SetEncodedKey(loo_BdAesKey.GetEncoded("base64"),"base64")
// AES by definition has a block size of 16.
loo_Crypt.EncryptBd(loo_BdZip)

// 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
li_Success = loo_BdZip.WriteFile("qa_output/JPK_VAT_3_v1-1_20181201-000.xml.zip.aes")
loo_Xml.UpdateChildContentInt("DocumentList|Document|FileSignatureList|FileSignature|ContentLength",loo_BdZip.NumBytes)

// ------------------------------------------------------------
// Step 4: RSA Encrypt the AES key using the public key certificate provided by the Ministry of Finance
loo_Cert = create oleobject
// Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadFromFile("qa_data/pem/mf_public_rsa.pem")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Xml
    destroy loo_BdXml
    destroy loo_Crypt
    destroy loo_Zip
    destroy loo_BdZip
    destroy loo_Prng
    destroy loo_BdAesKey
    destroy loo_Cert
    return
end if

loo_PubKey = loo_Cert.ExportPublicKey()
loo_Rsa = create oleobject
// Use "Chilkat_9_5_0.Rsa" for versions of Chilkat < 10.0.0
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")

loo_Rsa.ImportPublicKeyObj(loo_PubKey)
destroy loo_PubKey

loo_Rsa.EncodingMode = "base64"
loo_Rsa.LittleEndian = 0
// in-place RSA encrypt the contents of bdAesKey.
loo_Rsa.EncryptBd(loo_BdAesKey,0)
loo_Xml.UpdateChildContent("EncryptionKey",loo_BdAesKey.GetEncoded("base64"))

// 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..)
loo_Crypt.HashAlgorithm = "md5"
loo_Xml.UpdateChildContent("DocumentList|Document|FileSignatureList|FileSignature|HashValue",loo_Crypt.HashBdENC(loo_BdZip))

// 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).
ls_FinalXml = loo_Xml.GetXml()
Write-Debug ls_FinalXml

loo_Xml.SaveXml("qa_output/jpk_vat.xml")

Write-Debug "Finished."


destroy loo_Xml
destroy loo_BdXml
destroy loo_Crypt
destroy loo_Zip
destroy loo_BdZip
destroy loo_Prng
destroy loo_BdAesKey
destroy loo_Cert
destroy loo_Rsa

 

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