For循环具有意外的副作用

Kry*_*hic 0 .net c# debugging loops

所以我一直在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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和我的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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

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

在此输入图像描述

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

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

Jus*_*ner 12

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

class Program
{
    public static void Main(string[] args)
    {
        var arr = new byte[] { 10 };
        Console.WriteLine(arr[0]); // prints 10
        DoSomething(arr);
        Console.WriteLine(arr[0]); // prints 11
    }

    private static void DoSomething(byte[] arr)
    {
        arr[0] = 11;
    }
}
Run Code Online (Sandbox Code Playgroud)

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