首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

CLR 內幕深度解析:Visual Studio "Orcas" 新增核心類庫全覽

     編輯:關於.NET

下一版 Microsoft .NET Framework(隨 Visual Studio "Orcas" 釋出)的組件分為「Red Bits」與「Green Bits」兩大類。Red Bits 包含 .NET Framework 2.0 與 3.0 的現有庫(如 mscorlib.dll、System.dll),為確保高度向後相容性,對其修改嚴格受限。Green Bits 則是全新庫組件(如 System.Core.dll),在 Red Bits 之上提供新功能。本文深入探討 CLR 為新版本貢獻的核心功能:Suite B 加密支援、大整數運算、高效能集合與管道通訊。

Suite B 加密標準支援

Visual Studio "Orcas" 主要目標之一是支援 NSA 訂定的 Suite B 加密演算法集,包含:

  • AES 加密(128/256 位金鑰)
  • SHA-256 與 SHA-384 雜湊
  • ECDSA 數位簽章(256/384 位橢圓曲線)
  • ECDH 金鑰交換(256/384 位橢圓曲線)

新增 FIPS 認證的實作類別位於 System.Security.Cryptography 命名空間:

  • AesCryptoServiceProvider
  • SHA256CryptoServiceProvider
  • SHA384CryptoServiceProvider

這些類別封裝 Windows 原生加密服務提供者(CSP),行為與 managed 版本幾乎相同,開發者可互換使用。

CngKey 類別:金鑰管理抽象層

為支援 CNG(Cryptography Next Generation)架構,新增 CngKey 類別抽象金鑰儲存與操作,類似 CAPI 的金鑰容器概念。可用於開啟、建立、刪除、匯出金鑰,並搭配 ECDsaCng、ECDiffieHellmanCng 使用。

橢圓曲線加密(ECC)實作

ECC 為非對稱加密,效率優於傳統 RSA。NSA 建議:保護 256 位對稱金鑰需 15,360 位 RSA 金鑰,但僅需 521 位 ECC 金鑰即可達同等安全強度。

ECDsaCng:數位簽章

// 簽署資料
byte[] SignMyData(byte[] data) {
    ECDsaCng signingAlg = new ECDsaCng(CngKey.Open("MyKey"));
    return signingAlg.SignData(data);
}

// 驗證簽章
bool VerifyJanesSignature(byte[] data, byte[] signature) {
    ECDsaCng signingAlg = new ECDsaCng(CngKey.Open("JanesKey"));
    return signingAlg.VerifyData(data, signature);
}

可透過 HashAlgorithm 屬性指定雜湊演算法(預設 SHA-256),支援 SHA-384、SHA-512 等。

ECDiffieHellmanCng:金鑰交換

bool Generate256BitKey(string otherParty) {
    ECDiffieHellmanCng keyExchAlg = new ECDiffieHellmanCng(CngKey.Open("MyKey"));
    byte[] myPublicKey = keyExchAlg.PublicKey.ToByteArray();
    // 發送 myPublicKey 給對方...
    byte[] otherPartysPublicKeyBytes = ...; // 接收對方公鑰
    ECDiffieHellmanCngPublicKey otherPartysPublicKey = 
        ECDiffieHellmanCngPublicKey.FromByteArray(otherPartysPublicKeyBytes, CngKeyBlobFormat.EccPublicBlob);
    return keyExchAlg.DeriveKeyMaterial(otherPartysPublicKey);
}

雙方均可衍生相同對稱金鑰,但必須透過驗證機制確保公鑰真實性,防止中間人攻擊。

大整數支援:BigInteger 類別

位於 System.Numerics 命名空間,支援任意大小整數運算(受限於記憶體)。適用於天文計算、統計學、密碼學大質數等場景。

using System.Numerics;

static BigInteger Factorial(BigInteger x) {
    BigInteger result = 1;
    for (BigInteger i = 2; i <= x; i++) result *= i;
    return result;
}

// 1000! 將輸出 2568 位數字

提供靜態方法:Abs、Compare、DivRem、GreatestCommonDivisor、ModPow、Pow 等。目前僅支援十進位 (D)、一般 (G)、十六進位 (X) 格式化。

高效能集合:HashSet<T>

位於 System.Collections.Generic,為無序不重複元素集合,提供標準集合運算:

HashSet<int> set1 = new HashSet<int>();
set1.Add(1); set1.Add(3); set1.Add(5); set1.Add(3); // 僅含 1,3,5

HashSet<int> set2 = new HashSet<int>(new int[] { 2, 4, 6 });
set2.UnionWith(set1); // set2 變為 1,2,3,4,5,6

核心方法:

  • UnionWith(聯集)、IntersectWith(交集)、ExceptWith(差集)、SymmetricExceptWith(對稱差集)
  • 參數接受 IEnumerable<T>,可與任何集合互操作
  • 額外提供 IsSubsetOf、IsProperSubsetOf、IsSupersetOf、IsProperSupersetOf

管道通訊:匿名與具名管道

新增 System.IO.Pipes 命名空間,支援同機或跨網路進程間通訊 (IPC)。

匿名管道(父子進程,單向,本機)

// 父進程
using (var pipeStream = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) {
    process.StartInfo.Arguments = pipeStream.GetClientHandleAsString();
    process.Start();
    pipeStream.DisposeLocalCopyOfClientHandle();
    using (var sw = new StreamWriter(pipeStream)) { sw.AutoFlush = true; sw.WriteLine(Console.ReadLine()); }
}

// 子進程
using (var pipeStream = new AnonymousPipeClientStream(PipeDirection.In, args[0])) {
    using (var sr = new StreamReader(pipeStream)) {
        string temp; while ((temp = sr.ReadLine()) != null) Console.WriteLine(temp);
    }
}

具名管道(全雙工、跨網路、多實例、模擬、訊息模式)

// 伺服器
using (var pipeStream = new NamedPipeServerStream("mypipe")) {
    pipeStream.WaitForConnection();
    using (var sw = new StreamWriter(pipeStream)) { sw.AutoFlush = true; sw.WriteLine("Hello"); sw.WriteLine("World"); }
}

// 用戶端
using (var pipeStream = new NamedPipeClientStream("mypipe")) {
    pipeStream.Connect();
    using (var sr = new StreamReader(pipeStream)) {
        string temp; while ((temp = sr.ReadLine()) != null) Console.WriteLine(temp);
    }
}

訊息模式支援可變長度訊息邊界,透過 PipeTransmissionMode.Message 與輔助類別 MessageWriter/MessageReader 實現。

總結

本文涵蓋的類別僅為 "Orcas" Green Bits 功能的一部份。開發者可下載最新 CTP 體驗,並至 BCL Team Blog 提供回饋。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved