彩票走势图

使用Aspose.Cells为电子表格添加数字签名

原创|其它|编辑:郝浩|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)
  &nbsp;wb.Save("e:\test\digital signatures\newfile.xlsx")

  End Sub
 End Class
End Namespace

在运行上述的代码后,我们为电子表格添加的数字签名效果图如下:

用Aspose.Cells为电子表格文件添加数字签名

我们可以用MS Excel 查看数字标签的具体信息:

用Aspose.Cells为电子表格文件添加数字签名


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP