下一版 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 加密支援、大整數運算、高效能集合與管道通訊。
Visual Studio "Orcas" 主要目標之一是支援 NSA 訂定的 Suite B 加密演算法集,包含:
新增 FIPS 認證的實作類別位於 System.Security.Cryptography 命名空間:
AesCryptoServiceProviderSHA256CryptoServiceProviderSHA384CryptoServiceProvider這些類別封裝 Windows 原生加密服務提供者(CSP),行為與 managed 版本幾乎相同,開發者可互換使用。
為支援 CNG(Cryptography Next Generation)架構,新增 CngKey 類別抽象金鑰儲存與操作,類似 CAPI 的金鑰容器概念。可用於開啟、建立、刪除、匯出金鑰,並搭配 ECDsaCng、ECDiffieHellmanCng 使用。
ECC 為非對稱加密,效率優於傳統 RSA。NSA 建議:保護 256 位對稱金鑰需 15,360 位 RSA 金鑰,但僅需 521 位 ECC 金鑰即可達同等安全強度。
// 簽署資料
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 等。
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);
}
雙方均可衍生相同對稱金鑰,但必須透過驗證機制確保公鑰真實性,防止中間人攻擊。
位於 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) 格式化。
位於 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 提供回饋。