Цезарь (дешифратор) - C#
Формулировка задачи:
Помогите исправить дешифратор цезаря
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void doWork()
{
string sum = "", s = "";
s = Console.ReadLine();
int key = 0;
key = Convert.ToInt32(Console.ReadLine());
string alf = "abcdefghijklmnopqrstuvwxyz";
int m = s.Length;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < alf.Length; j++)
{
if (s[i] == alf[j])
{
sum += alf[j+key];
}
}
}
Console.WriteLine(sum);
}
public static void doWork2()
{
string sum = "", s = "";
s = Console.ReadLine();
int key = 0;
string alf = "abcdefghijklmnopqrstuvwxyz";
int m = s.Length;
for (int g = 0; g < alf.Length; g++)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < alf.Length; j++)
{
if (s[i] == alf[j])
{
if (j - key < 0)
{
Console.WriteLine("\n Max value to shift :",key,"\n");
Console.ReadLine();
}
else
sum += alf[j - key];
}
}
}
key++;
Console.WriteLine("\n", sum, "\n");
}
}
static void Main(string[] args)
{
try
{
int a = Convert.ToInt32(Console.ReadLine());
switch (a)
{
case 1: doWork(); break;
case 2: doWork2(); break;
}
}
catch (Exception)
{
throw new Exception();
}
}
}
}Решение задачи: «Цезарь (дешифратор)»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication105
{
class Program
{
class CaesarCipher
{
public static string Alphabet { get; set; }
public static int Shift { private get; set; }
public static string Encryption(string text )
{
text = text.ToLower();
var res = new StringBuilder();
for (int i = 0; i < text.Length; i++)
for (int j = 0; j < Alphabet.Length; j++)
if (text[i] == Alphabet[j]) res.Append(Alphabet[(j + Shift) % Alphabet.Length]);
return res.ToString();
}
public static string Decryption(string crypt)
{
crypt = crypt.ToLower();
var res = new StringBuilder();
for (int i = 0; i < crypt.Length; i++)
for (int j = 0; j < Alphabet.Length; j++)
if (crypt[i] == Alphabet[j]) res.Append(Alphabet[(j - Shift + Alphabet.Length) % Alphabet.Length]);
return res.ToString();
}
}
static void Main(string[] args)
{
CaesarCipher.Alphabet = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
CaesarCipher.Shift = 3;
string text = "текст";
Console.WriteLine("Текст {0}", text);
string crypt = CaesarCipher.Decryption(text);
Console.WriteLine("Шифруем текст: {0}", crypt);
text = " ";
text = CaesarCipher.Encryption(crypt);
Console.WriteLine("Дешифруем текст: {0}", text);
CaesarCipher.Alphabet = "abcdefghijklmnopqrstuvwxyz";
text = "tekst";
Console.WriteLine("Текст {0}", text);
crypt = CaesarCipher.Decryption(text);
Console.WriteLine("Шифруем текст: {0}", crypt);
text = " ";
text = CaesarCipher.Encryption(crypt);
Console.WriteLine("Дешифруем текст: {0}", text);
Console.ReadKey();
}
}
}