رمز نگاری رشته در asp.net va csharp

, , پیغام بگذارید

در این نوشته کوتاه قصد دارم روش رمز نگاری رشته به روش md5 در محیط سی شارپ csahrp  اشاره کنم البته رمزنگاری متن زمانی ارزش خود را نشان می دهد که دیتابیس شما هک شده در ان صورت رشته های رمز شده قابل تشخص نخواهد بود البته در صورتی هکر از روش رمز نگاری استفاده شده را حدس بزند موفق به تشخیص رشته رمز شده خواهد بود چرا روش رمز نگاری md5   روش رمز نگاری روش دو طرفه می باشد قابل بازیابی است حالا بدون توضیح اصلی میریم این روش رمز نگاری رو شرح بدیم

ابتدا باید  کلاس

Cryptography

از فضای کار زیر

System.Security

بصورت زیر به برنامه یا فرم یا کلاس مورد نظر اضافه کنیم

using System.Security.Cryptography;

بعد از افزون کلاس فوق می توان تابع ,md5  را تعریف و از ان استفاده کرد که بصورت زیر خواهد بود

using System;
using System.Security.Cryptography;
using System.Text;

namespace MD5Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "Hello World!";
            using (MD5 md5Hash = MD5.Create())
تعریف نوع داده هش
            {
                string hash = GetMd5Hash(md5Hash, source);
تابع مورد نظر عمل هش انجام میده در پایین تعریف شده 
                Console.WriteLine("The MD5 hash of " + source + " is: " + hash + ".");

                Console.WriteLine("Verifying the hash...");

                if (VerifyMd5Hash(md5Hash, source, hash))
                {
                    Console.WriteLine("The hashes are the same.");
                }
                else
                {
                    Console.WriteLine("The hashes are not same.");
                }
            }



        }
        static string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
انجام عمل رمز نگاری یا هش 
            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
دراین قسمت نیز حرف ایکس 2 به متن رمز شده اضافه میکند 
            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        // Verify a hash against a string.
        static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
        {
            // Hash the input.
            string hashOfInput = GetMd5Hash(md5Hash, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}

// This code example produces the following output:
//
// The MD5 hash of Hello World! is: ed076287532e86365e841e92bfc50d8c.
// Verifying the hash...
// The hashes are the same.

 

در  کد بالا مشاهده می فرماید ابتدا در تابع اولی متنی بصورت رمز کرده در قمست اصلی عمل چک درستی رمز متن رمز شده را چک میکند

 

پاسخ دادن

anti spam *