Represents text that should be kept confidential, such as by deleting it from computer memory when no longer needed. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
Initializes a new instance of theclass.
Appends a character to the end of the current secure string.
Deletes the value of the current secure string.
Creates a copy of the current secure string.
Releases all resources used by the currentobject.
Determines whether the specified object is equal to the current object.(Inherited from Object.)
Serves as the default hash function. (Inherited from Object.)
Inserts a character in this secure string at the specified index position.
Indicates whether this secure string is marked read-only.
Makes the text value of this secure string read-only.
Removes the character at the specified index position from this secure string.
Replaces the existing character at the specified index position with another character.
Returns a string that represents the current object.(Inherited from Object.)
How secure is SecureString? section.) The value of an instance of is automatically protected using a mechanism supported by the underlying platform when the instance is initialized or when the value is modified. Your application can render the instance immutable and prevent further modification by invoking the MakeReadOnly method.is a string type that provides a measure of security. It tries to avoid storing potentially sensitive strings in process memory as plain text. (For limitations, however, see the
The maximum length of ainstance is 65,536 characters.
This type implements the IDisposable interface. When you have finished using an instance of the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.
The ComVisibleAttribute.class and its members are not visible to COM. For more information, see
In this section:
An instance of the System.String class is both immutable and, when no longer needed, cannot be programmatically scheduled for garbage collection; that is, the instance is read-only after it is created, and it is not possible to predict when the instance will be deleted from computer memory. Because System.String instances are immutable, operations that appear to modify an existing instance actually create a copy of it to manipulate. Consequently, if a String object contains sensitive information such as a password, credit card number, or personal data, there is a risk the information could be revealed after it is used because your application cannot delete the data from computer memory.
A String object in that it has a text value. However, the value of a object is pinned in memory, may use a protection mechanism, such as encryption, provided by the underlying operating system, can be modified until your application marks it as read-only, and can be deleted from computer memory either by your application calling the Dispose method or by the .NET Framework garbage collector.object is similar to a
For a discussion of the limitations of the How secure is SecureString? section.class, see the
Theclass includes members that allow you to do the following:
- Instantiate a object
You instantiate aobject by calling its parameterless constructor.
- Add characters to a object
- Remove characters from a object
You can replace an individual character by calling the SetAt method, remove an individual character by calling the RemoveAt method, or remove all characters from the instance by calling the Clear method.
- Make the object read-only
Once you have defined the string that the MakeReadOnly method to make the string read-only.object represents, you call its
- Get information about the object
- Release the memory allocated to the instance
The .NET Framework Class Library commonly usesinstances in the following ways:
To pass a string to unmanaged code. For more information, see the SecureString and interop section.
Because the operating system does not directly support Marshal class has five methods that do this:, you must convert the value of the object to the required string type before passing the string to a native method. The
Marshal.SecureStringToBSTR, which converts the string value to a binary string (BSTR) recognized by COM.
Each of these methods creates a clear-text string in unmanaged memory. It is the responsibility of the developer to zero out and free that memory as soon as it is no longer needed. Each of the string conversion and memory allocation methods has a corresponding method to zero out and free the allocated memory:
Allocation and conversion method
Zero and free method
When created properly, a String. When creating a string from a character-at-a-time source, String creates multiple intermediate in memory, whereas creates just a single instance. Garbage collection of String objects is non-deterministic. In addition, because its memory is not pinned, the garbage collector will make additional copies of String values when moving and compacting memory. In contrast, the memory allocated to a object is pinned, and that memory can be freed by calling the Dispose method.instance provides more data protection than a
Although data stored in a String instance, there are significant limitations on how secure a instance is. These include:instance is more secure than data stored in a
On the Windows operating system, the contents of ainstance's internal character array is encrypted. However, whether because of missing APIs or key management issues, encryption is not available on all platforms. Because of this, is available on Desktop (Windows only), and not on .NET Core.
Even if theimplementation is able to take advantage of encryption, the plain text assigned to the instance may be exposed at various times:
Because Windows doesn't offer a secure string implementation at the operating system level, the .NET Framework still has to convert the secure string value to its plain text representation in order to use it.
If the secure string is used in an interop call, it must be converted to an ANSI string, a Unicode string, or a binary string (BSTR). For more information, see the SecureString and interop section.
The time interval for which the String class.instance's value is exposed is merely shortened in comparison to the
- Storage versus usage
More generally, theclass defines a storage mechanism for string values that should be protected or kept confidential. However, outside of the .NET Framework itself, no usage mechanism supports . This means that the secure string must be converted to a usable form (typically a clear text form) that can be recognized by its target, and that decryption and conversion must occur in user space.
Overall, String because it limits the exposure of sensitive string data. However, those strings may still be exposed to any process or operation that has access to raw memory, such as a malicious process running on the host computer, a process dump, or a user-viewable swap file. Instead of using to protect passwords, the recommended alternative is to use an opaque handle to credentials that are stored outside of the process.is more secure than
The following example demonstrates how to use ato secure a user’s password for use as a credential to start a new process.
Imports System.ComponentModel Imports System.Diagnostics Imports System.Security Public Class Example Public Shared Sub Main() ' Instantiate the secure string. Dim securePwd As New SecureString() Dim key As ConsoleKeyInfo Console.Write("Enter password: ") Do key = Console.ReadKey(True) ' Ignore any key out of range If CInt(key.Key) >= 65 And CInt(key.Key <= 90) Then ' Append the character to the password. securePwd.AppendChar(key.KeyChar) Console.Write("*") End If ' Exit if Enter key is pressed. Loop While key.Key <> ConsoleKey.Enter Console.WriteLine() Try Process.Start("Notepad.exe", "MyUser", securePwd, "MYDOMAIN") Catch e As Win32Exception Console.WriteLine(e.Message) Finally securePwd.Dispose() End Try End Sub End Class
Available since 2.0
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.