-3

At First I don't know which one is outside the size. And as you can see I have put % not to be outside the array. This is my code, please don't blame me for any childish mistake. If you have tips for a more efficient encrypting method I am opened

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)
    {

    }
}
}

Please help me.

Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31
  • No details on the error? Where exactly did it occur ? Nothing ... – Suraj S Dec 24 '17 at 17:40
  • And do not post irrelevant code. – Steve Dec 24 '17 at 17:48
  • You are getting an out of bounds index for your result because it’s length is limited to 6, but use i as indexer whose upper bound is the length of charAArray. You get another out of bounds index if charAArray is longer than charBArray because you switched the order of operands on your modulo. Instead of using the length of each array as upper bound it’s using the current index position as upper bound which also goes up to charAArrays length. The `if (i != 0)` statement looks like it’s just there to work around an DivideByZeroException which is caused by your wrong order of operands. – ckuri Dec 25 '17 at 00:45

1 Answers1

0

If your strings are longer than 6 characters the following assignment will be out of bounds: result[i] = (char) ( ((int)charAArray[a.Length % i] ^ (int)charBArray[b.Length % i]) ) ; //error

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • As @SoronelHaetir says the result for example of 2^5 would be 7, which would be outside the bounds of `result[]`. String length doesn't matter. – Deolus Dec 24 '17 at 17:55