CryptographicException 类

定义

在加密操作期间发生错误时引发的异常。

C#
public class CryptographicException : SystemException
C#
public class CryptographicException : Exception
C#
[System.Serializable]
public class CryptographicException : SystemException
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class CryptographicException : SystemException
继承
CryptographicException
继承
CryptographicException
派生
属性

示例

下面的代码示例演示如何使用 CryptographicException 类的成员。

C#
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 occurred.
            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.

注解

CryptographicException 使用 HRESULT CORSEC_E_CRYPTO,除非指定了备用错误代码。

有关 CryptographicException实例的初始属性值列表,请参阅构造函数。

构造函数

CryptographicException()

使用默认属性初始化 CryptographicException 类的新实例。

CryptographicException(Int32)

使用指定的 HRESULT 错误代码初始化 CryptographicException 类的新实例。

CryptographicException(SerializationInfo, StreamingContext)
已过时.

使用序列化的数据初始化 CryptographicException 类的新实例。

CryptographicException(String)

使用指定的错误消息初始化 CryptographicException 类的新实例。

CryptographicException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用初始化 CryptographicException 类的新实例。

CryptographicException(String, String)

使用指定格式的指定错误消息初始化 CryptographicException 类的新实例。

属性

Data

获取键/值对的集合,这些键/值对提供有关异常的其他用户定义的信息。

(继承自 Exception)
HelpLink

获取或设置与此异常关联的帮助文件的链接。

(继承自 Exception)
HResult

获取或设置 HRESULT,它是分配给特定异常的编码数值。

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。

(继承自 Exception)
Message

获取描述当前异常的消息。

(继承自 Exception)
Source

获取或设置导致错误的应用程序或对象的名称。

(继承自 Exception)
StackTrace

获取调用堆栈上即时帧的字符串表示形式。

(继承自 Exception)
TargetSite

获取引发当前异常的方法。

(继承自 Exception)

方法

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetBaseException()

在派生类中重写时,返回一个或多个后续异常的根本原因 Exception

(继承自 Exception)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)
已过时.

在派生类中重写时,使用有关异常的信息设置 SerializationInfo

(继承自 Exception)
GetType()

获取当前实例的运行时类型。

(继承自 Exception)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。

(继承自 Exception)

事件

SerializeObjectState
已过时.

序列化异常以创建包含有关异常的序列化数据的异常状态对象时发生。

(继承自 Exception)

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

另请参阅