当前位置: 首页>>代码示例>>C#>>正文


C# CryptographicUnexpectedOperationException类代码示例

本文整理汇总了C#中System.Security.Cryptography.CryptographicUnexpectedOperationException的典型用法代码示例。如果您正苦于以下问题:C# CryptographicUnexpectedOperationException类的具体用法?C# CryptographicUnexpectedOperationException怎么用?C# CryptographicUnexpectedOperationException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


CryptographicUnexpectedOperationException类属于System.Security.Cryptography命名空间,在下文中一共展示了CryptographicUnexpectedOperationException类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Security.Cryptography;
using System.Runtime.Serialization;

class Members
{
    [STAThread]
    static void Main(string[] args)
    {
        Members testRun = new Members();

        testRun.TestConstructors();
        testRun.ShowProperties();

        Console.WriteLine("Sample ended successfully, " +
            " press Enter to continue.");
        Console.ReadLine();
    }

    // Test each public implementation of the
    // CryptographicUnexpectedOperationException constructors.
    private void TestConstructors()
    {
        EmptyConstructor();
        StringConstructor();
        StringExceptionConstructor();
        StringStringConstructor();
    }

    private void EmptyConstructor()
    {
        // Construct a CryptographicUnexpectedOperationException
        // with no parameters.
        CryptographicUnexpectedOperationException cryptographicException =
            new CryptographicUnexpectedOperationException();
        Console.WriteLine("Created an empty " + 
            "CryptographicUnexpectedOperationException.");
    }

    private void StringConstructor()
    {
        // Construct a CryptographicUnexpectedOperationException
        // using a custom error message.
        string errorMessage = ("Unexpected operation exception.");
        CryptographicUnexpectedOperationException cryptographicException =
            new CryptographicUnexpectedOperationException(errorMessage);
        Console.WriteLine("Created a " + 
            "CryptographicUnexpectedOperationException with the following " +
            " error message: " + errorMessage);
    }

    private void StringExceptionConstructor()
    {
        // Construct a CryptographicUnexpectedOperationException using a 
        // custom error message and an inner exception.
        string errorMessage = ("The current operation is not supported.");
        NullReferenceException nullException = new NullReferenceException();
        CryptographicUnexpectedOperationException cryptographicException = 
            new CryptographicUnexpectedOperationException(
            errorMessage, nullException);
        Console.WriteLine("Created a " +
            "CryptographicUnexpectedOperationException with the following" +
            "error message: " + errorMessage + " and inner exception: "
            + nullException.ToString());
    }

    private void StringStringConstructor()
    {
        // Create a CryptographicUnexpectedOperationException using a time
        // format and the current date.
        string dateFormat = "{0:t}";
        string timeStamp = (DateTime.Now.ToString());
        CryptographicUnexpectedOperationException cryptographicException = 
            new CryptographicUnexpectedOperationException(
            dateFormat, timeStamp);
        Console.WriteLine("Created a " + 
            "CryptographicUnexpectedOperationException with (" + dateFormat +
            ") as the format and (" + timeStamp + ") as the message.");
    }

    // Construct an invalid DSACryptoServiceProvider to throw a
    // CryptographicUnexpectedOperationException for introspection.
    private void ShowProperties()
    {
        // Attempting to encode an OID greater than 127 bytes is not supported
        // and will throw an exception.
        string veryLongNumber = "1234567890.1234567890.";
        for (int i=0; i < 4; i++)
        {
            veryLongNumber += veryLongNumber;
        }
        veryLongNumber += "0";
        try 
        {
            byte[] tooLongOID = CryptoConfig.EncodeOID(veryLongNumber);
        }
        catch(CryptographicUnexpectedOperationException ex)
        {
            // Retrieve the link to the Help file for the exception.
            string helpLink = ex.HelpLink;
            
            // Retrieve the exception that caused the current
            // CryptographicUnexpectedOperationException.
            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
            // CryptographicUnexpectedOperationException.
            System.Exception baseException = ex.GetBaseException();
            string baseExceptionMessage = "";
            if (baseException != null)
            {
                baseExceptionMessage = baseException.Message;
            }

            Console.WriteLine("Caught an expected exception:");
            Console.WriteLine(entireException + "\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 CryptographicUnexpectedOperationException 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);
    }
}
//
开发者ID:.NET开发者,项目名称:System.Security.Cryptography,代码行数:169,代码来源:CryptographicUnexpectedOperationException

输出:

Created an empty CryptographicUnexpectedOperationException.
Created a CryptographicUnexpectedOperationException with the following 
error message: Unexpected operation exception.
Created a CryptographicUnexpectedOperationException with the following
error message: The current operation is not supported. and inner exception:
System.NullReferenceException: Object reference not set to an instance of
an object.
Created a CryptographicUnexpectedOperationException with ({0:t}) as the
format and (2/24/2004 2:35:22 PM) as the message.
Caught an expected exception:
System.Security.Cryptography.CryptographicUnexpectedOperationException: 
Encoded OID length is too large (greater than 0x7f bytes).
at System.Security.Cryptography.CryptoConfig.EncodeOID(String str)
at Members.ShowProperties() in c:\consoleapplication1\class1.cs:line 106

Properties of the exception are as follows:
Message: Encoded OID length is too large (greater than 0x7f bytes).
Source: mscorlib
Stack trace:    at System.Security.Cryptography.CryptoConfig.EncodeOID(
String str)
at Members.ShowProperties() in c:\consoleapplication1\class1.cs:line 106
Help link:
Target site's name: EncodeOID
Base exception message: Encoded OID length is too large (greater than 0x7f
bytes).
Inner exception message:
Sample ended successfully,  press Enter to continue.


注:本文中的System.Security.Cryptography.CryptographicUnexpectedOperationException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。