Thứ Tư, 27 tháng 3, 2013

Mã hóa MD5, Send Email, tạo password ngẫu nhiên

public string MD5(string password) // chuổi password cần mã hóa
    {
        byte[] textbytes = System.Text.Encoding.Default.GetBytes(password);
        try
        {
            System.Security.Cryptography.MD5CryptoServiceProvider crythandler;
            crythandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hash = crythandler.ComputeHash(textbytes);
            String ret = "";
            foreach (byte a in hash)
            {
                if (a < 16)
                    ret += "0" + a.ToString("x");
                else
                    ret += a.ToString("x");
            }
            return ret;
        }
        catch
        {
            throw;
        }
    }
 /// hàm mã hóa password md5
        public string EncodePassword(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();

            originalBytes = UTF8Encoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            return BitConverter.ToString(encodedBytes);
        }

public bool SendMailDetails(string Frommail, string pass, string Tomail, string Tenhienthi, string Title, string Content, string Host)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(Frommail, Tenhienthi, Encoding.UTF8);
            mail.To.Add(Tomail);
            mail.Subject = Title;
            mail.SubjectEncoding = Encoding.UTF8;
            mail.Body = Content;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;

            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Host = Host;
            mySmtpClient.Credentials = new System.Net.NetworkCredential(Frommail, pass);
            mySmtpClient.EnableSsl = true;
            try { mySmtpClient.Send(mail); } //--Gửi mail
            catch { return false; }

            return true;
        }

       Host : smtp.gmail.com : nếu from mail là gmail



        // tạo ra password ngẫu nhiên
        public string GenerateRandomPassword(int length)
        {
            string allowedLetterChars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
            string allowedNumberChars = "0123456789";
            char[] chars = new char[length];
            Random rd = new Random();
            bool useLetter = true;
            for (int i = 0; i < length; i++)
            {
                if (useLetter)
                {
                    chars[i] = allowedLetterChars[rd.Next(0, allowedLetterChars.Length)];
                    useLetter = false;
                }
                else
                {
                    chars[i] = allowedNumberChars[rd.Next(0, allowedNumberChars.Length)];
                    useLetter = true;
                }
            }
            return new string(chars);
        }










Không có nhận xét nào:

Đăng nhận xét