原創|其它|編輯:郝浩|2012-09-14 16:32:39.000|閱讀 505 次
概述:數字簽名能有效地防止他人對文件的篡改,進而為文件的可信度提供保障。所以,很多人都想知道:如何用Aspose.Cells為電子表格文件添加數字簽名。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
數字簽名能有效地防止他人對文件的篡改,進而為文件的可信度提供保障。所以,很多人都想知道:如何用Aspose.Cells為電子表格文件添加數字簽名?
Aspose.Cells提供Aspose.Cells.DigitalSignatures命名空間來執行這個任務。我們只需使用the Digital Signature dialog box (數字簽名對話框)就能為文件添加數字簽名。數字簽名對話框中列出了有效的證書,我們可以使用數字簽名對話框來查看證書,并選擇要使用的證書。
下面,我們就給出具體的事例代碼供大家參考:
[C#]
using System;
using System.IO;
using Aspose.Cells;
using System.Collections;
using System.Text;
using System.Threading;
using Aspose.Cells.Rendering;
using System.Security.Cryptography;
using System.Drawing;
using System.Diagnostics;
using Aspose.Cells.DigitalSignatures;
using System.Security.Cryptography.X509Certificates;
namespace Aspose.Cells.test
{
/// <summary>
/// Summary description for MyTest.
/// </summary>
class MyTest
{
public MyTest()
{
}
[STAThread]
static void Main(string[] args)
{
MyTest test = new MyTest();
test.testSign();
test.testvalidateSign();
Console.ReadLine();
}
internal void testvalidateSign()
{
//instantiate and open the file
Workbook wb = new Workbook(@"e:\test\digital signatures\newfile.xlsx");
//wb.IsDigitallySigned is true when the workbook is signed already.
System.Console.WriteLine(wb.IsDigitallySigned); //True -OK
//get digitalSignature collection from workbook
DigitalSignatureCollection dsc = wb.GetDigitalSignature();
foreach (DigitalSignature ds in dsc)
{
System.Console.WriteLine(ds.Comments); //test for sign -OK
System.Console.WriteLine(ds.SignTime); //11/25/2010 1:22:01 PM -OK
System.Console.WriteLine(ds.IsValid); //True -OK
}
}
internal void testSign()
{
//dsc is signature collection contains one or more signature needed to sign
DigitalSignatureCollection dsc = new DigitalSignatureCollection();
//cert must contain private key, it can be contructed from cert file or windows certificate collection.
//123456 is password of cert
X509Certificate2 cert = new X509Certificate2("e:\\test\\digital signatures\\mykey2.pfx", "123456");
DigitalSignature ds = new DigitalSignature(cert, "test for sign", DateTime.Now);
dsc.Add(ds);
Workbook wb = new Workbook();
//wb.SetDigitalSignature signs all signatures in dsc
wb.SetDigitalSignature(dsc);
wb.Save(@"e:\test\digital signatures\newfile.xlsx");
}
}
}
[VB]
Imports System
Imports System.IO
Imports Aspose.Cells
Imports System.Collections
Imports System.Text
Imports System.Threading
Imports Aspose.Cells.Rendering
Imports System.Security.Cryptography
Imports System.Drawing
Imports System.Diagnostics
Imports Aspose.Cells.DigitalSignatures
Imports System.Security.Cryptography.X509Certificates
Namespace Aspose.Cells.test
''' <summary>
''' Summary description for MyTest.
''' </summary>
Friend Class MyTest
Public Sub New()
End Sub
<STAThread> _
Shared Sub Main(ByVal args() As String)
Dim test As New MyTest()
test.testSign()
test.testvalidateSign()
Console.ReadLine()
End Sub
Friend Sub testvalidateSign()
'instantiate and open the file
Dim wb As New Workbook("e:\test\digital signatures\newfile.xlsx")
'wb.IsDigitallySigned is true when the workbook is signed already.
System.Console.WriteLine(wb.IsDigitallySigned) 'True -OK
'get digitalSignature collection from workbook
Dim dsc As DigitalSignatureCollection = wb.GetDigitalSignature()
For Each ds As DigitalSignature In dsc
System.Console.WriteLine(ds.Comments) 'test for sign -OK
System.Console.WriteLine(ds.SignTime) '11/25/2010 1:22:01 PM -OK
System.Console.WriteLine(ds.IsValid) 'True -OK
Next ds
End Sub
Friend Sub testSign()
'dsc is signature collection contains one or more signature needed to sign
Dim dsc As New DigitalSignatureCollection()
'cert must contain private key, it can be contructed from cert file or windows certificate collection.
'123456 is password of cert
Dim cert As New X509Certificate2("e:\test\digital signatures\mykey2.pfx", "123456")
Dim ds As New DigitalSignature(cert, "test for sign", DateTime.Now)
dsc.Add(ds)
Dim wb As New Workbook()
'wb.SetDigitalSignature signs all signatures in dsc
wb.SetDigitalSignature(dsc)
wb.Save("e:\test\digital signatures\newfile.xlsx")
End Sub
End Class
End Namespace
在運行上述的代碼后,我們為電子表格添加的數字簽名效果圖如下:
我們可以用MS Excel 查看數字標簽的具體信息:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網