Beispiel #1
0
        public static string MD5EncryptPassword(string password, MD5ResultMode mode)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
            string text = BitConverter.ToString(mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(password)));

            mD5CryptoServiceProvider.Clear();
            if (mode != MD5ResultMode.Strong)
            {
                return(text.Replace("-", null).Substring(8, 16));
            }
            return(text.Replace("-", null));
        }
Beispiel #2
0
        /// <summary>
        /// MD5
        /// </summary>
        /// <param name="password"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static string MD5EncryptPassword(string password, MD5ResultMode mode)
        {
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }
            using MD5 mD5 = MD5.Create();//MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();

            byte[] passd = Encoding.UTF8.GetBytes(password);

            byte[] md5bytes = mD5.ComputeHash(passd);

            string text0;

            if (mode == MD5ResultMode.Strong)
            {
                text0 = BitConverter.ToString(md5bytes);
            }
            else
            {
                text0 = BitConverter.ToString(md5bytes, 4, 8);
            }

            string text1 = text0.Replace("-", null);

            //string text = BitConverter.ToString(mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(password)));
            mD5.Clear();

            return(text1);

            //if (mode != MD5ResultMode.Strong)
            //{
            //    return text.Replace("-", null).Substring(8, 16);
            //}
            //return text.Replace("-", null);
        }
Beispiel #3
0
 /// <summary>
 /// MD5 加密
 /// </summary>
 /// <param name="password">要加密的字符串</param>
 /// <param name="mode">加密强度</param>
 /// <returns>等效于此实例经过 MD5 加密密文</returns>
 public static string MD5EncryptPassword(string password, MD5ResultMode mode)
 {
     if (password == null)
     {
         throw new ArgumentNullException("password");
     }
     MD5CryptoServiceProvider ServiceProvider = new MD5CryptoServiceProvider();
     string NewPassword = BitConverter.ToString(ServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(password)));
     ServiceProvider.Clear();
     if (mode != MD5ResultMode.Strong)
     {
         return NewPassword.Replace("-", null).Substring(8, 0x10);
     }
     return NewPassword.Replace("-", null);
 }