當前位置: 首頁>>代碼示例>>C#>>正文


C# AsymmetricKeyExchangeDeformatter類代碼示例

本文整理匯總了C#中System.Security.Cryptography.AsymmetricKeyExchangeDeformatter的典型用法代碼示例。如果您正苦於以下問題:C# AsymmetricKeyExchangeDeformatter類的具體用法?C# AsymmetricKeyExchangeDeformatter怎麽用?C# AsymmetricKeyExchangeDeformatter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


AsymmetricKeyExchangeDeformatter類屬於System.Security.Cryptography命名空間,在下文中一共展示了AsymmetricKeyExchangeDeformatter類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ContosoDeformatter

//引入命名空間
using System;
using System.Security.Cryptography;

namespace Contoso
{
    public class ContosoDeformatter : AsymmetricKeyExchangeDeformatter
    {
        private RSA rsaKey;

        // Default constructor.
        public ContosoDeformatter(){}

        // Constructor with the public key to use for encryption.
        public ContosoDeformatter(AsymmetricAlgorithm key)
        {
            SetKey(key);
        }

        // Set the public key for encyption operations.
        public override void SetKey(AsymmetricAlgorithm key) {
            if (key != null)
            {
                rsaKey = (RSA)key;
            }
            else
            {
                throw new ArgumentNullException("key");
            }
        }

        // Disallow access to the parameters of the formatter.
        public override String Parameters 
        {
            get { return null; }
            set { ; }
        }

        // Create the encrypted key exchange data from the specified input
        // data. This method uses the RSACryptoServiceProvider only. To
        // support additional providers or provide custom decryption logic,
        // add logic to this member.
        public override byte[] DecryptKeyExchange(byte[] rgbData) {
            byte[] decryptedBytes = null;

            if (rsaKey != null)
            {
                if (rsaKey is RSACryptoServiceProvider)
                {
                    RSACryptoServiceProvider serviceProvder =
                        (RSACryptoServiceProvider)rsaKey;

                    decryptedBytes = serviceProvder.Decrypt(rgbData, true);
                }
                // Add custom decryption logic here.
            }
            else
            {
                throw new CryptographicUnexpectedOperationException(
                    "Cryptography_MissingKey");
            }

            return decryptedBytes;
        }
    }
}
//
開發者ID:.NET開發者,項目名稱:System.Security.Cryptography,代碼行數:67,代碼來源:AsymmetricKeyExchangeDeformatter

輸出:

Data to encrypt : Sample Contoso encryption application.
Encrypted data: Khasdf-3248&$%23
Data decrypted : Sample Contoso encryption application.

This sample completed successfully; press Enter to exit.


注:本文中的System.Security.Cryptography.AsymmetricKeyExchangeDeformatter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。