[引]生成加密和解密的密钥
1.對稱密鑰
2.不對稱密鑰
3.將非對稱密鑰存儲在密鑰容器中
4.將非對稱密鑰存儲在密鑰容器中示例
===============================
創(chuàng)建和管理密鑰是加密過程的一個重要部分。
對稱算法要求創(chuàng)建必須對不應(yīng)解密數(shù)據(jù)的任何人保密的密鑰和初始化向量 (IV)。
不對稱算法要求創(chuàng)建一個公鑰和一個私鑰。
公鑰可以對任何人公開,
而私鑰必須只為將要對用公鑰加密的數(shù)據(jù)進行解密的一方知道。
對稱密鑰
========
.NET Framework 提供的對稱加密類
需要一個密鑰和一個新的初始化向量 (IV) 來加密和解密數(shù)據(jù)。
每當(dāng)使用默認構(gòu)造函數(shù)創(chuàng)建某個托管對稱加密類的新實例時,
都將自動創(chuàng)建新的密鑰和 IV。
無論您允許誰解密您的數(shù)據(jù),
他或她都必須擁有同樣的密鑰和 IV 并使用相同的算法。
通常,應(yīng)該為每個會話創(chuàng)建新的密鑰和 IV,
并且無論是密鑰還是 IV 都不應(yīng)存儲以用于稍后的會話中。
為了將對稱密鑰和 IV 傳送給遠程方,
通常使用不對稱加密來加密對稱密鑰和 IV。
通過不安全的網(wǎng)絡(luò)發(fā)送這些值而不對這些值進行加密會極不安全,
這是因為截獲這些值的任何人都能夠解密您的數(shù)據(jù)。
下面的示例顯示
實現(xiàn)TripleDES 算法的 TripleDESCryptoServiceProvider 類的新實例的創(chuàng)建。
Visual Basic:
Dim TDES As TripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider()
C#:
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
在執(zhí)行上面的代碼時,將生成新的密鑰和 IV 并將其分別放置在 Key 和 IV 屬性中。
有時您可能需要生成多個密鑰。
這種情況下,可以創(chuàng)建實現(xiàn)對稱算法的類的新實例,
然后通過調(diào)用 GenerateKey 和 GenerateIV 方法創(chuàng)建新的密鑰和 IV。
下面的代碼示例闡釋如何在創(chuàng)建了不對稱加密類的新實例后創(chuàng)建新的密鑰和 IV。
Visual Basic :
Dim TDES As TripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider()
TDES.GenerateIV()
TDES.GenerateKey()
C# :
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
TDES.GenerateIV();
TDES.GenerateKey();
當(dāng)執(zhí)行上面的代碼時,創(chuàng)建 TripleDESCryptoServiceProvider 的新實例后將生成密鑰和 IV。
調(diào)用 GenerateKey 和 GenerateIV 方法時將創(chuàng)建另一個密鑰和 IV。
不對稱密鑰
==========
.NET Framework 為不對稱加密提供了
RSACryptoServiceProvider 和 DSACryptoServiceProvider 類。
這些類在您使用默認構(gòu)造函數(shù)創(chuàng)建新實例時創(chuàng)建一個公鑰/私鑰對。
既可以存儲不對稱密鑰以用在多個會話中,
也可以只為一個會話生成不對稱密鑰。
公鑰可以被廣泛地使用,私鑰應(yīng)被嚴密地保護起來。
每當(dāng)創(chuàng)建不對稱算法類的新實例時,都生成一個公鑰/私鑰對。
創(chuàng)建該類的新實例后,可以用以下兩種方法之一提取密鑰信息:
ToXMLString 方法,它返回密鑰信息的 XML 表示形式。
ExportParameters 方法,它返回 RSAParameters 結(jié)構(gòu)以保存密鑰信息。
兩個方法都接受布爾值,該值指示是只返回公鑰信息還是同時返回公鑰和私鑰信息。
通過使用 ImportParameters 方法,
可以將 RSACryptoServiceProvider 類初始化為 RSAParameters 結(jié)構(gòu)的值。
千萬不要將不對稱私鑰逐字存儲(或者說以明文形式存儲)在本地計算機上。
如果需要存儲私鑰,則應(yīng)使用密鑰容器。
有關(guān)如何在密鑰容器中存儲私鑰的更多信息,
請參見如何:將非對稱密鑰存儲在密鑰容器中。
下面的代碼示例創(chuàng)建 RSACryptoServiceProvider 類的一個新實例,
創(chuàng)建一個公鑰/私鑰對,并將公鑰信息保存在 RSAParameters 結(jié)構(gòu)中。
Visual Basic? 復(fù)制代碼
'Generate a public/private key pair.
Dim RSA as RSACryptoServiceProvider = new RSACryptoServiceProvider()
'Save the public key information to an RSAParameters structure.
Dim RSAKeyInfo As RSAParameters = RSA.ExportParameters(false)
?
C#? 復(fù)制代碼
//Generate a public/private key pair.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Save the public key information to an RSAParameters structure.
RSAParameters RSAKeyInfo = RSA.ExportParameters(false);
?
將非對稱密鑰存儲在密鑰容器中
============================
創(chuàng)建非對稱密鑰并將其保存在密鑰容器中
------------
1.創(chuàng)建 CspParameters 類的一個新實例,
? 并將您要密鑰容器使用的名稱傳遞給 CspParameters.KeyContainerName 字段。
2.為從 AsymmetricAlgorithm 類派生的一個類
?(通常是 RSACryptoServiceProvider 或 DSACryptoServiceProvider)創(chuàng)建一個新實例,
? 并將先前創(chuàng)建的 CspParameters 對象傳遞給其構(gòu)造函數(shù)。
從密鑰容器中刪除密鑰
------------
1.創(chuàng)建 CspParameters 類的一個新實例,
? 并將您要密鑰容器使用的名稱傳遞給 CspParameters.KeyContainerName 字段。
2.為從 AsymmetricAlgorithm 類派生的一個類
(通常是 RSACryptoServiceProvider 或 DSACryptoServiceProvider)創(chuàng)建一個新實例,
?并將先前創(chuàng)建的 CspParameters 對象傳遞給其構(gòu)造函數(shù)。
3.將從 AsymmetricAlgorithm 中派生的類
? 的 PersistKeyInCSP 屬性設(shè)置為 false(在 Visual Basic 中為 False)。
4.調(diào)用從 AsymmetricAlgorithm 派生的類的 Clear 方法。
? 該方法釋放該類所有的資源并清除密鑰容器。
將非對稱密鑰存儲在密鑰容器中示例
====
下面的示例說明下面這一過程:
創(chuàng)建一個非對稱密鑰,將其保存在密鑰容器中,
以后檢索此密鑰,最后從該容器中刪除此密鑰。
請注意,GenKey_SaveInContainer 方法和 GetKeyFromContainer 方法的代碼相似。
當(dāng)為 CspParameters 對象指定密鑰容器名稱并將其傳遞給 PersistKeyInCsp 屬性
?? 或 PersistKeyInCsp 屬性設(shè)置為 true 的 AsymmetricAlgorithm 對象時,
? 將會發(fā)生以下情況。
? 如果不存在具有指定名稱的密鑰容器,
??????? 則系統(tǒng)將創(chuàng)建一個密鑰容器,但密鑰保持不變。
? 如果確實存在具有指定名稱的密鑰容器,
??????? 則將此容器中的密鑰自動加載到當(dāng)前 AsymmetricAlgorithm 對象中。
? 因此,GenKey_SaveInContainer 方法中的代碼保持密鑰不變,因為它首先運行;
? 而 GetKeyFromContainer 方法中的代碼加載此密鑰,因為它隨后運行。
Visual Basic? 復(fù)制代碼
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
?
C#? 復(fù)制代碼
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.
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/freeliver54/archive/2007/03/05/664695.html
總結(jié)
以上是生活随笔為你收集整理的[引]生成加密和解密的密钥的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hive2
- 下一篇: Code First :使用Entity