简体   繁体   English

在C#中使用计时器控件进行加密时间

[英]encryption time using timer control in C#

My problem that I want to calculate the encryption time in triple DES. 我想在三重DES中计算加密时间的问题。 I put a timer control in the form like shown below but the problem that it continue to count even the encryption is end. 我将计时器控件的格式如下所示,但是即使加密结束,它也继续计数的问题。 How can I stop the timer control in the way he counts the encryption time? 如何以计时器计算加密时间的方式停止计时器控件?

在此处输入图片说明

        timer1.Enabled = true;
        timer1.Start();


          //3DES ENCRYPTION 

        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        UTF8Encoding utf8 = new UTF8Encoding();
        TripleDESCryptoServiceProvider tDES = new TripleDESCryptoServiceProvider();
        tDES.Key = md5.ComputeHash(utf8.GetBytes(textBox1.Text));
        //to decrypt the data we must use these two property 
        tDES.Mode = CipherMode.ECB;
        tDES.Padding = PaddingMode.PKCS7;

        //this is interface to encrypt our data 
        ICryptoTransform trans = tDES.CreateEncryptor();
        encrypted = trans.TransformFinalBlock(utf8.GetBytes(textBox2.Text), 0, utf8.GetBytes(textBox2.Text).Length);
        textBox3.Text = BitConverter.ToString(encrypted);

You shouldn't use a Timer for keeping track of how long something takes. 您不应该使用Timer来跟踪某物需要花费多长时间。 Timer is set up to invoke code on an interval (say; updating that text box in real time). 设置了Timer以按一定间隔调用代码(例如,实时更新该文本框)。

Stopwatch on the other hand is specifically designed for doing highly accurate timing of operations. 另一方面, Stopwatch是专门为执行高度精确的计时而设计的。 You should use that in this case: 在这种情况下,您应该使用它:

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//Operations
stopwatch.Stop();

TimeSpan duration = stopwatch.Elapsed;
//Do something with duration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM