PDA

View Full Version : سوال: سوال: ذخیره هش کد در فایل؟



mtnam1372
شنبه 27 خرداد 1391, 14:00 عصر
سلام من با این کد هش کد تولید کردم



string s, s1;
s = Console.ReadLine();
MD5 o = new MD5CryptoServiceProvider();
o.ComputeHash(System.Text.Encoding.UTF8.GetBytes(s ));
s1 = System.Text.Encoding.UTF8.GetString(o.Hash);
Console.WriteLine(s1);
Console.ReadKey();


خوب همونطور که میبینید من هش کد رو تبدیل به رشته کردم و بعد چاپ کردم
ولی وقتی این رشترو در یه فایل متنی ذخیره میکنم اون بعضی از کارکتر های وِزرو خراب میکنه
که وقتی من دوباره کد رو از کاربر گرفتم و تبدیل به هش کد کردم با قبلی فرق میکنه این مشکل منه
به نظر شما باینری ذخیرش کنم؟چطوری کدشو بزنید
ایا روش بهتری هست؟؟
نمی خام از بانک استفاده کنم
این کد رو برای اجرای اول از کاربر میگیرم و هش شده ذخیره میکنم و میخام دفعات بعدی به کمک همین کد کاربر لاگین بشه مثل وب سایت ها

tooraj_azizi_1035
شنبه 27 خرداد 1391, 14:08 عصر
سلام
خروجی خاصیت Hash از نوع []byte است.
از این تابع برای ذخیره استفاده کنید ببینید باز هم مشکل پیش می آید:


public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)08{
09 try
10 {
11 // Open file for reading
12 System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
13
14 // Writes a block of bytes to this stream using data from a byte array.
15 _FileStream.Write(_ByteArray, 0, _ByteArray.Length);
16
17 // close file stream
18 _FileStream.Close();
19
20 return true;
21 }
22 catch (Exception _Exception)
23 {
24 // Error
25 Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
26 }
27
28 // error occured, return false
29 return false;
30}

mtnam1372
شنبه 27 خرداد 1391, 14:43 عصر
تو رج جان من نفهمیدم چی شد با توجه به اینکه من میخام هش کد رو از کاربر بگیره و در یه فایل در درایو c ذخیره کنه
کد رو کامل کن و بزار ممنونتم


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
string s ;
string sik="C:\\d.txt";
Console.WriteLine("enter password");
s = Console.ReadLine();
MD5 o = new MD5CryptoServiceProvider();
o.ComputeHash(System.Text.Encoding.UTF8.GetBytes(s ));


System.IO.FileStream _FileStream = new System.IO.FileStream(sik, System.IO.FileMode.Create, System.IO.FileAccess.Write);
_FileStream.Write(o, 0,????);

Console.WriteLine("sabt shod");
Console.ReadKey();
}
}
}

Saeed_m_Farid
شنبه 27 خرداد 1391, 17:59 عصر
این کد کار موردنظر شما رو انجام میده و برای مقایسه باینری هم میدونم که خودتون میتونید انجام بدین ولی اگه مشکل داشتین، بگین اونم با هم حل کنیم:
class Program
{
static void Main(string[] args)
{
string input, path = @"C:\CookieLike.bin";
Console.WriteLine("Enter password: ");
input = Console.ReadLine();
byte[] data =
(new MD5CryptoServiceProvider()).ComputeHash(
System.Text.Encoding.UTF8.GetBytes(input));

try
{
// Open file for reading
System.IO.FileStream _FileStream =
new System.IO.FileStream(
path,
System.IO.FileMode.Create,
System.IO.FileAccess.Write);

// Writes a block of bytes to this
// stream using data from a byte array.
_FileStream.Write(data, 0, data.Length);

// close file stream
_FileStream.Close();

Console.WriteLine("Binary array saved to: {0}", path);
}
catch (Exception ex)
{
// Error
Console.WriteLine("Error: {0}", ex.ToString());
}
Console.ReadKey();
}
}

ولی :




ولی وقتی این رشترو در یه فایل متنی ذخیره میکنم اون بعضی از کارکتر های وِزرو خراب میکنه

شما یه جای کارتون ایراد داره، والّا کاراکترها خودبخود که خراب نمیشن! احتمالاً متد ذخیره، تبدیل و یا مقایسه تون درست نیست؛ msdn برای hash MD5 کاملاً برای موردی مشابه درخواست شما، مثال (http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5.aspx) زده:
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"));
}

// 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;
}
}

}