Attribuer et valider des signatures numériques
Une signature numérique garantit qu’un fichier de classeur est valide et que personne ne l’a modifié. Vous pouvez créer une signature numérique personnelle en utilisant leAUTOCERT outil livré avec la suite Office Microsoft ou tout autre outil. Vous pouvez même acheter une signature numérique. Après avoir créé ou acquis une signature numérique, vous devez la joindre à votre classeur. Joindre une signature numérique revient à sceller une enveloppe. Si une enveloppe arrive scellée, vous avez un certain niveau d’assurance que personne n’a altéré son contenu.
Aspose.Cells for Java API fournissent lecom.aspose.cells.DigitalSignatureCollection & com.aspose.cells.DigitalSignature classes pour signer les feuilles de calcul et les valider.
Signature des feuilles de calcul
Le processus de signature nécessite un certificat, comme indiqué ci-dessus. En plus du certificat, il faut également avoir son mot de passe pour signer avec succès les feuilles de calcul en utilisant le Aspose.Cells API.
L’extrait de code suivant illustre l’utilisation de Aspose.Cells for Java API pour signer une feuille de calcul.
// 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"); |
Validation des feuilles de calcul
L’extrait de code suivant illustre l’utilisation de Aspose.Cells for Java API pour valider la feuille de calcul.
// 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()); | |
} |