SecureString 构造函数

定义

初始化 SecureString 类的新实例。

重载

名称 说明
SecureString()

初始化 SecureString 类的新实例。

SecureString(Char*, Int32)

从对象的子数组SecureString初始化类的新实例Char

此构造函数不符合 CLS。 符合 CLS 的替代方法是 SecureString()

SecureString()

Source:
SecureString.cs
Source:
SecureString.cs
Source:
SecureString.cs
Source:
SecureString.cs
Source:
SecureString.cs

初始化 SecureString 类的新实例。

public:
 SecureString();
public SecureString();
Public Sub New ()

例外

保护或取消保护此实例的值时出错。

此平台上不支持此操作。

示例

以下示例使用默认的(或无参数)构造函数来实例化新 SecureString 对象。 然后,它会调用 AppendChar 该方法,以向其添加字符数组。

using System;
using System.Security;

public class Example
{
   public static void Main()
   {
      // Define the string value to assign to a new secure string.
      char[] chars = { 't', 'e', 's', 't' };
      // Instantiate the secure string.
      SecureString testString = new SecureString();
      // Assign the character array to the secure string.
      foreach (char ch in chars)
         testString.AppendChar(ch);      
      // Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", 
                        testString.Length);
      testString.Dispose();
   }
}
// The example displays the following output:
//      The length of the string is 4 characters.
Imports System.Security

Module Example
   Public Sub Main()
      ' Define the string value to assign to a new secure string.
      Dim chars() As Char = { "t"c, "e"c, "s"c, "t"c }
      ' Instantiate the secure string.
      Dim testString As SecureString = New SecureString()
      ' Assign the character array to the secure string.
      For Each ch As char In chars
         testString.AppendChar(ch)
      Next         
      ' Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", _ 
                        testString.Length)
      testString.Dispose()
   End Sub
End Module
' The example displays the following output:
'      The length of the string is 4 characters.

以下示例基于对象的值创建 SecureString 对象 String

using System;
using System.Security;

public class Example
{
   public static void Main()
   {
      // Define the string value to be assigned to the secure string.
      string initString = "TestString";
      // Instantiate the secure string.
      SecureString testString = new SecureString();
      // Use the AppendChar method to add each char value to the secure string.
      foreach (char ch in initString)
         testString.AppendChar(ch);
         
      // Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", 
                        testString.Length);
      testString.Dispose();
   }
}
// The example displays the following output:
//      The length of the string is 10 characters.
Imports System.Security

Module Example
   Public Sub Main()
      ' Define the string value to be assigned to the secure string.
      Dim initString As String = "TestString"
      ' Instantiate the secure string.
      Dim testString As SecureString = New SecureString()
      ' Use the AppendChar method to add each char value to the secure string.
      For Each ch As Char In initString
         testString.AppendChar(ch)
      Next   
      ' Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", _ 
                        testString.Length)
      testString.Dispose()
   End Sub
End Module
' The example displays the following output:
'      The length of the string is 10 characters.

适用于

SecureString(Char*, Int32)

Source:
SecureString.cs
Source:
SecureString.cs
Source:
SecureString.cs
Source:
SecureString.cs
Source:
SecureString.cs

重要

此 API 不符合 CLS。

从对象的子数组SecureString初始化类的新实例Char

此构造函数不符合 CLS。 符合 CLS 的替代方法是 SecureString()

public:
 SecureString(char* value, int length);
[System.CLSCompliant(false)]
public SecureString(char* value, int length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public SecureString(char* value, int length);
[<System.CLSCompliant(false)>]
new System.Security.SecureString : nativeptr<char> * int -> System.Security.SecureString
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new System.Security.SecureString : nativeptr<char> * int -> System.Security.SecureString

参数

value
Char*

指向 Char 对象的数组的指针。

length
Int32

要包含在新实例中的元素 value 数。

属性

例外

valuenull

length 小于零或大于 65,536。

保护或取消保护此安全字符串的值时出错。

此平台上不支持此操作。

示例

以下示例通过将构造函数传递给字符数组来实例化新 SecureString 对象。

using System;
using System.Security;

public class Example
{
   unsafe public static void Main()
   {
      SecureString testString;
      // Define the string value to assign to a new secure string.
      char[] chars = { 't', 'e', 's', 't' };

      // Instantiate a new secure string.
      fixed(char* pChars = chars)
      {
         testString = new SecureString(pChars, chars.Length);
      }
      // Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", 
                        testString.Length);
      testString.Dispose();
   }
}
// The example displays the following output:
//      The length of the string is 4 characters.

注解

此构造函数将新SecureString对象初始化为由value指定的字符length数;然后加密实例的值。

在 C# 中,此构造函数仅在不安全代码的上下文中定义。

适用于