CryptographicException Class
.NET Framework 4.5
The exception that is thrown when an error occurs during a cryptographic operation.
System.Object
System.Exception
System.SystemException
System.Security.Cryptography.CryptographicException
System.Security.Cryptography.CryptographicUnexpectedOperationException
System.Exception
System.SystemException
System.Security.Cryptography.CryptographicException
System.Security.Cryptography.CryptographicUnexpectedOperationException
Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)
The CryptographicException type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | CryptographicException() | Initializes a new instance of the CryptographicException class with default properties. |
![]() | CryptographicException(Int32) | Initializes a new instance of the CryptographicException class with the specified HRESULT error code. |
![]() | CryptographicException(String) | Initializes a new instance of the CryptographicException class with a specified error message. |
![]() | CryptographicException(SerializationInfo, StreamingContext) | Initializes a new instance of the CryptographicException class with serialized data. |
![]() | CryptographicException(String, Exception) | Initializes a new instance of the CryptographicException class with a specified error message and a reference to the inner exception that is the cause of this exception. |
![]() | CryptographicException(String, String) | Initializes a new instance of the CryptographicException class with a specified error message in the specified format. |
| Name | Description | |
|---|---|---|
![]() | Data | Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.) |
![]() | HelpLink | Gets or sets a link to the help file associated with this exception. (Inherited from Exception.) |
![]() | HResult | Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.) |
![]() | InnerException | Gets the Exception instance that caused the current exception. (Inherited from Exception.) |
![]() | Message | Gets a message that describes the current exception. (Inherited from Exception.) |
![]() | Source | Gets or sets the name of the application or the object that causes the error. (Inherited from Exception.) |
![]() | StackTrace | Gets a string representation of the immediate frames on the call stack. (Inherited from Exception.) |
![]() | TargetSite | Gets the method that throws the current exception. (Inherited from Exception.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetBaseException | When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetObjectData | When overridden in a derived class, sets the SerializationInfo with information about the exception. (Inherited from Exception.) |
![]() | GetType | Gets the runtime type of the current instance. (Inherited from Exception.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Creates and returns a string representation of the current exception. (Inherited from Exception.) |
| Name | Description | |
|---|---|---|
![]() | SerializeObjectState | Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception.) |
The following code example demonstrates how to use members of the CryptographicException class.
using System; using System.Text; using System.Security.Cryptography; using System.Runtime.Serialization; class CryptographicExceptionMembers { [STAThread] public static void Main(string[] args) { CryptographicExceptionMembers testRun = new CryptographicExceptionMembers(); testRun.TestConstructors(); testRun.ShowProperties(); Console.WriteLine("This sample ended successfully; " + " press Enter to exit."); Console.ReadLine(); } // Test each public implementation of the CryptographicException // constructors. private void TestConstructors() { EmptyConstructor(); IntConstructor(); StringConstructor(); StringExceptionConstructor(); StringStringConstructor(); } private void EmptyConstructor() { // Construct a CryptographicException with no parameters. CryptographicException cryptographicException = new CryptographicException(); Console.WriteLine("Created an empty CryptographicException."); } private void IntConstructor() { // Construct a CryptographicException using the error code for an // unexpected operation exception. int exceptionNumber = unchecked((int)0x80131431); CryptographicException cryptographicException = new CryptographicException(exceptionNumber); Console.WriteLine("Created a CryptographicException with the " + "following error code: " + exceptionNumber); } private void StringConstructor() { // Construct a CryptographicException using a custom error message. string errorMessage = ("Unexpected Operation exception."); CryptographicException cryptographicException = new CryptographicException(errorMessage); Console.WriteLine("Created a CryptographicException with the " + "following error message: " + errorMessage); } private void StringExceptionConstructor() { // Construct a CryptographicException using a custom error message // and an inner exception. string errorMessage = ("The current operation is not supported."); NullReferenceException nullException = new NullReferenceException(); CryptographicException cryptographicException = new CryptographicException(errorMessage, nullException); Console.WriteLine("Created a CryptographicException with the " + "following error message: " + errorMessage + " and the inner exception of " + nullException.ToString()); } private void StringStringConstructor() { // Create a CryptographicException using a time format and a the // current date. string dateFormat = "{0:t}"; string timeStamp = (DateTime.Now.ToString()); CryptographicException cryptographicException = new CryptographicException(dateFormat, timeStamp); Console.WriteLine("Created a CryptographicException with (" + dateFormat + ") as the format and (" + timeStamp + ") as the message."); } // Construct an invalid DSACryptoServiceProvider to throw a // CryptographicException for introspection. private void ShowProperties() { try { // Create a DSACryptoServiceProvider with invalid provider type // code to throw a CryptographicException exception. CspParameters cspParams = new CspParameters(44); DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams); } catch (CryptographicException ex) { // Retrieve the link to the help file for the exception. string helpLink = ex.HelpLink; // Retrieve the exception that caused the current // CryptographicException exception. System.Exception innerException = ex.InnerException; string innerExceptionMessage = ""; if (innerException != null) { innerExceptionMessage = innerException.ToString(); } // Retrieve the message that describes the exception. string message = ex.Message; // Retrieve the name of the application that caused the exception. string exceptionSource = ex.Source; // Retrieve the call stack at the time the exception occured. string stackTrace = ex.StackTrace; // Retrieve the method that threw the exception. System.Reflection.MethodBase targetSite = ex.TargetSite; string siteName = targetSite.Name; // Retrieve the entire exception as a single string. string entireException = ex.ToString(); // GetObjectData setSerializationInfo(ref ex); // Get the root exception that caused the current // CryptographicException exception. System.Exception baseException = ex.GetBaseException(); string baseExceptionMessage = ""; if (baseException != null) { baseExceptionMessage = baseException.Message; } Console.WriteLine("Caught an expected exception:"); Console.WriteLine(entireException); Console.WriteLine("\n"); Console.WriteLine("Properties of the exception are as follows:"); Console.WriteLine("Message: " + message); Console.WriteLine("Source: " + exceptionSource); Console.WriteLine("Stack trace: " + stackTrace); Console.WriteLine("Help link: " + helpLink); Console.WriteLine("Target site's name: " + siteName); Console.WriteLine("Base exception message: " + baseExceptionMessage); Console.WriteLine("Inner exception message: " + innerExceptionMessage); } } private void setSerializationInfo(ref CryptographicException ex) { // Insert information about the exception into a serialized object. FormatterConverter formatConverter = new FormatterConverter(); SerializationInfo serializationInfo = new SerializationInfo(ex.GetType(), formatConverter); StreamingContext streamingContext = new StreamingContext(StreamingContextStates.All); ex.GetObjectData(serializationInfo,streamingContext); } } // // This sample produces the following output: // // Created an empty CryptographicException. // Created a CryptographicException with the following error code: -2146233295 // Created a CryptographicException with the following error message: // Unexpected Operation exception. // Created a CryptographicException with the following error message: The // current operation is not supported. and the inner exception of // System.NullReferenceException: Object reference not set to an instance of // an object. // Created a CryptographicException with ({0:t}) as the format and (2/24/2004 // 2:13:15 PM) as the message. // Caught an expected exception: // System.Security.Cryptography.CryptographicException: CryptoAPI // cryptographic service provider (CSP) for this implementation could not be // acquired. // at System.Security.Cryptography.DSACryptoServiceProvider..ctor(Int32 // dwKeySize, CspParameters parameters) // at System.Security.Cryptography.DSACryptoServiceProvider..ctor( // CspParametersparameters) // at CryptographicExceptionMembers.ShowProperties() in c:\inetpub\ // vssolutions\test\testbuild\consoleapplication1\class1.cs:line 109 // // // Properties of the exception are as follows: // Message: CryptoAPI cryptographic service provider (CSP) for this // implementation could not be acquired. // Source: mscorlib // Stack trace: // at System.Security.Cryptography.DSACryptoServiceProvider..ctor( // Int32 dwKeySize, CspParameters parameters) // at System.Security.Cryptography.DSACryptoServiceProvider..ctor( // CspParameters parameters) // at CryptographicExceptionMembers.ShowProperties() in c:\inetpub\ // vssolutions\test\testbuild\consoleapplication1\class1.cs:line 109 // Help link: // Target site's name: .ctor // Base exception message: CryptoAPI cryptographic service provider (CSP) for // this implementation could not be acquired. // Inner exception message: // This sample ended successfully; press Enter to exit.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

