C# 索引在数组的界限之外

C# 索引在数组的界限之外,c#,arrays,C#,Arrays,一开始我不知道哪一个尺码不合适。正如您所看到的,我已将%设置为不在数组之外。这是我的密码,请不要因为任何幼稚的错误责备我。如果你有一个更有效的加密方法,我打开提示 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Syste

一开始我不知道哪一个尺码不合适。正如您所看到的,我已将%设置为不在数组之外。这是我的密码,请不要因为任何幼稚的错误责备我。如果你有一个更有效的加密方法,我打开提示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
 using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace encrypting
{
    public partial class Form1 : Form
    {
    string text;
    string key;

    public string calcXor(string a, string b)
    {
        char[] charAArray = a.ToCharArray();
        char[] charBArray = b.ToCharArray();
        char[] result = new char[6];
        int len = 0;




        for (int i = 0; i < a.Length; i++)
        {
            if (i != 0)
                result[i] = (char) ( ((int)charAArray[a.Length % i] ^ (int)charBArray[b.Length % i]) ) ; //error



        }

        return new string(result);
    }

    private void Crypt()
    {
        char[] a;
        int i, sizeoftext = text.Length,j=0;
        string somestring ="";
        string key = textBox2.Text;
        StringBuilder sb = new StringBuilder(somestring);
        for(i=0;i<sizeoftext-1;i++)
        {
            if (i == key.Length-1)
                j = 0;





        }
        somestring = calcXor(text, key);


        if (File.Exists(textBox3.Text)) ;
        File.Create(textBox3.Text).Close();

        System.IO.File.WriteAllText(textBox3.Text, somestring);



    }

    public Form1()
    {

        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {


             text = File.ReadAllText(textBox1.Text);

        Crypt();


    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        key = this.Text;
    }

    private void button2_Click(object sender, EventArgs e)
    {

            OpenFileDialog fd = new OpenFileDialog();

            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fd.FileName;
            }

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fd = new FolderBrowserDialog();
        if(fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            textBox3.Text = fd.SelectedPath;
            textBox3.Text = textBox3.Text +"\\" + textBox4.Text + ".txt";
        }
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
使用系统文本;
命名空间加密
{
公共部分类Form1:Form
{
字符串文本;
字符串键;
公共字符串计算器(字符串a、字符串b)
{
char[]chararray=a.ToCharArray();
char[]charbaray=b.ToCharArray();
字符[]结果=新字符[6];
int len=0;
for(int i=0;i对于(i=0;i如果字符串长度超过6个字符,则以下赋值将超出范围:

result[i]=(char)((int)chararray[a.Length%i]^(int)charBArray[b.Length%i]))
;//错误

没有关于错误的详细信息?它到底发生在哪里?什么都没有…并且不要发布无关的代码。您将获得结果的越界索引,因为它的长度限制为6,但使用i作为索引器,其上限是chararray的长度。如果chararray长于char,您将获得另一个越界索引因为您在模上切换了操作数的顺序。它不是使用每个数组的长度作为上限,而是使用当前索引位置作为上限,上限也会上升到数组长度。
if(i!=0)
语句似乎只是用来解决由操作数顺序错误引起的DivideByZeroException。正如@SoronelHaetir所说,例如2^5的结果将是7,这将超出
结果[]
的范围。字符串长度无关紧要。