رمزنگاری پساکوانتومی و امنتر در .NET 10
نویسنده: وحید نصیری
تاریخ: ۱۴۰۴/۰۹/۱۱ ۰۹:۱۸
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System.Security.Cryptography;
using System.Text;
// بررسی پشتیبانی پلتفرم
if (!MLDsa.IsSupported)
throw new PlatformNotSupportedException("ML-DSA در این سیستم پشتیبانی نمیشود");
// تولید جفتکلید ML-DSA-65 (تقریباً معادل امنیت ۱۹۲ بیت)
using var mlDsa = MLDsa.GenerateKey(MLDsaAlgorithm.MLDsa65);
// صادرات کلید عمومی (برای بهاشتراکگذاری)
byte[] publicKey = mlDsa.ExportSubjectPublicKeyInfo();
Console.WriteLine($"اندازه کلید عمومی: {publicKey.Length} بایت");
// متن دلخواه برای امضا
string message = "این یک پیام بسیار مهم است که باید تا سال ۲۰۴۰ معتبر بماند.";
byte[] data = Encoding.UTF8.GetBytes(message);
// امضا کردن
byte[] signature = mlDsa.SignData(data);
Console.WriteLine($"اندازه امضا: {signature.Length} بایت (~3.3 KB برای ML-DSA-65)");
// ——— مرحله تأیید ———
using var verifier = MLDsa.ImportSubjectPublicKeyInfo(publicKey);
bool valid = verifier.VerifyData(data, signature);
Console.WriteLine($"امضا معتبر است؟ {valid}"); // True
// تست دستکاری
data[0] ^= 1; // یک بیت را عوض میکنیم
bool tampered = verifier.VerifyData(data, signature);
Console.WriteLine($"امضا پس از دستکاری معتبر است؟ {tampered}"); // Falseusing System.Security.Cryptography;
using System.Text;
// کلید اصلی (در دنیای واقعی از Key Vault یا HSM بگیرید)
byte[] masterKey = RandomNumberGenerator.GetBytes(32); // 256 بیت
string secret = "sk-proj-SuperSecretOpenAIKey1234567890";
byte[] plaintext = Encoding.UTF8.GetBytes(secret);
// ——— رمزنگاری ———
byte[] nonce = RandomNumberGenerator.GetBytes(12); // هر بار باید یکتا باشد!
byte[] ciphertext = new byte[plaintext.Length];
byte[] tag = new byte[16]; // تگ احراز هویت
using var aes = new AesGcm(masterKey);
aes.Encrypt(nonce, plaintext, ciphertext, tag);
// ذخیرهسازی: nonce + ciphertext + tag را کنار هم نگه دارید
byte[] stored = nonce.Concat(ciphertext).Concat(tag).ToArray();
// ——— رمزگشایی ———
static string Decrypt(byte[] masterKey, byte[] storedData)
{
var nonce = storedData.AsSpan(0, 12);
var tag = storedData.AsSpan(storedData.Length - 16);
var ciphertext = storedData.AsSpan(12, storedData.Length - 28);
using var aes = new AesGcm(masterKey);
byte[] recovered = new byte[ciphertext.Length];
aes.Decrypt(nonce, ciphertext, tag, recovered);
return Encoding.UTF8.GetString(recovered);
}
// استفاده
string apiKey = Decrypt(masterKey, stored);
Console.WriteLine(apiKey); // کلید اصلی برمیگردد
// تست دستکاری → به طور خودکار خطا میدهد
ciphertext[0] ^= 1;
try { Decrypt(masterKey, stored); }
catch (CryptographicException) { Console.WriteLine("دستکاری تشخیص داده شد!"); }// بارگذاری کلید رمز شده از دیسک
byte[] encryptedKey = File.ReadAllBytes(".openai/key.enc");
string apiKey = Decrypt(masterKey, encryptedKey);
// استفاده موقت
OpenAiClient client = new(apiKey);
// ... استفاده ...
// پاک کردن از حافظه
Array.Clear(Encoding.UTF8.GetBytes(apiKey), 0, apiKey.Length);