Word控件Spire.Doc 【加密解密】教程(一):在 C#、VB.NET 中使用自定义密码加密、解密、保护 Word

Word 加密是保护 Word 文档的一种方法,它要求用户为文档提供密码。没有密码,加密文件无法打开。本指南中的解决方案演示了如何通过 Spire.Doc for .NET 在 C# 和 VB.NET 中使用自定义密码加密 Word 文档。

Spire.Doc for.NET 最新下载

在 C#、VB.NET 中使用自定义密码加密 Word

Spire.Doc for .NET专门为 .NET 执行 Word 处理任务,提供了 Document.Encrypt 方法,使用户能够加密 Word。传递给此方法的重载是字符串密码。首先,加载需要保护的Word文档。其次,调用Document.Encrypt方法使用密码进行加密。第三,保存加密文件并启动查看。调试后会弹出一个对话框,要求输入密码。输入密码打开文件,文件信息会显示如下,告诉用户它是加密的。

下载并安装 Spire.Doc for .NET并使用以下代码加密 Word。

[C#]

using Spire.Doc;

namespace Encryption
{
class Program
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");

//Encrypt
document.Encrypt("eiceblue");

//Save and Launch
document.SaveToFile("Encryption.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Encryption.docx");
}
}
}

[VB.NET]

Imports Spire.Doc

Namespace Encryption
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")

'Encrypt
document.Encrypt("eiceblue")

'Save and Launch
document.SaveToFile("Encryption.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("Encryption.docx")
End Sub
End Class
End Namespace

在 C#、VB.NET 中解密 Word 文档

Word Decryption 是对加密的 Word 文档进行解码的过程。它需要密码或密钥。如果读者想要打开和阅读一个受保护的Word,他们需要首先解密这个Word文档。本指南演示了一个简单方便的解决方案,用于通过 Spire.Doc for .NET 在 C# 和 VB.NET 中解密 Word。

Spire.Doc for .NET,专为程序员在没有Word自动化的情况下操作Word而开发,为用户提供了Document类的方法Document.LoadFromFile(String fileName, FileFormat fileFormat, String password)来打开加密的Word文档。它还提供了另一种方法Document.RemoveEncryption()在没有任何保护的情况下解密 Word。通过这两种方法,用户可以使用 Spire.Doc for .NET 轻松解密 Word。下载并安装 Spire.Doc for .NET。然后按照代码解密。

[C#]

using Spire.Doc;

namespace DecryptWord
{
class Decryption
{
static void Main(string[] args)
{
//Load Encrypted Word
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\Student Transcript.docx", FileFormat.Docx,"123456");

//Decrypt
document.RemoveEncryption();

//Save and Launch
document.SaveToFile("decryption.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("decryption.docx");
}
}
}

[VB.NET]

Imports Spire.Doc

Namespace DecryptWord
Friend Class Decryption
Shared Sub Main(ByVal args() As String)
'Load Encrypted Word
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\Student Transcript.docx", FileFormat.Docx, "123456")

'Decrypt
document.RemoveEncryption()

'Save and Launch
document.SaveToFile("decryption.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("decryption.docx")
End Sub
End Class
End Namespace

在 C# 中使用指定的保护类型保护 Word

字保护不同于字加密。它允许用户打开和查看 Word 文档,但有一些用户无法编辑或只能填写字段的权限。本指南中的解决方案演示了使用 Spire.Doc for .NET 在 C# 和 VB.NET 中使用指定保护类型保护 Word 的解决方案。以下屏幕截图显示了只读保护后的结果。

无保护:设置没有保护的文档。
AllowOnlyRevisions:允许向 Word 添加修订标记。
AllowOnlyComments:允许修改 Word 中的注释。
AllowOnlyFormFields:允许在 Word 的表单域中输入数据。
AllowOnlyReading:只允许阅读 Word。

下面的代码显示了如何使用 AllowOnlyReading 类型保护 Word。下载并安装 Spire.Doc for .NET并按照代码进行操作。

[C#]

using Spire.Doc;

namespace ProtectWord
{
class Program
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\.NET Framework.docx");
//Protect Word
document.Protect(ProtectionType.AllowOnlyReading, "123456");
//Save and Launch
document.SaveToFile("ProtectWord.docx");
System.Diagnostics.Process.Start("ProtectWord.docx");
}
}
}

[VB.NET]

Imports Spire.Doc

Namespace ProtectWord
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\.NET Framework.docx")
'Protect Word
document.Protect(ProtectionType.AllowOnlyReading, "123456")
'Save and Launch
document.SaveToFile("ProtectWord.docx")
System.Diagnostics.Process.Start("ProtectWord.docx")
End Sub
End Class
End Namespace

  欢迎下载|体验更多E-iceblue产品 技术交流Q群(767755948)  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值