c# 計算字符串和文件的MD5值的方法


快速使用Romanysoft LAB的技術實現 HTML 開發Mac OS App,並銷售到蘋果應用商店中。
 
HTML開發Mac OS App 視頻教程》
 
官方QQ群:(申請加入,說是我推薦的
  • App實踐出真知 434558944       App實踐出真知
  • App學習交流 452180823          App實踐出真知
 
Step1:引入庫
1 using System.Security.Cryptography; 2 using System.IO;

 

Step2: 計算字符串的Md5值    

 1         static public string GetMD5WithString(string sDataIn)  2  {  3             string str = "";  4             byte[] data = Encoding.GetEncoding("utf-8").GetBytes(str);  5             MD5 md5 = new MD5CryptoServiceProvider();  6             byte[] bytes = md5.ComputeHash(data);  7             for (int i = 0; i < bytes.Length; i++)  8  {  9                 str += bytes[i].ToString("x2"); 10  } 11             return str; 12         }

 

Step3:計算文件的Md5值

 

1         static public string GetMD5WithFilePath(string filePath) 2  { 3             FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 4             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 5             byte[] hash_byte = md5.ComputeHash(file); 6             string str = System.BitConverter.ToString(hash_byte); 7             str = str.Replace("-", ""); 8             return str; 9         }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM