WorkbookBase.SetPasswordEncryptionOptions 方法
设置使用密码加密工作簿的选项。
命名空间: Microsoft.Office.Tools.Excel
程序集: Microsoft.Office.Tools.Excel.v4.0.Utilities(在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)
语法
声明
Public Sub SetPasswordEncryptionOptions ( _
passwordEncryptionProvider As Object, _
passwordEncryptionAlgorithm As Object, _
passwordEncryptionKeyLength As Object, _
passwordEncryptionFileProperties As Object _
)
public void SetPasswordEncryptionOptions(
Object passwordEncryptionProvider,
Object passwordEncryptionAlgorithm,
Object passwordEncryptionKeyLength,
Object passwordEncryptionFileProperties
)
参数
- passwordEncryptionProvider
类型:System.Object
加密提供程序的字符串,区分大小写。
- passwordEncryptionAlgorithm
类型:System.Object
算法简称(如“RC4”)的字符串,区分大小写。
- passwordEncryptionKeyLength
类型:System.Object
加密密钥长度,为 8 的倍数(40 或更大)。
- passwordEncryptionFileProperties
类型:System.Object
如果为 true(默认值),则加密文件属性。
备注
PasswordEncryptionProvider、PasswordEncryptionAlgorithm 和 PasswordEncryptionKeyLength 参数不是相互独立的。 选择的加密提供程序限制了可以选择的算法集和密钥长度。
对于 PasswordEncryptionKeyLength 参数,密钥长度的范围不存在固有限制。 该范围由加密服务提供程序确定,该提供程序还确定加密算法。
可选参数
有关可选参数的信息,请参见Office 解决方案中的可选参数。
示例
下面的代码示例检查 HasPassword 属性的值,以确定工作簿是否受密码保护。 如果工作簿不受密码保护,则此示例将 Password 属性设置为从用户输入获得密码,然后调用 SetPasswordEncryptionOptions 方法设置加密算法、密钥长度和加密提供程序的名称,并启用文件属性加密。 然后,此示例在工作表 Sheet1 中显示 PasswordEncryptionProvider、PasswordEncryptionAlgorithm、PasswordEncryptionKeyLength 和 PasswordEncryptionFileProperties 属性的值。 此示例假定方法 GetPasswordFromUserInput 是在别处定义的。
此示例针对的是文档级自定义项。
Private Sub WorkbookPasswordOptions()
If Not Me.HasPassword Then
Me.Password = GetPasswordFromUserInput()
Me.SetPasswordEncryptionOptions( _
"Microsoft RSA SChannel Cryptographic Provider", _
"RC4", 128, True)
End If
' Display the password properties in Sheet1.
Globals.Sheet1.Range("A1").Value2 = _
"Password Encryption Provider:"
Globals.Sheet1.Range("A2").Value2 = _
"Password Encryption Algorithm:"
Globals.Sheet1.Range("A3").Value2 = _
"Password Encryption Key Length:"
Globals.Sheet1.Range("A4").Value2 = _
"Password Encryption File Properties:"
Globals.Sheet1.Range("A1", "A4").Columns.AutoFit()
Globals.Sheet1.Range("B1").Value2 = _
Me.PasswordEncryptionProvider
Globals.Sheet1.Range("B2").Value2 = _
Me.PasswordEncryptionAlgorithm
Globals.Sheet1.Range("B3").Value2 = _
Me.PasswordEncryptionKeyLength
Globals.Sheet1.Range("B4").Value2 = _
Me.PasswordEncryptionFileProperties
Globals.Sheet1.Range("B1", "B4").Columns.AutoFit()
End Sub
private void WorkbookPasswordOptions()
{
if (!this.HasPassword)
{
this.Password = GetPasswordFromUserInput();
this.SetPasswordEncryptionOptions(
"Microsoft RSA SChannel Cryptographic Provider",
"RC4", 128, true);
}
// Display the password properties in Sheet1.
Globals.Sheet1.Range["A1", missing].Value2 =
"Password Encryption Provider:";
Globals.Sheet1.Range["A2", missing].Value2 =
"Password Encryption Algorithm:";
Globals.Sheet1.Range["A3", missing].Value2 =
"Password Encryption Key Length:";
Globals.Sheet1.Range["A4", missing].Value2 =
"Password Encryption File Properties:";
Globals.Sheet1.Range["A1", "A4"].Columns.AutoFit();
Globals.Sheet1.Range["B1", missing].Value2 =
this.PasswordEncryptionProvider;
Globals.Sheet1.Range["B2", missing].Value2 =
this.PasswordEncryptionAlgorithm;
Globals.Sheet1.Range["B3", missing].Value2 =
this.PasswordEncryptionKeyLength;
Globals.Sheet1.Range["B4", missing].Value2 =
this.PasswordEncryptionFileProperties;
Globals.Sheet1.Range["B1", "B4"].Columns.AutoFit();
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。