PDA

View Full Version : سوال: دیکد کردن اطلاعات



water_lily_2012
دوشنبه 23 آبان 1390, 20:04 عصر
سلام
من از کلاس زیر برای کد کردن استفاده می کنم. البته از MD5 استفاده می کنم. بعد از کد کردن، رمز را در جدول ذخیره می کنم. حالا می خواهم رمز را از جدول خوانده و دیکد کنم ولی متاسفانه نمی دونم باید چه کار کنم.
لطفا دوستان کمک کنند.


public string ComputeHash(string plainText,
string hashAlgorithm,
byte[] saltBytes)
{
if (saltBytes == null)
{
int minSaltSize = 4;
int maxSaltSize = 8;

// Generate a random number for the size of the salt.
Random random = new Random();
int saltSize = random.Next(minSaltSize, maxSaltSize);

// Allocate a byte array, which will hold the salt.
saltBytes = new byte[saltSize];

// Initialize a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

// Fill the salt with cryptographically strong byte values.
rng.GetNonZeroBytes(saltBytes);
}

// Convert plain text into a byte array.
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

// Allocate array, which will hold plain text and salt.
byte[] plainTextWithSaltBytes =
new byte[plainTextBytes.Length + saltBytes.Length];

// Copy plain text bytes into resulting array.
for (int i = 0; i < plainTextBytes.Length; i++)
plainTextWithSaltBytes[i] = plainTextBytes[i];

// Append salt bytes to the resulting array.
for (int i = 0; i < saltBytes.Length; i++)
plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];

HashAlgorithm hash;

// Make sure hashing algorithm name is specified.
if (hashAlgorithm == null)
hashAlgorithm = "";

// Initialize appropriate hashing algorithm class.
switch (hashAlgorithm.ToUpper())
{
case "SHA1":
hash = new SHA1Managed();
break;

case "SHA256":
hash = new SHA256Managed();
break;

case "SHA384":
hash = new SHA384Managed();
break;

case "SHA512":
hash = new SHA512Managed();
break;

default:
hash = new MD5CryptoServiceProvider();
break;
}

// Compute hash value of our plain text with appended salt.
byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);

// Create array which will hold hash and original salt bytes.
byte[] hashWithSaltBytes = new byte[hashBytes.Length +
saltBytes.Length];

// Copy hash bytes into resulting array.
for (int i = 0; i < hashBytes.Length; i++)
hashWithSaltBytes[i] = hashBytes[i];

// Append salt bytes to the result.
for (int i = 0; i < saltBytes.Length; i++)
hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];

// Convert result into a base64-encoded string.
string hashValue = Convert.ToBase64String(hashWithSaltBytes);

// Return the result.
return hashValue;
}

nunegandom
دوشنبه 23 آبان 1390, 21:17 عصر
هش کردن، یه جور کد کردنه یک طرفه هست و شما نمیتونی هش رو دیکد کنی،برای رمز ها استفاده میشه که اگه جایی هک شد و هکر به هش دست پیدا کرد هم نتونه کاری کنه!

omidh2007
سه شنبه 24 آبان 1390, 00:00 صبح
همونطور که دوست خوبمون گفت ، Hash یه طرفه هستش
( البته یه سری سایت هایی هستن که پول میگیرن و با چند تا سرور خیلی قوی Hash رو پیدا میکنن !!)
به هر حال برای Hash کردن میتونی از این الگوریتم استفاده کنی .


public static string GetMD5Hash(string input)
{
System.Security.Cryptography.MD5CryptoServiceProvi der x = new System.Security.Cryptography.MD5CryptoServiceProvi der();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
string password = s.ToString();
return password;
}




و وقتی مثلا طرف توی فرم لاگین ، پسورد وارد کرد ، پسورد باید Hash بشه و Hash ساخته شده با Hash موجود در دیتابیس مقایسه بشه .
اگه با هم برابر بودن ، کاربر اجازه ورود میگیره .
اگر اشتباه نکرده باشم ، Windows هم توی فایل SAM همین مکانیزم رو به کار میبره .

water_lily_2012
سه شنبه 24 آبان 1390, 10:48 صبح
نه متاسفانه، می خواهم خود رمز را نشان بدهم و دنبال راهی بودم. وگرنه الگوریتمی که صحت رمز را بررسی کنه را دارم.