デジタル署名の割り当てと検証
デジタル署名により、ワークブックファイルが有効であることと、誰もがそれを改ざんしていないことが保証されます。Microsoft Officeスイートに含まれるSELFCERTツールやその他のツールを使用して、個人用のデジタル署名を作成できます。デジタル署名を取得または作成した後は、ワークブックに添付する必要があります。デジタル署名を添付することは封筒を封印することに似ています。封筒が封印された状態で届くと、その内容が誰も改ざんしていないという程度の保証が得られます。
Aspose.Cells for Java APIは、スプレッドシートに署名を追加し、検証するためのcom.aspose.cells.DigitalSignatureCollectionおよびcom.aspose.cells.DigitalSignatureクラスを提供します。
スプレッドシートに署名するには、前述の証明書が必要です。証明書とともに、Aspose.Cells APIを使用してスプレッドシートに署名するためには、パスワードも必要です。
指定されたパスワードが証明書のパスワードと一致しない場合、javax.crypto.BadPaddingException型の例外がスローされます。
次のコードスニペットは、Aspose.Cells for Java APIを使用してスプレッドシートに署名する方法を示しています。
// 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"); |
スプレッドシートの検証
次のコードスニペットは、Aspose.Cells for Java APIを使用してスプレッドシートを検証する方法を示しています。
// 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()); | |
} |