/// <summary> /// MD5加密,和动网上的16/32位MD5加密结果相同 /// </summary> /// <param name="strSource">待加密字串</param> /// <param name="encryptType">16或32值之一,其它则采用.net默认MD5加密算法</param> /// <returns>加密后的字串</returns> public string MD5Encrypt(string strSource, MD5EncryptEnum encryptEnum = MD5EncryptEnum.Default) { byte[] bytes = Encoding.ASCII.GetBytes(strSource); byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm) System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes); StringBuilder sb = new StringBuilder(); switch (encryptEnum) { case MD5EncryptEnum.Short: for (int i = 4; i < 12; i++) { sb.Append(hashValue[i].ToString("x2")); } break; case MD5EncryptEnum.Long: for (int i = 0; i < 16; i++) { sb.Append(hashValue[i].ToString("x2")); } break; default: for (int i = 0; i < hashValue.Length; i++) { sb.Append(hashValue[i].ToString("x2")); } break; } return(sb.ToString()); }
private string MD5Encrypt(byte[] bytes, MD5EncryptEnum encryptEnum) { StringBuilder stringBuilder = new StringBuilder(); switch (encryptEnum) { case MD5EncryptEnum.Short: for (int j = 4; j < 12; j++) { stringBuilder.Append(bytes[j].ToString("x2")); } break; case MD5EncryptEnum.Long: for (int k = 0; k < 16; k++) { stringBuilder.Append(bytes[k].ToString("x2")); } break; default: for (int i = 0; i < bytes.Length; i++) { stringBuilder.Append(bytes[i].ToString("x2")); } break; } return(stringBuilder.ToString()); }
public string MD5Encrypt(string strSource, MD5EncryptEnum encryptEnum = MD5EncryptEnum.Default) { byte[] bytes = Encoding.ASCII.GetBytes(strSource); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] arrays = md5.ComputeHash(bytes); md5.Clear(); return(MD5Encrypt(arrays, encryptEnum)); }
public string MD5EncryptFile(string filePath, MD5EncryptEnum encryptEnum = MD5EncryptEnum.Default) { using (FileStream stream = File.Open(filePath, FileMode.Open)) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] b = md5.ComputeHash(stream); md5.Clear(); return(MD5Encrypt(b, encryptEnum)); } }