Assegna e convalida le firme digitali
Una firma digitale garantisce che un file della cartella di lavoro sia valido e che nessuno lo abbia modificato. È possibile creare una firma digitale personale utilizzando il fileAUTOCERT strumento fornito con la suite Office Microsoft o qualsiasi altro strumento. Puoi persino acquistare una firma digitale. Dopo aver creato o acquisito una firma digitale, è necessario allegarla alla cartella di lavoro. Allegare una firma digitale è simile a sigillare una busta. Se una busta arriva sigillata, hai un certo livello di sicurezza che nessuno ha manomesso il suo contenuto.
Aspose.Cells for Java API fornire ilcom.aspose.cells.DigitalSignatureCollection & com.aspose.cells.DigitalSignature classi per firmare i fogli di calcolo e convalidarli.
Firmare i fogli di calcolo
Il processo di firma richiede un certificato come discusso sopra. Insieme al certificato, si dovrebbe avere anche la propria password per firmare correttamente i fogli di calcolo utilizzando lo Aspose.Cells API.
Il seguente frammento di codice illustra l’utilizzo di Aspose.Cells for Java API per firmare un foglio di calcolo.
// 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"); |
Convalida dei fogli di calcolo
Il seguente frammento di codice illustra l’utilizzo di Aspose.Cells for Java API per convalidare il foglio di calcolo.
// 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()); | |
} |