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

Delphi ActiveX 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

 

 

 

(Delphi ActiveX) Create JPK VAT metadata XML

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

Chilkat for Delphi Downloads

Chilkat ActiveX DLL for Delphi

Chilkat non-ActiveX DLL for Delphi

* The examples here use the ActiveX DLL.

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
xml: TChilkatXml;
bdXml: TChilkatBinData;
crypt: TChilkatCrypt2;
zip: TChilkatZip;
e: IChilkatZipEntry;
bdZip: TChilkatBinData;
prng: TChilkatPrng;
bdAesKey: TChilkatBinData;
ivBytes: WideString;
cert: TChilkatCert;
pubKey: IPublicKey;
rsa: TChilkatRsa;
finalXml: WideString;

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

xml := TChilkatXml.Create(Self);
xml.Tag := 'InitUpload';
xml.AddAttribute('xmlns','http://e-dokumenty.mf.gov.pl');
xml.UpdateChildContent('DocumentType','JPK');
xml.UpdateChildContent('Version','01.02.01.20160617');
xml.UpdateAttrAt('EncryptionKey',1,'algorithm','RSA');
xml.UpdateAttrAt('EncryptionKey',1,'encoding','Base64');
xml.UpdateAttrAt('EncryptionKey',1,'mode','ECB');
xml.UpdateAttrAt('EncryptionKey',1,'padding','PKCS#1');
xml.UpdateChildContent('EncryptionKey','TO BE DETERMINED');
xml.UpdateAttrAt('DocumentList|Document|FormCode',1,'schemaVersion','1-1');
xml.UpdateAttrAt('DocumentList|Document|FormCode',1,'systemCode','JPK_VAT (3)');
xml.UpdateChildContent('DocumentList|Document|FormCode','JPK_VAT');
xml.UpdateChildContent('DocumentList|Document|FileName','JPK_VAT_3_v1-1_20181201.xml');
xml.UpdateChildContent('DocumentList|Document|ContentLength','9999');
xml.UpdateAttrAt('DocumentList|Document|HashValue',1,'algorithm','SHA-256');
xml.UpdateAttrAt('DocumentList|Document|HashValue',1,'encoding','Base64');
xml.UpdateChildContent('DocumentList|Document|HashValue','TO BE DETERMINED');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList',1,'filesNumber','1');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|Packaging|SplitZip',1,'mode','zip');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|Packaging|SplitZip',1,'type','split');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|Encryption|AES',1,'block','16');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|Encryption|AES',1,'mode','CBC');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|Encryption|AES',1,'padding','PKCS#7');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|Encryption|AES',1,'size','256');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|Encryption|AES|IV',1,'bytes','16');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|Encryption|AES|IV',1,'encoding','Base64');
xml.UpdateChildContent('DocumentList|Document|FileSignatureList|Encryption|AES|IV','TO BE DETERMINED');
xml.UpdateChildContent('DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber','1');
xml.UpdateChildContent('DocumentList|Document|FileSignatureList|FileSignature|FileName','JPK_VAT_3_v1-1_20181201-000.xml.zip.aes');
xml.UpdateChildContent('DocumentList|Document|FileSignatureList|FileSignature|ContentLength','9999');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|FileSignature|HashValue',1,'algorithm','MD5');
xml.UpdateAttrAt('DocumentList|Document|FileSignatureList|FileSignature|HashValue',1,'encoding','Base64');
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
bdXml := TChilkatBinData.Create(Self);
success := bdXml.LoadFile('qa_data/xml_dsig/jpk_vat/JPK_VAT_3_v1-1_20181201-000.xml');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Failed to load XML file.');
    Exit;
  end;

xml.UpdateChildContentInt('DocumentList|Document|ContentLength',bdXml.NumBytes);

crypt := TChilkatCrypt2.Create(Self);
crypt.HashAlgorithm := 'sha256';
crypt.EncodingMode := 'base64';
xml.UpdateChildContent('DocumentList|Document|HashValue',crypt.HashBdENC(bdXml.ControlInterface));

// ------------------------------------------------------------
// Step 2: Create a Zip archive containing the XML.
zip := TChilkatZip.Create(Self);
// The filename we pass here doesn't matter because we won't actually be creating a .zip file.
zip.NewZip('anything.zip');
e := zip.AppendBd('JPK_VAT_3_v1-1_20181201-000.xml',bdXml.ControlInterface);

// Write the .zip file to a BinData object.
bdZip := TChilkatBinData.Create(Self);
zip.WriteBd(bdZip.ControlInterface);

// ------------------------------------------------------------
// Step 3: Generate a random 256-bit AES key (32-bytes)
prng := TChilkatPrng.Create(Self);
bdAesKey := TChilkatBinData.Create(Self);
prng.GenRandomBd(32,bdAesKey.ControlInterface);
ivBytes := prng.GenRandom(16,'base64');

// Store the IV (base64 string) in the XML.
xml.UpdateChildContent('DocumentList|Document|FileSignatureList|Encryption|AES|IV',ivBytes);

// ------------------------------------------------------------
// Step 4: AES encrypt our zip archive (the contents of bdZip)
crypt.CipherMode := 'cbc';
crypt.KeyLength := 256;
crypt.CryptAlgorithm := 'aes';
crypt.PaddingScheme := 0;
crypt.SetEncodedIV(ivBytes,'base64');
crypt.SetEncodedKey(bdAesKey.GetEncoded('base64'),'base64');
// AES by definition has a block size of 16.
crypt.EncryptBd(bdZip.ControlInterface);

// 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
success := bdZip.WriteFile('qa_output/JPK_VAT_3_v1-1_20181201-000.xml.zip.aes');
xml.UpdateChildContentInt('DocumentList|Document|FileSignatureList|FileSignature|ContentLength',bdZip.NumBytes);

// ------------------------------------------------------------
// Step 4: RSA Encrypt the AES key using the public key certificate provided by the Ministry of Finance
cert := TChilkatCert.Create(Self);
success := cert.LoadFromFile('qa_data/pem/mf_public_rsa.pem');
if (success <> 1) then
  begin
    Memo1.Lines.Add(cert.LastErrorText);
    Exit;
  end;
pubKey := cert.ExportPublicKey();
rsa := TChilkatRsa.Create(Self);
rsa.ImportPublicKeyObj(pubKey);

rsa.EncodingMode := 'base64';
rsa.LittleEndian := 0;
// in-place RSA encrypt the contents of bdAesKey.
rsa.EncryptBd(bdAesKey.ControlInterface,0);
xml.UpdateChildContent('EncryptionKey',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..)
crypt.HashAlgorithm := 'md5';
xml.UpdateChildContent('DocumentList|Document|FileSignatureList|FileSignature|HashValue',crypt.HashBdENC(bdZip.ControlInterface));

// 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).
finalXml := xml.GetXml();
Memo1.Lines.Add(finalXml);

xml.SaveXml('qa_output/jpk_vat.xml');

Memo1.Lines.Add('Finished.');
end;

 

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