Signing a VbaProject only works when the DigitalSignature is created from certificate bytes and password.
When a DigitalSignature is created from an existing X509Certificate2 then Sign throws a NotImplementedException.
I want to resign a VbaProject with a certificate from the windows certificate store (last code example).
This does work:
var digitalSignature = new DigitalSignature(File.ReadAllBytes("signing.p12"), "1234", "Signing Digital Signature using Aspose.Cells", DateTime.UtcNow);
workbook.VbaProject.Sign(digitalSignature);
This does not work:
var cert = new X509Certificate2(File.ReadAllBytes("signing.p12"), "1234");
var digitalSignature = new DigitalSignature(cert, "Signing Digital Signature using Aspose.Cells", DateTime.UtcNow);
workbook.VbaProject.Sign(digitalSignature);
This does not work:
using var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindByThumbprint, "1234", false);
var cert = certs[0];
var digitalSignature = new DigitalSignature(cert, "Signing Digital Signature using Aspose.Cells", DateTime.UtcNow);
workbook.VbaProject.Sign(digitalSignature);
Which version of Aspose.Cells for .NET you are using? Please try our latest version/fix. I tested your scenario/case using our latest version/fix: Aspose.Cells for .NET v22.6.1 (Download | NuGet), it works fine and as expected. I used the following sample code with a certificate file.
e.g. Sample code:
Workbook workbook = new Workbook();
var certificate = File.ReadAllBytes("e:\\test2\\digitalsig\\cert1.pfx");
var password = "1q2w3e4r5t";
var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(certificate, password);
var digitalSignature = new Aspose.Cells.DigitalSignatures.DigitalSignature(cert, "Signing Digital Signature using Aspose.Cells", DateTime.UtcNow);
workbook.VbaProject.Sign(digitalSignature);
Could you please share a standalone sample (.NET6) project (source code without compilation errors) with certificate or other resource files, zip the project to reproduce the issue on our end. We will check it soon.
Thanks for the sample project and certificate file.
I have logged a ticket with an id " CELLSNETCORE-397" for your issue. We will look into it soon. By the way, when I use your sample code with your certificate file in a common .NET (e.g., 4.0) project, it works fine.
Once we have an update on it, we will let you know.
Using an existing X509Certificate2 instance to sign VBA project is not supported in .NetStandard/.NetCore. It maybe refer to a package System.Security.Cryptography.Pkcs.
The DigitalSignature created from certificate bytes and password is suggested.
Please try to export the cert to bytes first:
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindByThumbprint, "1234", false);
var cert = certs[0];
//export cert to bytes first, then create DigitalSignature from bytes and password
var digitalSignature = new DigitalSignature(cert.Export(X509ContentType.Pkcs12, "1234"), "1234", "Signing Digital Signature using Aspose.Cells", DateTime.UtcNow);
workbook.VbaProject.Sign(digitalSignature);
The only limitation is that the cert must be marked as Exportable when the cert is imported, otherwise the exception Key not valid for use in specified state. will be thrown.
Please try it on your side, and let us know your feedback.
We can see the issue, and a separate ticket CELLSNETCORE-398 is logged. If we have some progress, we will notify you.
We are pleased to inform you that the issue is resolved now. The fix will be included in Aspose.Cells v22.7, which will be released in the next few days. Please note, in Aspose.Cells v22.7, you would need to add the following nuget packages to your project reference:
“System.Text.Encoding.CodePages” version=“4.7.0”
“System.Drawing.Common” version=“4.7.0”
“System.Security.Cryptography.Pkcs” version=“6.0.1”
You will also be notified once the new version is released.
Please add a newline \n at the end of module codes in your code, the vba signature will be OK/validated.
So, change the code line from workbook.VbaProject.Modules[i].Codes = "Sub test()\nEnd Sub";
to workbook.VbaProject.Modules[i].Codes = "Sub test()\nEnd Sub\n";