Digitale Signaturen zuweisen und validieren
Eine digitale Signatur gewährleistet, dass eine Arbeitsbuchdatei gültig ist und niemand sie verändert hat. Sie können eine persönliche digitale Signatur mithilfe des mit der Microsoft Office-Suite gelieferten SELFCERT-Tools oder eines anderen Tools erstellen. Sie können auch eine digitale Signatur erwerben. Nachdem Sie eine digitale Signatur erstellt oder erworben haben, müssen Sie sie an Ihr Arbeitsbuch anhängen. Das Anfügen einer digitalen Signatur ähnelt dem Versiegeln eines Umschlags. Wenn ein versiegelter Umschlag ankommt, haben Sie eine gewisse Sicherheitsgewährleistung, dass niemand den Inhalt manipuliert hat.
Aspose.Cells for Java API bietet die Klassen com.aspose.cells.DigitalSignatureCollection und com.aspose.cells.DigitalSignature zum Signieren von Arbeitsmappen sowie zur Validierung von ihnen.
Signieren der Arbeitsmappen
Der Signiervorgang erfordert wie oben diskutiert ein Zertifikat. Neben dem Zertifikat sollte man auch sein Passwort haben, um die Arbeitsmappen mit Hilfe der Aspose.Cells-API erfolgreich zu signieren.
Das folgende Code-Snippet zeigt die Verwendung der Aspose.Cells for Java-API zum Signieren einer Arbeitsmappe.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SigningSpreadsheets.class); | |
// Create an instance of DigitalSignatureCollection | |
DigitalSignatureCollection signatures = new DigitalSignatureCollection(); | |
// Load the certificate into an instance of InputStream | |
InputStream inStream = new FileInputStream("d:/temp.pfx"); | |
// 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, KEYSTORE_PASSWORD.toCharArray()); | |
// Create an instance of DigitalSignature and pass the instance of KeyStore, password, comments and time | |
DigitalSignature signature = new DigitalSignature(inputKeyStore, KEYSTORE_PASSWORD, "test for sign", | |
DateTime.getNow()); | |
// Add the instance of DigitalSignature into the collection | |
signatures.add(signature); | |
// Load an existing spreadsheet using the Workbook class | |
Workbook workbook = new Workbook(dataDir + "unsigned.xlsx"); | |
// Set the signature | |
workbook.setDigitalSignature(signatures); | |
// Save the signed spreadsheet | |
workbook.save(dataDir + "signed.xlsx"); |
Validieren der Arbeitsmappen
Das folgende Code-Snippet zeigt die Verwendung der Aspose.Cells for Java-API zur Validierung der Arbeitsmappe.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ValidatingSpreadsheets.class); | |
// Load an existing spreadsheet in an instance of Workbook | |
Workbook workbook = new Workbook(dataDir + "signed.xlsx"); | |
// Retrieve the collection of digital signatures from the Workbook | |
DigitalSignatureCollection signatures = workbook.getDigitalSignature(); | |
// Loop over the collection of digital signatures | |
for (DigitalSignature signature : (Iterable<DigitalSignature>) signatures) { | |
// Check the signature status using the isValid property | |
System.out.println(signature.isValid()); | |
} |