2014-11-03 66 views
0

我用C#編寫了一個程序,假設時間需要多長時間才能在RSA中加密和解密,然後顯示時間。我將秒錶對象放入加密和解密方法中,而不是製作自己的方法。無論我嘗試過什麼,輸出始終都是00:00:00:00。我的秒錶不保留並顯示時間

public void EncryptFile(string inFile) 
{ 
    Stopwatch stopWatch = new Stopwatch(); 
    // Get the elapsed time as a TimeSpan value. 
    TimeSpan ts = stopWatch.Elapsed; 
    stopWatch.Start(); 

    // Create instance of Rijndael for 
    // symetric encryption of the data. 

    RijndaelManaged rjndl = new RijndaelManaged(); 
    rjndl.KeySize = 256; 
    rjndl.BlockSize = 256; 
    rjndl.Mode = CipherMode.CBC; 
    ICryptoTransform transform = rjndl.CreateEncryptor(); 

    // Use RSACryptoServiceProvider to 
    // enrypt the Rijndael key. 
    // rsa is previously instantiated: 
    // rsa = new RSACryptoServiceProvider(cspp); 
    byte[] keyEncrypted = rsa.Encrypt(rjndl.Key, false); 

    // Create byte arrays to contain 
    // the length values of the key and IV. 
    byte[] LenK = new byte[4]; 
    byte[] LenIV = new byte[4]; 

    int lKey = keyEncrypted.Length; 
    LenK = BitConverter.GetBytes(lKey); 
    int lIV = rjndl.IV.Length; 
    LenIV = BitConverter.GetBytes(lIV); 

    // Write the following to the FileStream 
    // for the encrypted file (outFs): 
    // - length of the key 
    // - length of the IV 
    // - ecrypted key 
    // - the IV 
    // - the encrypted cipher content 

    int startFileName = inFile.LastIndexOf("\\") + 1; 
    // Change the file's extension to ".enc" 
    string outFile = EncrFolder + 
     inFile.Substring(startFileName, 
     inFile.LastIndexOf(".") -  startFileName) + ".enc"; 

    using (FileStream outFs = new FileStream(outFile, FileMode.Create)) 
    { 
     outFs.Write(LenK, 0, 4); 
     outFs.Write(LenIV, 0, 4); 
     outFs.Write(keyEncrypted, 0, lKey); 
     outFs.Write(rjndl.IV, 0, lIV); 

     // Now write the cipher text using 
     // a CryptoStream for encrypting. 
     using (CryptoStream outStreamEncrypted = 
     new CryptoStream(outFs, transform, CryptoStreamMode.Write)) 
     { 
     int count = 0; 
     int offset = 0; 

     // blockSizeBytes can be any arbitrary size. 
     int blockSizeBytes = rjndl.BlockSize/8; 
     byte[] data = new byte[blockSizeBytes]; 
     int bytesRead = 0; 

     using (FileStream inFs = new FileStream(inFile, FileMode.Open)) 
     { 
      do 
      { 
       count = inFs.Read(data, 0, blockSizeBytes); 
       offset += count; 
       outStreamEncrypted.Write(data, 0, count); 
       bytesRead += blockSizeBytes; 
      } while (count > 0); 

      inFs.Close(); 
     } 
     outStreamEncrypted.FlushFinalBlock(); 
     stopWatch.Stop(); 
     // Format and display the TimeSpan value. 
     string elapsedTime = string.Format("{0:00}:{1:00}:{2:00}.{3:00}", 
      ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds/10); 
     label3.Text = "Elapsed time " + elapsedTime; 
     outStreamEncrypted.Close(); 
     } 
     outFs.Close(); 
    } 
} 
+2

注意,這個問題實際上不是任何與加密,因此不應該被標記接着就,隨即;還有很多代碼與發佈前可能已刪除的時間無關。 – Blorgbeard 2014-11-03 04:12:43

回答

1

看起來你正在使用的變量TS在string.Format方法,但它並不像它無時不設置爲stopWatch.ElapsedTime調用stopWatch.Stop()後。

1

TimeSpan是一種值類型,您不能只分配TimeSpan ts = stopWatch.Elapsed;並期望它發生變化。使用stopWatch.Elapsed,而不是將它分配給一個卡住的變量。

4

你記錄經過的時間開始計時之前,(所以這顯然是零):

TimeSpan ts = stopWatch.Elapsed; 
stopWatch.Start(); 

然後,您可以參考ts你停止計時後,但你永遠不更新的ts值。

stopWatch.Stop(); 
// Format and display the TimeSpan value. 
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", 
    ts.Hours, ts.Minutes, ts.Seconds, 
    ts.Milliseconds/10); 

相反,你需要檢查所經過的時間後停止計時 - 在Stop後移動的申報和ts初始化到:

stopWatch.Stop(); 
// Format and display the TimeSpan value. 
TimeSpan ts = stopWatch.Elapsed; 
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", 
    ts.Hours, ts.Minutes, ts.Seconds, 
    ts.Milliseconds/10); 
1

以經過時間停止計時器之後。

stopWatch.Start() 
// Your code Here 
stopWatch.Stop() 
TimeSpan ts = stopWatch.Elapsed 

,並可以使用

ts.Hours, 
ts.Minutes, 
ts.Seconds, 
ts.Milliseconds 
ts.Ticks 

時,分,秒,毫秒和蜱