使用证书为VBA代码项目进行数字签名

使用C#为VBA代码项目使用证书进行数字签名

下面的示例代码演示了如何使用 Workbook.VbaProject.Sign() 方法。这是示例代码的输入和输出文件。您可以使用任何Excel文件和任何证书来测试此代码。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String password = "1234";
String pfxPath = sourceDir + "sampleDigitallySignVbaProjectWithCertificate.pfx";;
String comment = "Signing Digital Signature using Aspose.Cells";
// Load the certificate into an instance of InputStream
InputStream inStream = new FileInputStream(pfxPath);
// Create an instance of KeyStore with PKCS12 cryptography
KeyStore inputKeyStore = KeyStore.getInstance("PKCS12");
// Use the KeyStore.load method to load the certificate stream and its password
inputKeyStore.load(inStream, password.toCharArray());
inStream.close();
// Create an instance of DigitalSignature and pass the instance of KeyStore, password, comments and time
DigitalSignature signature = new DigitalSignature(inputKeyStore, password, comment,
DateTime.getNow());
Workbook wb = new Workbook(sourceDir + "sampleDigitallySignVbaProjectWithCertificate.xlsm");
wb.getVbaProject().sign(signature);
wb.save(outputDir + "outputDigitallySignVbaProjectWithCertificate.xlsm");