مشاركة عبر


كيفية القيام بما يلي: تخزين مفاتيح غير متماثلة في حاوية مفاتيح

لا يجب أبداً تخزين المفاتيح الخاصة الغير متماثلة على هيئة حرفية أو هيئة نص عادي على الكمبيوتر المحلي. و إذا كنت تحتاج لتخزين مفتاح خاص فيجب عليك استخدام حاوية مفاتيح. للحصول على مزيد من المعلومات حول حاوية المفتاح، راجع التعرف على مستوى الجهاز و الحاويات مفتاح RSA مستوى المستخدم.

لإنشاء مفتاح غير متماثل و حفظه في حاوية مفاتيح

  1. قم بإنشاء مثيل جديد من الفئة CspParameters ثم مرر الاسم الذي تريد اطلاقه على حاوية المفتاح إلى الحقل CspParameters.KeyContainerName.

  2. قم بإنشاء مثيل جديد من فئة مشتقة من الفئة AsymmetricAlgorithm(عادةً RSACryptoServiceProvider أو DSACryptoServiceProvider ) ثم قم بتمرير الكائن CspParameters الذي تم إنشاؤه مسبقاً إلى الدالة الإنشائية الخاصة به .

لحذف مفتاح من حاوية مفاتيح

  1. قم بإنشاء مثيل جديد من الفئة CspParameters ثم قم بتمرير الاسم الذي تريد إطلاقه على حاوية المفاتيح إلى الحقل CspParameters.KeyContainerName.

  2. قم بإنشاء مثيل جديد من فئة مشتقة من الفئة AsymmetricAlgorithm(عادةً RSACryptoServiceProvider أو DSACryptoServiceProvider ) ثم قم بتمرير الكائن CspParameters الذي تم إنشاؤه مسبقاً إلى الدالة الإنشائية الخاصة به.

  3. قم بضبط خاصيةPersistKeyInCSP للفئة المشتقة من AsymmetricAlgorithm على false (False في Visual Basic).

  4. قم باستدعاء أسلوب Clear من الفئة المشتقة من AsymmetricAlgorithm. يقوم هذا الأسلوب بتحرير كافة موارد الفئة ومسح حاوية المفاتيح.

مثال

يوضح المثال التالي كيفية إنشاء مفتاح غير متماثل، وحفظه في حاوية مفاتيح، واسترداد المفتاح في وقت لاحق، وحذف المفتاح من الحاوية.

يرجى ملاحظة أن التعليمات البرمجية في الأسلوبGenKey_SaveInContainer و الأسلوب GetKeyFromContainer متشابهة. عندما تقوم بتحديد اسم حاوية مفاتيح لكائن CspParameters وتمريرها إلى الكائن AsymmetricAlgorithm مع الخاصية PersistKeyInCsp أو الخاصية PersistKeyInCsp تم تحديدها على true سيحدث ما يلي. إذا لم تتواجد حاوية مفاتيح لها الاسم المحدد، سيتم إنشاء واحدة ويظل المفتاح قائماً. إذا لم تتواجد حاوية مفاتيح لها الاسم المحدد، سيتم تحميل المفتاح الموجود بالحاوية في كائن AsymmetricAlgorithm الحالي. ولذلك ستحتفظ التعليمات البرمجية في الأسلوب GenKey_SaveInContainer بالمفتاح القائم لأنه تم تشغيلها أولاً، بينما ستقوم التعليمات البرمجية في الأسلوب GetKeyFromContainer بتحميل المفتاح لأنه تم تشغيلها ثانياً.

Imports System
Imports System.IO
Imports System.Security.Cryptography
 _

Public Class StoreKey

    Public Shared Sub Main()
        Try
            ' Create a key and save it in a container.
            GenKey_SaveInContainer("MyKeyContainer")

            ' Retrieve the key from the container.
            GetKeyFromContainer("MyKeyContainer")

            ' Delete the key from the container.
            DeleteKeyFromContainer("MyKeyContainer")

            ' Create a key and save it in a container.
            GenKey_SaveInContainer("MyKeyContainer")

            ' Delete the key from the container.
            DeleteKeyFromContainer("MyKeyContainer")
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub

    Public Shared Sub GenKey_SaveInContainer(ByVal ContainerName As String)
        ' Create the CspParameters object and set the key container 
        ' name used to store the RSA key pair.
        Dim cp As New CspParameters()
        cp.KeyContainerName = ContainerName

        ' Create a new instance of RSACryptoServiceProvider that accesses
        ' the key container MyKeyContainerName.
        Dim rsa As New RSACryptoServiceProvider(cp)

        ' Display the key information to the console.
        Console.WriteLine("Key added to container:  {0}", rsa.ToXmlString(True))
    End Sub

    Public Shared Sub GetKeyFromContainer(ByVal ContainerName As String)
        ' Create the CspParameters object and set the key container 
        '  name used to store the RSA key pair.
        Dim cp As New CspParameters()
        cp.KeyContainerName = ContainerName

        ' Create a new instance of RSACryptoServiceProvider that accesses
        ' the key container MyKeyContainerName.
        Dim rsa As New RSACryptoServiceProvider(cp)

        ' Display the key information to the console.
        Console.WriteLine("Key retrieved from container : {0}", rsa.ToXmlString(True))
    End Sub

    Public Shared Sub DeleteKeyFromContainer(ByVal ContainerName As String)
        ' Create the CspParameters object and set the key container 
        '  name used to store the RSA key pair.
        Dim cp As New CspParameters()
        cp.KeyContainerName = ContainerName

        ' Create a new instance of RSACryptoServiceProvider that accesses
        ' the key container.
        Dim rsa As New RSACryptoServiceProvider(cp)

        ' Delete the key entry in the container.
        rsa.PersistKeyInCsp = False

        ' Call Clear to release resources and delete the key from the container.
        rsa.Clear()

        Console.WriteLine("Key deleted.")
    End Sub
End Class
using System;
using System.IO;
using System.Security.Cryptography;

public class StoreKey

{
    public static void Main()
    {
        try
        {
            // Create a key and save it in a container.
            GenKey_SaveInContainer("MyKeyContainer");
            
            // Retrieve the key from the container.
            GetKeyFromContainer("MyKeyContainer");
    
            // Delete the key from the container.
            DeleteKeyFromContainer("MyKeyContainer");

            // Create a key and save it in a container.
            GenKey_SaveInContainer("MyKeyContainer");

            // Delete the key from the container.
            DeleteKeyFromContainer("MyKeyContainer");
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }

    }

    public static void GenKey_SaveInContainer(string ContainerName)
    {
        // Create the CspParameters object and set the key container 
        // name used to store the RSA key pair.
        CspParameters cp = new CspParameters();
        cp.KeyContainerName = ContainerName;

        // Create a new instance of RSACryptoServiceProvider that accesses
        // the key container MyKeyContainerName.
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

        // Display the key information to the console.
        Console.WriteLine("Key added to container: \n  {0}", rsa.ToXmlString(true));
    }

    public static void GetKeyFromContainer(string ContainerName)
    {
        // Create the CspParameters object and set the key container 
        // name used to store the RSA key pair.
        CspParameters cp = new CspParameters();
        cp.KeyContainerName = ContainerName;

        // Create a new instance of RSACryptoServiceProvider that accesses
        // the key container MyKeyContainerName.
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

        // Display the key information to the console.
        Console.WriteLine("Key retrieved from container : \n {0}", rsa.ToXmlString(true));
    }

    public static void DeleteKeyFromContainer(string ContainerName)
    {
        // Create the CspParameters object and set the key container 
        // name used to store the RSA key pair.
        CspParameters cp = new CspParameters();
        cp.KeyContainerName = ContainerName;

        // Create a new instance of RSACryptoServiceProvider that accesses
        // the key container.
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

        // Delete the key entry in the container.
        rsa.PersistKeyInCsp = false;

        // Call Clear to release resources and delete the key from the container.
        rsa.Clear();

        Console.WriteLine("Key deleted.");
    }
}
Key added to container: <RSAKeyValue> Key Information A</RSAKeyValue> Key retrieved from container : <RSAKeyValue> Key Information A</RSAKeyValue> Key deleted. Key added to container: <RSAKeyValue> Key Information B</RSAKeyValue> Key deleted. 

راجع أيضًا:

المبادئ

إنشاء المفاتيح للتشفير و فك التشفير

تشفير البيانات

فك تشفير البيانات

خدمات التشفير

موارد أخرى

مهام التشفير