Зашифровать-расшифровать текст с помощью шифра Цезаря - C#
Формулировка задачи:
Задание такое - текст из файла Прізвище1.txt зашифровать в Прізвище2.txt, а потом дешифровать из Прізвище2.txt в Прізвище3.txt.
Люди, подскажите, почему код вроде правильный написанный, но вот дешифрование не работает. То есть зашифрованный текст из Прізвище2.txt в Прізвище3.txt тупо записывается без расшифрования.
string small = "abcdefghijklmnopqrstuvwxyz", big = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string[] imoN = new string[26];
string[] imoZ = new string[26];
int x, q = 0, key = 0;
Console.WriteLine("Кодування файлу методом Цезаря...\n");
StreamReader s = new StreamReader(File.Open(@"e:\Прізвище1.txt", FileMode.Open));
string c = s.ReadToEnd();
StreamWriter sw = new StreamWriter(@"e:\Прізвище2.txt", true, Encoding.Default);
for (int j = 0; j < c.Length; j++)
{
if (c[j] == ' ')
sw.Write(string.Format(" "));
if (c[j] == '\n')
sw.Write(string.Format("\n"));
for (int i = 0; i <= 25; i++)
{
x = (i + 5) % 26;
if (c[j] == small[i])
sw.Write(string.Format(@"{0}", small[x]));
if (c[j] == big[i])
sw.Write(string.Format(@"{0}", big[x]));
}
}
for (char ch = 'a', ch1 = 'A'; ch <= 'z' || ch1 <= 'Z'; ch++, ch1++)
{
float j = 0.0f;
for (int i = 0; i < c.Length; i++)
{
if (c[i] == ch || c[i] == ch1)
++j;
}
imoN[q] = Convert.ToString(j / c.Length);
++q;
}
sw.Close();
StreamReader cs = new StreamReader(File.Open(@"e:\Прізвище2.txt", FileMode.Open));
string ss = cs.ReadToEnd();
q = 0;
for (char ch = 'a', ch1 = 'A'; ch <= 'z' || ch1 <= 'Z'; ch++, ch1++)
{
float j = 0.0f;
for (int i = 0; i < ss.Length; i++)
{
if (ss[i] == ch || ss[i] == ch1)
++j;
}
imoZ[q] = Convert.ToString(j / c.Length);
++q;
}
for (int r = 0; r < imoZ.Length; r++)
{
if (imoN[25] == imoZ[r])
{
for (int i = 0; i <= 25; i++)
{
if (small[i] == small[r])
key = (26 + i - 25) % 26;
}
}
}
Console.Write("Key={0}\n", key);
cs.Close();
StreamReader sd = new StreamReader(File.Open(@"e:\Прізвище2.txt", FileMode.Open));
string cd = sd.ReadToEnd();
StreamWriter sq = new StreamWriter(@"e:\Прізвище3.txt", true, Encoding.Default);
for (int j = 0; j < cd.Length; j++)
{
if (cd[j] == ' ')
sq.Write(string.Format(" "));
if (cd[j] == '\n')
sq.Write(string.Format("\n"));
for (int i = 0; i <= 25; i++)
{
x = (i + 26 - key) % 26;
if (cd[j] == small[i])
sq.Write(string.Format(@"{0}", small[x]));
if (cd[j] == big[i])
sq.Write(string.Format(@"{0}", big[x]));
}
}
sq.Close();
Console.WriteLine("\nФайл для кодування: e:\\Прізвище1.txt\nЗакодований файл: e:\\Прізвище2.txt\nРозшифрований файл: e:\\Прізвище3.txt");
Console.Read();Решение задачи: «Зашифровать-расшифровать текст с помощью шифра Цезаря»
textual
Листинг программы
string small = "abcdefghijklmnopqrstuvwxyz", big = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string[] imoN = new string[26];
string[] imoZ = new string[26];
int x, q = 0, key = 0;
Console.WriteLine("Кодування файлу методом Цезаря...\n");
StreamReader s = new StreamReader(File.Open(@"e:\Прізвище1.txt", FileMode.Open));
string c = s.ReadToEnd();
StreamWriter sw = new StreamWriter(@"e:\Прізвище2.txt", true, Encoding.Default);
for (int j = 0; j < c.Length; j++)
{
if (c[j] == ' ')
sw.Write(string.Format(" "));
if (c[j] == '\n')
sw.Write(string.Format("\n"));
for (int i = 0; i <= 25; i++)
{
x = (i + 5) % 26;
if (c[j] == small[i])
sw.Write(string.Format(@"{0}", small[x]));
if (c[j] == big[i])
sw.Write(string.Format(@"{0}", big[x]));
}
}
for (char ch = 'a', ch1 = 'A'; ch <= 'z' || ch1 <= 'Z'; ch++, ch1++)
{
float j = 0.0f;
for (int i = 0; i < c.Length; i++)
{
if (c[i] == ch || c[i] == ch1)
++j;
}
imoN[q] = Convert.ToString(j / c.Length);
++q;
}
sw.Close();
StreamReader cs = new StreamReader(File.Open(@"e:\Прізвище2.txt", FileMode.Open));
string ss = cs.ReadToEnd();
q = 0;
for (char ch = 'a', ch1 = 'A'; ch <= 'z' || ch1 <= 'Z'; ch++, ch1++)
{
float j = 0.0f;
for (int i = 0; i < ss.Length; i++)
{
if (ss[i] == ch || ss[i] == ch1)
++j;
}
imoZ[q] = Convert.ToString(j / c.Length);
++q;
}
for (int r = 0; r < imoZ.Length; r++)
{
if (imoN[25] == imoZ[r])
{
for (int i = 0; i <= 25; i++)
{
if (small[i] == small[r])
key = (26 + i - 25) % 26;
}
}
}
Console.Write("Key={0}\n", key);
cs.Close();
StreamReader sd = new StreamReader(File.Open(@"e:\Прізвище2.txt", FileMode.Open));
string cd = sd.ReadToEnd();
for (int j = 0; j < cd.Length; j++)
{
if (cd[j] == ' ')
sq.Write(string.Format(" "));
if (cd[j] == '\n')
sq.Write(string.Format("\n"));
for (int i = 0; i <= 25; i++)
{
x = (i + 26 - key) % 26;
if (cd[j] == small[i])
sq.Write(string.Format(@"{0}", small[x]));
if (cd[j] == big[i])
sq.Write(string.Format(@"{0}", big[x]));
}
}
StreamWriter sq = new StreamWriter(@"e:\Прізвище3.txt", true, Encoding.Default);
sq.Close();
Console.WriteLine("\nФайл для кодування: e:\\Прізвище1.txt\nЗакодований файл: e:\\Прізвище2.txt\nРозшифрований файл: e:\\Прізвище3.txt");
Console.Read();