For循环具有意外的副作用

时间:2015-09-26 19:35:07

标签: c# .net loops debugging

所以我一直在C#中编写一个小字节密码,一切顺利,直到我尝试做一些for循环来测试运行时性能。事情开始变得非常奇怪。请允许我告诉你,而不是试图解释它:

首先,这是工作代码(for循环注释掉):

using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DreamforceFramework.Framework.Cryptography;

namespace TestingApp
{
    static class Program
    {
        static void Main(string[] args)
        {

            string myData = "This is a test.";
            byte[] myDataEncrypted;
            string myDecryptedData = null;
            Stopwatch watch = new Stopwatch();
            Console.WriteLine("Warming up for Encryption...");
            //for (int i = 0; i < 20; i++)
            //{
            //    // Warm up the algorithm for a proper speed benchmark.
            //    myDataEncrypted = DreamforceByteCipher.Encrypt(myData, "Dreamforce");
            //}
            watch.Start();
            myDataEncrypted = DreamforceByteCipher.Encrypt(myData, "Dreamforce");
            watch.Stop();
            Console.WriteLine("Encryption Time: " + watch.Elapsed);
            Console.WriteLine("Warming up for Decryption...");
            //for (int i = 0; i < 20; i++)
            //{
            //    // Warm up the algorithm for a proper speed benchmark.
            //    myDecryptedData = DreamforceByteCipher.Decrypt(myDataEncrypted, "Dreamforce");
            //}
            watch.Reset();
            watch.Start();
            myDecryptedData = DreamforceByteCipher.Decrypt(myDataEncrypted, "Dreamforce");
            watch.Stop();
            Console.WriteLine("Decryption Time: " + watch.Elapsed);
            Console.WriteLine(myDecryptedData);
            Console.Read();
        }
    }
}

和我的ByteCipher(在最初发生错误之后我高度简化了它,以试图查明问题):

using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using DreamforceFramework.Framework.Utilities;

namespace DreamforceFramework.Framework.Cryptography
{
    /// <summary>
    /// DreamforceByteCipher
    /// Gordon Kyle Wallace, "Krythic"
    /// Copyright (C) 2015 Gordon Kyle Wallace, "Krythic" - All Rights Reserved
    /// </summary>
    public static class DreamforceByteCipher
    {

        public static byte[] Encrypt(string data, string password)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            string passwordHash = DreamforceHashing.GenerateSHA256(password);
            byte[] hashedPasswordBytes = Encoding.ASCII.GetBytes(passwordHash);
            int passwordShiftIndex = 0;
            bool twistPath = false;
            for (int i = 0; i < bytes.Length; i++)
            {
                int shift = hashedPasswordBytes[passwordShiftIndex];
                bytes[i] = twistPath
                    ? (byte)(
                        (data[i] + (shift * i)))
                    : (byte)(
                        (data[i] - (shift * i)));
                passwordShiftIndex = (passwordShiftIndex + 1) % 64;
                twistPath = !twistPath;
            }
            return bytes;
        }

        /// <summary>
        /// Decrypts a byte array back into a string.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string Decrypt(byte[] data, string password)
        {
            string passwordHash = DreamforceHashing.GenerateSHA256(password);
            byte[] hashedPasswordBytes = Encoding.UTF8.GetBytes(passwordHash);
            int passwordShiftIndex = 0;
            bool twistPath = false;
            for (int i = 0; i < data.Length; i++)
            {
                int shift = hashedPasswordBytes[passwordShiftIndex];
                data[i] = twistPath
                    ? (byte)(
                        (data[i] - (shift * i)))
                    : (byte)(
                        (data[i] + (shift * i)));
                passwordShiftIndex = (passwordShiftIndex + 1) % 64;
                twistPath = !twistPath;
            }
            return Encoding.ASCII.GetString(data);
        }
    }
}

将for循环注释掉,这是我得到的输出:

enter image description here

最后一行显示所有内容都已成功解密。

现在......这就是事情变得奇怪的地方。如果取消注释for循环并运行程序,则输出结果为:

enter image description here

解密不起作用。这绝对没有意义,因为保存解密数据的变量应该每次都被重写。我是否在C#/ .NET中遇到导致这种奇怪行为的错误?

一个简单的解决方案: http://pastebin.com/M3xa9yQK

1 个答案:

答案 0 :(得分:12)

您的a3方法会修改value输入数组。因此,在数据不再加密之前,您只能使用任何给定的输入字节数组一次调用Decrypt。以一个简单的控制台应用程序为例:

data

所以,回答你的问题,不。您还没有在.NET中发现错误。您在代码中发现了一个非常简单的错误。