System.IndexOutOfRangeException: "Индекс находился вне границ массива."
using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace Project10
{
class Class1
{
public static void Encrypte()
{
int p = 0;
int d = 0;
StreamReader rdr = new StreamReader(@"\Users\Desktop\1.txt");
string text = Regex.Replace(rdr.ReadToEnd().Trim(), "\\s+", " ");
StreamReader message = new StreamReader(@"\Users\Desktop\message.txt");
string writePath = @"\Users\Desktop\2.txt";
string value = message.ReadToEnd();
int sum = 0;
byte[] asciiBytes = Encoding.GetEncoding(1251).GetBytes(value);
string letter = "";
string newText = "";
int j = 0;
for (int i = 0; i < text.Length; i++)
{
letter = Convert.ToString(text[i]);
if (letter == " ")
{
sum++;
if (p == 0)
{
d = asciiBytes[j]; //тут вот
j++;
}
if (d % 2 == 1)
{
letter = " ";
d /= 2;
p++;
}
else
{
letter = " ";
d /= 2;
p++;
}
if (p == 8) p = 0;
}
newText += letter;
try
{
using (StreamWriter sw = new StreamWriter(writePath, false, System.Text.Encoding.Default))
{
sw.Write(newText);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
rdr.Close();
Console.WriteLine("Пробелов: " + sum);
Console.WriteLine("Зашифруется " + sum / 8 + " символов.");
}
public static void Decrypte()
{
StreamReader rdr = new StreamReader(@"/Users/Desktop/2.txt");
string text = rdr.ReadToEnd();
char[] array = text.ToCharArray();
string result = "";
for (int i = 1; i < array.Length; i++)
{
if (array[i] == ' ' & array[i] != array[i - 1])
{
result += "0";
}
else if (array[i] == ' ' & array[i] == array[i - 1])
{
result = result.Remove(result.Length - 1);
result += "1";
}
}
Console.WriteLine();
Console.WriteLine("Зашифрованое сообщение:");
string neww = "";
string message = "";
for (int n = 0; n < result.Length; n++)
{
for (int i = 0; i < 8; i++)
{
neww += result[i];
}
string output = new string(neww.ToCharArray().Reverse().ToArray());
int dec = Convert.ToInt32(output, 2);
byte[] two = new byte[1];
two[0] = (byte)dec;
String decodedString = Encoding.GetEncoding(1251).GetString(two);
message += decodedString;
result = result.Remove(0, 8);
neww = "";
}
Console.WriteLine(message);
}
public static void Main(string[] args)
{
Encrypte();
Decrypte();
}
}
}