精華區beta CSSE 關於我們 聯絡資訊
※ [本文轉錄自 C_Sharp 看板] 作者: ccc316123 (ccc) 看板: C_Sharp 標題: [問題] ICryptoTransform 使用上的問題 時間: Mon Sep 28 01:10:21 2009 上課時 老師丟了兩個ICryptoTransform的程式範例給我 要我查出兩種寫法差異在哪? 執行上速度快慢的比較 ------------------------------------------------------------------------ 範例1 : static byte[] DESEncrypt(String strInput, byte[] byteKey, byte[] byteIV) { DES des = null; ICryptoTransform ict = null; MemoryStream ms = null; CryptoStream cs = null; StreamWriter sw = null; byte[] byteResult = null; try { // Create a new DES object to generate a key // and initialization vector (IV). des = DES.Create(); des.Key = byteKey; des.IV = byteIV; ict = des.CreateEncryptor(); ms = new MemoryStream(); cs = new CryptoStream(ms, ict, CryptoStreamMode.Write); sw = new StreamWriter(cs); sw.Write(strInput); sw.Close(); cs.Close(); byteResult = ms.ToArray(); ms.Close(); return byteResult; } catch (Exception e) { throw e; } finally { if (sw != null) sw.Close(); if (cs != null) cs.Close(); if (ms != null) ms.Close(); } } ------------------------------------------------------------------------ 範例2 : DES des = null; public DesSample() { des = new DESCryptoServiceProvider(); } public string Encrypt(string data, byte[] bKey) { try { byte[] bData = Encoding.UTF8.GetBytes(data); des.Key = bKey; ICryptoTransform iCryptoTransform = des.CreateEncryptor(); byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length); return Convert.ToBase64String(bResult); } catch { throw; } } ------------------------------------------------------------------------ 小弟我查了很久 一直找不到資料 聽老師說 Microsoft宣稱第一種寫法是最快的 為什麼勒?? 還請各位大大指教一下 或告訴我哪裡可以找到相關資料>< -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.44.198.82 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.44.198.82