
الـ hashing غير الـ encryption غير الـ encoding: تستخدم مين وإمتى؟
- كاتب موثّق
محتويات المقال
IbanEncrypted. القيم شكلها عشوائي، وكتير منها بيخلص بـ =. واحد من المراجعين بياخد قيمة منهم، ويشغّل base64 --decode في الـ terminal، ويقرا رقم الحساب البنكي بتاع عميل. والـ helper اللي "بيشفّر" طوله سطرين، ومفيش ولا واحد فيهم جاب سيرة أي key.
الفكرة الغلط الشائعة
اللي بيحصل فعلًا
الـ encoding بيغيّر الشكل، مش مين يقدر يقرا
الـ hashing بيعمل بصمة، مش سر
الـ encryption بيخبّي الداتا ورا key
نفس الـ input، 3 نواتج
mona@example.com. الـ AES-GCM استخدم key عشوائي 256 bit هنرميه بعدها، و nonce عشوائي جديد كل مرة، والناتج بتاعه (الـ nonce والـ ciphertext والـ tag مع بعض) متطبوع كـ Base64، لأن الـ ciphertext هو كمان bytes:
Base64 bW9uYUBleGFtcGxlLmNvbQ==SHA-256 c1b996b40e336d1006b4895c7ac034df41e6078b82bcdb1d8f2c4bc5c916e058SHA-256 c1b996b40e336d1006b4895c7ac034df41e6078b82bcdb1d8f2c4bc5c916e058AES-GCM DojCYZA27TkzhcS0jtdFwlInwCIt24jdRU/PS2CvddQPVVcSLdY5uYHH72s=AES-GCM ujSteGU3PVDZziSJ13MHlXcxeki3mu59MlOLJZCE4atCnaUBQEQHrsh6CHU=AuthenticationTagMismatchException.
نصلّح الـ column "المتشفّر"
using System.Text;public static class IbanStore{ // Looks scrambled in the database; anyone can reverse it public static string Encrypt(string iban) => Convert.ToBase64String(Encoding.UTF8.GetBytes(iban)); public static string Decrypt(string stored) => Encoding.UTF8.GetString(Convert.FromBase64String(stored));}using Microsoft.AspNetCore.DataProtection;public sealed class IbanStore(IDataProtectionProvider provider){ // AES-256-CBC + HMACSHA256 by default, with managed key rotation private readonly IDataProtector _protector = provider.CreateProtector("Payouts.Iban.v1"); public string Protect(string iban) => _protector.Protect(iban); // Throws CryptographicException if the value was tampered with public string Unprotect(string stored) => _protector.Unprotect(stored);}Protect بترجّعها "enciphered and tamper-proofed"، وUnprotect بترمي CryptographicException لو الـ payload "has been tampered with or was produced by a different IDataProtector." والـ purpose string بيمنع إن protector بتاع feature تانية يفك داتا الـ payouts.
تطبقها إزاي في مشروعك
دوّر على الـ encoding المتنكّر دوّر على Convert.ToBase64Stringوbtoa، وعلى أي methods اسمهاEncryptأوDecryptومش بتاخد key، وعلى أي column اسمه شبه*Encrypted. اعمل decode لقيمة منهم وشوف إيه اللي هيطلع.اسأل سؤالين لكل field حساس محتاج الأصل يرجعلك؟ اعمله encrypt. محتاج بس تتأكد منه أو تقارنه؟ اعمله hash. ولا ده ولا ده؟ نصيحة OWASP إن "The best way to protect sensitive information is to not store it in the first place." استخدم library مجرّبة، وعمرك ما تعمل algorithm بنفسك في ASP.NET Core، الـ Data Protection بيغطي أغلب الحالات. ونصيحة OWASP كلها عن الـ custom algorithms 3 كلمات: "Don't do this." خزّن الـ keys بعيد عن الداتا حط الـ keys في key vault أو key ring محمي، وعمرها ما تكون في الـ source control ولا في نفس الـ database اللي فيها الـ ciphertext. وخطّط للـ rotation والـ backups قبل ما تشفّر أول صف.
الخلاصة
الـ Base64 طريقة تنقل بيها bytes كنص؛ أي حد يقدر يعمله decode، ومش بيحمي أي حاجة. الـ hash بصمة طولها ثابت: مش هترجّعه، بس الـ input المتوقَّع يتخمّن. الـ encryption بيخلّي الداتا سر طول ما الـ key سر بس؛ استخدم authenticated modes زي AES-GCM. اختار حسب احتياجك: هتقراها تاني يبقى encrypt، هتقارنها يبقى hash، ولا ده ولا ده يبقى ماتخزّنهاش. استخدم library مجرّبة زي الـ Data Protection بتاع ASP.NET Core، وخلّي الـ keys بعيد عن الداتا.
📚 المصادر
Cryptographic Storage Cheat Sheet — OWASP Cheat Sheet Series Hash function — MDN Web Docs Glossary RFC 4648: The Base16, Base32, and Base64 Data Encodings Consumer APIs overview for ASP.NET Core Data Protection — Microsoft Learn Data Protection key management and lifetime in ASP.NET Core — Microsoft Learn
التعليقات (0)