CryptoSys Home > FirmaSAT > Working entirely in memory

Working entirely in memory


Version 9.2 of FirmaSAT has improvements that make it easier to work entirely in memory.

The improvements provide work-arounds to solve problems storing UTF-8-encoded data in .NET and VBA UTF-16 strings (called "Unicode" strings in older Microsoft documentation).

Read in file from disk to a string | Read in key and certificate data to a string | Signing XML data entirely in memory | Example code | What's the issue here? | Contact us

Read in file from disk to a string

.NET: C# and VB.NET

The following code now works properly, allowing you to read in an XML file directly into a string and then pass that string as a parameter to other FirmaSAT functions.

string xmlStr = File.ReadAllText(xmlFile, Encoding.UTF8);
n = Sat.Validate(xmlStr);

Improvement in v9.2: In earlier versions, if xmlFile contains characters like "ó" in Nombre="Esta es una demostración", the string read in by File.ReadAllText would cause an "encoding error" when passed to a FirmaSAT function. This is now fixed in version 9.2.

VBA/VB6

In VBA/VB6, use the satAsciify function to avoid UTF-8 encoding issues. (satAsciify is a wrapper around the function SAT_Asciify.)

The resulting VBA Unicode string can be passed directly to any FirmaSAT functions.
Dim fname As String
Dim xmlstring As String
Dim n As Long
fname = "cfdv33a-nomina12B.xml"
xmlstring = satAsciify(fname)
n = SAT_ValidateXml(xmlstring, 0)
Debug.Assert (0 = n)

Improvement in v9.2: In earlier versions, the SAT_Asciify function would work for almost all input files, converting all non-ASCII characters to XML numeric character references. This would not work for cases like Año="2016" and Antigüedad="P3Y2M23D" where the attribute name contained a non-ASCII character like "ñ" or "ü". This is now fixed in version 9.2.

Read in key and certificate data to a string

Here is how to read in private key and X.509 certificate files to strings that can be used directly in FirmaSAT functions.

.NET: C# and VB.NET

string keyStr = Sat.GetKeyAsString("emisor.key", password, KeyOption.EncryptedPEM);
string cerStr = Sat.GetCertAsString("emisor.cer");

VBA/VB6

Dim keyfiledata As String
Dim certfiledata As String
keyfiledata = satGetKeyAsPEMString("emisor.key", password)
certfiledata = satGetCertAsString("emisor.cer")

Alternatively, you can hard-code the data.

string keyStr =
	"-----BEGIN ENCRYPTED PRIVATE KEY-----" +
	"MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQI5qDMtGWYa2wCAggA" +
	"MBQGCCqGSIb3DQMHBAhFAqj+c0f8JASCBMhNUpNUp57vMu8L3LHBKRBTFl0VE3oq" +
	...[cut]...
	"EsYulVdcXWzCF58HFQjUoDon0e/QMukS0eNgq9ipmoKAWKyy7+TQw7Xx3MmqkGlL" +
	"HGM=" +
	"-----END ENCRYPTED PRIVATE KEY-----";
string cerStr =
	"-----BEGIN CERTIFICATE-----" +
	"MIIF+TCCA+GgAwIBAgIUMzAwMDEwMDAwMDAzMDAwMjM3MDgwDQYJKoZIhvcNAQELBQAwggFmMSAwHgY" +
	"DVQQDDBdBLkMuIDIgZGUgcHJ1ZWJhcyg0MDk2KTEvMC0GA1UECgwmU2VydmljaW8gZGUgQWRtaW5pc3" +
	...[cut]...
	"DsUb0qP42QCGBiTUseGugAzqOP6EYpVPC73gFourmdBQgfayaEvi3xjNanFkPlW1XEYNrYJB4yNjphF" +
	"rvWwTY86vL2o8gZN0Utmc5fnoBTfM9r2zVKmEi6FUeJ1iaDaVNv47te9iS1ai4V4vBY8r" +
	"-----END CERTIFICATE-----";

Note that the private key data stored in the string beginning "BEGIN ENCRYPTED PRIVATE KEY" is still safely encrypted.

Signing XML data entirely in memory

With all the necessary data now stored in memory, we can sign the XML document. Use the .NET method Sat.SignXmlToBytes or the VBA/VB6 function satSignXmlToBytes (the preferred wrapper for SAT_SignXmlToString).

.NET: C# and VB.NET

byte[] xmlArr = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(xmlStr);
// Sign XML document with all parameters passed in memory
byte[] xmlArrSigned = Sat.SignXmlToBytes(xmlArr, keyStr, password, cerStr, 0);

VBA/VB6

Dim xmlbytes() As Byte
xmlbytes = satSignXmlToBytes(xmlstring, keyfiledata, password, certfiledata, 0)

Example code

You can see the above examples used in the .NET test code TestFirmaSat.cs and TestFirmaSat.vb, and the VBA/VB6 test code TestFirmaSat.bas. The test code source files are provided in the distribution.

What's the issue here?

Contact us

To contact us or comment on this page, please send us a message.

[Go to top]

This page last updated 15 August 2025