當前位置: 首頁>>代碼示例>>C#>>正文


C# Cryptography.MD5CryptoServiceProvider類代碼示例

本文整理匯總了C#中System.Security.Cryptography.MD5CryptoServiceProvider的典型用法代碼示例。如果您正苦於以下問題:C# MD5CryptoServiceProvider類的具體用法?C# MD5CryptoServiceProvider怎麽用?C# MD5CryptoServiceProvider使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


MD5CryptoServiceProvider類屬於System.Security.Cryptography命名空間,在下文中一共展示了MD5CryptoServiceProvider類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: MD5

 public static String MD5(String text)
 {
     UTF8Encoding encoder = new UTF8Encoding();
     var md5 = new MD5CryptoServiceProvider();
     byte[] hashedDataBytes = md5.ComputeHash(encoder.GetBytes(text));
     return System.Convert.ToBase64String(hashedDataBytes);
 }
開發者ID:codaxy,項目名稱:common,代碼行數:7,代碼來源:Cryptography.cs

示例2: GetMd5Sum

        // Create an md5 sum string of this string
        public static string GetMd5Sum(this string str)
        {
            // First we need to convert the string into bytes, which
            // means using a text encoder.
            Encoder enc = System.Text.Encoding.Unicode.GetEncoder();

            // Create a buffer large enough to hold the string
            byte[] unicodeText = new byte[str.Length * 2];
            enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true);

            // Now that we have a byte array we can ask the CSP to hash it
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(unicodeText);

            // Build the final string by converting each byte
            // into hex and appending it to a StringBuilder
            var sb = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("X2"));
            }

            // And return it
            return sb.ToString();
        }
開發者ID:jcoxhead,項目名稱:SilverlightExamples,代碼行數:26,代碼來源:Md5Sum.cs

示例3: GetHash

        /// <summary>
        /// Gets the hash.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static string GetHash(byte[] data)
        {
            // This is one implementation of the abstract class MD5.
            MD5 md5 = new MD5CryptoServiceProvider();

            return BitConverter.ToString(md5.ComputeHash(data)).Replace("-", String.Empty);
        }
開發者ID:kouweizhong,項目名稱:ajaxnet,代碼行數:12,代碼來源:MD5Helper.cs

示例4: GuardarAdministrativo

 public void GuardarAdministrativo(String username, String password)
 {
     MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
     byte[] data = System.Text.Encoding.ASCII.GetBytes(password);
     data = provider.ComputeHash(data);
     string md5 = string.Empty;
     for (int i = 0; i < data.Length; i++)
     {
         md5 += data[i].ToString("x2").ToLower();
     }
     password = md5;
     var sql = new StringBuilder();
     sql.AppendLine("insert into administradores (admin_username,admin_password) values (@username,@password)");
     var parametros = new List<SqlParameter>
         {
             new SqlParameter
                 {
                     ParameterName = "username",
                     SqlDbType =SqlDbType.VarChar,
                     SqlValue =  username
                 },
                 new SqlParameter
                     {
                     ParameterName = "password",
                     SqlDbType = SqlDbType.VarChar,
                     SqlValue = password
                 },
         };
     AccesoDatosSQL.Instance.Accesar.EjecutarConsultaSQL(sql.ToString(), parametros);
     if (AccesoDatosSQL.Instance.Accesar.HayError)
     {
         this.IsError = AccesoDatosSQL.Instance.Accesar.HayError;
         this.ErrorDescripcion = AccesoDatosSQL.Instance.Accesar.ErrorDescripcion;
     }
 }
開發者ID:LightProgrammingCompany,項目名稱:EtzJaimClinic,代碼行數:35,代碼來源:AdministradoresSQL.cs

示例5: EncryptString

        /// <summary>
        /// Returnes the string as MD5 hashing
        /// </summary>
        /// <param name="datastr"></param>
        /// <returns></returns>
        public static string EncryptString(string datastr)
        {
            HashAlgorithm mhash = new MD5CryptoServiceProvider();
              string res = string.Empty; // the returning result

              // Convert the original string to array of Bytes
              byte[] bytValue = Encoding.UTF8.GetBytes(datastr);

              // Compute the Hash, returns an array of Bytes
              byte[] bytHash = mhash.ComputeHash(bytValue);

              mhash.Clear();

              // convert the byte data to hex string values
              for (int i = 0; i < bytHash.Length; i++)
              {
            if (bytHash[i] < 16)
            {
              res += "0" + bytHash[i].ToString("x");
            }
            else
            {
              res += bytHash[i].ToString("x");
            }
              }

              return res;
        }
開發者ID:HydAu,項目名稱:sitecore8ecommerce,代碼行數:33,代碼來源:Hashing.cs

示例6: MD5Bytes

 /// <summary>
 /// MD5加密 返回純字節
 /// </summary>
 /// <param name="k"></param>
 /// <returns></returns>
 public static byte[] MD5Bytes(string sourceString)
 {
     MD5 md5 = new MD5CryptoServiceProvider();
     byte[] keyBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(sourceString));
     md5.Clear();
     return keyBytes;
 }
開發者ID:jameyK,項目名稱:Sophia.WeChat,代碼行數:12,代碼來源:MD5Encrypt.cs

示例7: Decrypt

        internal string Decrypt(string value)
        {
            MD5CryptoServiceProvider hashProvider = null;
            TripleDESCryptoServiceProvider provider = null;

            try
            {
                hashProvider = new MD5CryptoServiceProvider();
                var hashPassPhrase = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(passPhrase));

                provider = new TripleDESCryptoServiceProvider();
                provider.Key = hashPassPhrase;
                provider.Mode = CipherMode.ECB;
                provider.Padding = PaddingMode.PKCS7;

                var dataToEncrypt = Convert.FromBase64String(value);
                var decryptor = provider.CreateDecryptor();
                var results = decryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
                return Encoding.UTF8.GetString(results);

            }
            finally
            {
                if (provider != null) provider.Clear();
                if (hashProvider != null) hashProvider.Clear();
            }
        }
開發者ID:amido,項目名稱:Amido.PreProcessor,代碼行數:27,代碼來源:MD5.cs

示例8: GetMd5_16

 /// <summary>
 /// 16位加密
 /// </summary>
 /// <param name="ConvertString"></param>
 /// <returns></returns>
 public static string GetMd5_16(string ConvertString)
 {
     MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
     string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
     t2 = t2.Replace("-", "");
     return t2;
 }
開發者ID:nick121212,項目名稱:xima_desktop3,代碼行數:12,代碼來源:MD5Until.cs

示例9: CreateSign

        /// <summary>
        /// 創建簽名
        /// </summary>
        /// <param name="text">明文</param>
        /// <param name="merchantKey">商戶密鑰</param>
        /// <returns>簽名</returns>
        public static string CreateSign(string text, string merchantKey)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(text + "&Key=" + merchantKey));

            return BitConverter.ToString(hashBytes).Replace("-", "").ToUpper();
        }
開發者ID:PersonalTeam,項目名稱:cn.memeda.weixinapp,代碼行數:13,代碼來源:SecurityUtil.cs

示例10: Md5Encrypt

 /// <summary>
 /// 用MD5算法對字符串加密
 /// </summary>
 /// <param name="strOriginal"></param>
 /// <returns></returns>
 public static byte[] Md5Encrypt(string strOriginal)
 {
     var encoder = new UTF8Encoding();
     var md5Hasher = new MD5CryptoServiceProvider();
     byte[] bytePassword = md5Hasher.ComputeHash(encoder.GetBytes(strOriginal));
     return bytePassword;
 }
開發者ID:zhouyongtao,項目名稱:TicketHelper,代碼行數:12,代碼來源:MD5Encrypt.cs

示例11: Decrypt

        /// <summary>
        /// Decrypt the given string using the specified key.
        /// </summary>
        /// <param name="strEncrypted">The string to be decrypted.</param>
        /// <param name="key">The decryption key.</param>
        /// <returns>The decrypted string.</returns>
        /// <exception cref="Exception">Unexpected Exception</exception>
        public static string Decrypt(string strEncrypted, string key)
        {
            try
            {
                TripleDESCryptoServiceProvider decrypto = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider hash = new MD5CryptoServiceProvider();

                byte[] byteHash, byteBuff;

                string tempKey = key;

                byteHash = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(tempKey));

                hash = null;

                decrypto.Key = byteHash;
                decrypto.Mode = CipherMode.ECB; //CBC, CFB

                byteBuff = Convert.FromBase64String(strEncrypted);

                string strDecrypted = ASCIIEncoding.ASCII.GetString(decrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
                decrypto = null;

                return strDecrypted;
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message + ".\n Decryption Failed. Please start over..!");
            }
        }
開發者ID:sameesh-s,項目名稱:stego-app,代碼行數:37,代碼來源:Text.cs

示例12: AdminRegister

        public JsonResult AdminRegister()
        {
            if (user.getAll().Count() == 0)
            {
                if (Request["Password"] == null)
                {
                    return Json(new { info = false, message = "密碼不能為空" });
                }
                if (Request["UserName"] == null)
                {
                    return Json(new { info = false, message = "用戶名不能為空" });
                }
                User u = new User();
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] mdpass = md5.ComputeHash(System.Text.Encoding.Default.GetBytes("password" + Request["Password"]));
                u.Password = System.Text.Encoding.Default.GetString(mdpass);
                u.UserName = Request["UserName"];
                u.IsUse = true;
                u.Role_Id = 1;
                u.CreateTime = DateTime.Now;
                try
                {
                    user.addUser(u);
                    user.save();
                }
                catch
                {
                    return Json(new { info = false, message = "出錯了" });
                }

                return Json(new { info = true, message = "管理員賬戶創建成功" });
            }
            else return Json(new { info = false, message = "出錯了" });
        }
開發者ID:WangJingye,項目名稱:TaskManage,代碼行數:34,代碼來源:UserController.cs

示例13: parse_packet

        public static FilePiece parse_packet(byte[] packet)
        {
            if (packet.Length < 28){ // header isn't long enough
                return null;
            }
            Int32 data_length = System.BitConverter.ToInt32(packet, 0);
            if (data_length < 0){ // no data
                return null;
            }
            Int64 piece_number = System.BitConverter.ToInt64(packet, 4);
            if (piece_number < 0){ // can't have less than 0 piece number
                return null;
            }
            byte[] checksum = new byte[16];
            for(int i = 12; i < 28; i++){
                checksum[i] = packet[i];
            }
            System.ArraySegment<byte> data_segment = new System.ArraySegment<byte>(packet, 28, packet.Length-28);

            byte[] data = data_segment.Array;

            // do a checksum
            MD5 check = new MD5CryptoServiceProvider();
            byte[] sum = check.ComputeHash(data);
            if (sum.Equals(checksum)){
                return null; // data checksum doesn't match
            }

            FilePiece piece = new FilePiece(piece_number, data);

            return piece;
        }
開發者ID:windsurfer,項目名稱:C-Sharp-Multicast,代碼行數:32,代碼來源:FilePiece.cs

示例14: GameEncryption

        private byte[] xorData; // This table is used for encrypting the server->client stream

        #endregion Fields

        #region Constructors

        public GameEncryption(uint seed)
        {
            cipherTable = new byte[0x100];

            // Set up the crypt key
            byte[] key = new byte[16];
            key[0] = key[4] = key[8] = key[12] = (byte)((seed >> 24) & 0xff);
            key[1] = key[5] = key[9] = key[13] = (byte)((seed >> 16) & 0xff);
            key[2] = key[6] = key[10] = key[14] = (byte)((seed >> 8) & 0xff);
            key[3] = key[7] = key[11] = key[15] = (byte)(seed & 0xff);

            byte[] iv = new byte[0];
            engine = new TwofishEncryption(128, ref key, ref iv, CipherMode.ECB, TwofishBase.EncryptionDirection.Decrypting);

            // Initialize table
            for ( int i = 0; i < 256; ++i )
                cipherTable[i] = (byte)i;

            sendPos = 0;

            // We need to fill the table initially to calculate the MD5 hash of it
            refreshCipherTable();

            // Create a MD5 hash of the twofish crypt data and use it as a 16-byte xor table
            // for encrypting the server->client stream.
            MD5 md5 = new MD5CryptoServiceProvider();
            xorData = md5.ComputeHash(cipherTable);
        }
開發者ID:BackupTheBerlios,項目名稱:sunuo-svn,代碼行數:34,代碼來源:GameEncryption.cs

示例15: Decrypt

 public static string Decrypt(string cypherString, bool useHasing)
 {
     byte[] keyArray;
     byte[] toDecryptArray = Convert.FromBase64String(cypherString);
     string key = "uzma";
     if (useHasing)
     {
         MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
         keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
         hashmd.Clear();
     }
     else
     {
         keyArray = UTF8Encoding.UTF8.GetBytes(key);
     }
     TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
     tDes.Key = keyArray;
     tDes.Mode = CipherMode.ECB;
     tDes.Padding = PaddingMode.PKCS7;
     ICryptoTransform cTransform = tDes.CreateDecryptor();
     try
     {
         byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
         tDes.Clear();
         return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
開發者ID:uzmaha,項目名稱:Development-GitHub,代碼行數:31,代碼來源:CryptorEngine.cs


注:本文中的System.Security.Cryptography.MD5CryptoServiceProvider類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。