Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Lianja) Create JPK VAT metadata XMLDemonstrates how to create the JPK VAT metadata XML (InitUpload) that will be signed using XADES.
// 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> loXml = createobject("CkXml") loXml.Tag = "InitUpload" loXml.AddAttribute("xmlns","http://e-dokumenty.mf.gov.pl") loXml.UpdateChildContent("DocumentType","JPK") loXml.UpdateChildContent("Version","01.02.01.20160617") loXml.UpdateAttrAt("EncryptionKey",.T.,"algorithm","RSA") loXml.UpdateAttrAt("EncryptionKey",.T.,"encoding","Base64") loXml.UpdateAttrAt("EncryptionKey",.T.,"mode","ECB") loXml.UpdateAttrAt("EncryptionKey",.T.,"padding","PKCS#1") loXml.UpdateChildContent("EncryptionKey","TO BE DETERMINED") loXml.UpdateAttrAt("DocumentList|Document|FormCode",.T.,"schemaVersion","1-1") loXml.UpdateAttrAt("DocumentList|Document|FormCode",.T.,"systemCode","JPK_VAT (3)") loXml.UpdateChildContent("DocumentList|Document|FormCode","JPK_VAT") loXml.UpdateChildContent("DocumentList|Document|FileName","JPK_VAT_3_v1-1_20181201.xml") loXml.UpdateChildContent("DocumentList|Document|ContentLength","9999") loXml.UpdateAttrAt("DocumentList|Document|HashValue",.T.,"algorithm","SHA-256") loXml.UpdateAttrAt("DocumentList|Document|HashValue",.T.,"encoding","Base64") loXml.UpdateChildContent("DocumentList|Document|HashValue","TO BE DETERMINED") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList",.T.,"filesNumber","1") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Packaging|SplitZip",.T.,"mode","zip") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Packaging|SplitZip",.T.,"type","split") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES",.T.,"block","16") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES",.T.,"mode","CBC") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES",.T.,"padding","PKCS#7") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES",.T.,"size","256") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES|IV",.T.,"bytes","16") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|Encryption|AES|IV",.T.,"encoding","Base64") loXml.UpdateChildContent("DocumentList|Document|FileSignatureList|Encryption|AES|IV","TO BE DETERMINED") loXml.UpdateChildContent("DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber","1") loXml.UpdateChildContent("DocumentList|Document|FileSignatureList|FileSignature|FileName","JPK_VAT_3_v1-1_20181201-000.xml.zip.aes") loXml.UpdateChildContent("DocumentList|Document|FileSignatureList|FileSignature|ContentLength","9999") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|FileSignature|HashValue",.T.,"algorithm","MD5") loXml.UpdateAttrAt("DocumentList|Document|FileSignatureList|FileSignature|HashValue",.T.,"encoding","Base64") loXml.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 loBdXml = createobject("CkBinData") llSuccess = loBdXml.LoadFile("qa_data/xml_dsig/jpk_vat/JPK_VAT_3_v1-1_20181201-000.xml") if (llSuccess <> .T.) then ? "Failed to load XML file." release loXml release loBdXml return endif loXml.UpdateChildContentInt("DocumentList|Document|ContentLength",loBdXml.NumBytes) loCrypt = createobject("CkCrypt2") loCrypt.HashAlgorithm = "sha256" loCrypt.EncodingMode = "base64" loXml.UpdateChildContent("DocumentList|Document|HashValue",loCrypt.HashBdENC(loBdXml)) // ------------------------------------------------------------ // Step 2: Create a Zip archive containing the XML. loZip = createobject("CkZip") // The filename we pass here doesn't matter because we won't actually be creating a .zip file. loZip.NewZip("anything.zip") e = loZip.AppendBd("JPK_VAT_3_v1-1_20181201-000.xml",loBdXml) release e // Write the .zip file to a BinData object. loBdZip = createobject("CkBinData") loZip.WriteBd(loBdZip) // ------------------------------------------------------------ // Step 3: Generate a random 256-bit AES key (32-bytes) loPrng = createobject("CkPrng") loBdAesKey = createobject("CkBinData") loPrng.GenRandomBd(32,loBdAesKey) lcIvBytes = loPrng.GenRandom(16,"base64") // Store the IV (base64 string) in the XML. loXml.UpdateChildContent("DocumentList|Document|FileSignatureList|Encryption|AES|IV",lcIvBytes) // ------------------------------------------------------------ // Step 4: AES encrypt our zip archive (the contents of bdZip) loCrypt.CipherMode = "cbc" loCrypt.KeyLength = 256 loCrypt.CryptAlgorithm = "aes" loCrypt.PaddingScheme = 0 loCrypt.SetEncodedIV(lcIvBytes,"base64") loCrypt.SetEncodedKey(loBdAesKey.GetEncoded("base64"),"base64") // AES by definition has a block size of 16. loCrypt.EncryptBd(loBdZip) // 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 llSuccess = loBdZip.WriteFile("qa_output/JPK_VAT_3_v1-1_20181201-000.xml.zip.aes") loXml.UpdateChildContentInt("DocumentList|Document|FileSignatureList|FileSignature|ContentLength",loBdZip.NumBytes) // ------------------------------------------------------------ // Step 4: RSA Encrypt the AES key using the public key certificate provided by the Ministry of Finance loCert = createobject("CkCert") llSuccess = loCert.LoadFromFile("qa_data/pem/mf_public_rsa.pem") if (llSuccess <> .T.) then ? loCert.LastErrorText release loXml release loBdXml release loCrypt release loZip release loBdZip release loPrng release loBdAesKey release loCert return endif loPubKey = loCert.ExportPublicKey() loRsa = createobject("CkRsa") loRsa.ImportPublicKeyObj(loPubKey) release loPubKey loRsa.EncodingMode = "base64" loRsa.LittleEndian = .F. // in-place RSA encrypt the contents of bdAesKey. loRsa.EncryptBd(loBdAesKey,.F.) loXml.UpdateChildContent("EncryptionKey",loBdAesKey.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..) loCrypt.HashAlgorithm = "md5" loXml.UpdateChildContent("DocumentList|Document|FileSignatureList|FileSignature|HashValue",loCrypt.HashBdENC(loBdZip)) // 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). lcFinalXml = loXml.GetXml() ? lcFinalXml loXml.SaveXml("qa_output/jpk_vat.xml") ? "Finished." release loXml release loBdXml release loCrypt release loZip release loBdZip release loPrng release loBdAesKey release loCert release loRsa |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.