- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Caeser_method
{
class Program
{
static void Main(string[] args)
{
int n = 3;
Console.Write("Input string to encoding: ");
string input = Console.ReadLine();
Csr enc = new Csr(n,input);
// Csr dec;
enc.encrypt();
Console.WriteLine(enc);
// dec = new Csr(n, input);
enc.decrypt();
Console.WriteLine(enc);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Caeser_method
{
class Csr
{
public int n;
public string phraze,outputphr;
public Csr(int n, string phraze)
{
this.n = n;
this.phraze = phraze;
this.outputphr = "";
}
public void encrypt()
{
foreach (char c in this.phraze)
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
{
if (char.IsLetter(c)) this.outputphr += (char)(char.IsUpper(c) ?
(c + this.n > 'Z' ? ('A' + ((c -'Z' + (this.n - 1)))) : (c + this.n)) :
(c + this.n > 'z' ? ('a' + ((c - 'z' + (this.n - 1)))) : (c + this.n)));
else this.outputphr += c;
}
else
{
if (char.IsLetter(c)) this.outputphr += (char)(char.IsUpper(c) ?
(c + this.n > 'Я' ? ('А' + ((c - 'Я' + (this.n - 1)))) : (c + this.n)) :
(c + this.n > 'я' ? ('а' + ((c - 'я' + (this.n - 1)))) : (c + this.n)));
else this.outputphr += c;
}
}
public void decrypt()
{
this.phraze = this.outputphr;
this.outputphr = "";
foreach (char c in this.phraze)
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
{
if (char.IsLetter(c)) this.outputphr += (char)(char.IsUpper(c) ?
(c - this.n < 'A' ? ('Z' - (('A' - c + (this.n - 1)))) : (c - this.n)) :
(c - this.n < 'a' ? ('z' - (('a' - c + (this.n - 1)))) : (c - this.n)));
else this.outputphr += c;
}
else
{
if (char.IsLetter(c)) this.outputphr += (char)(char.IsUpper(c) ?
(c - this.n < 'А' ? ('Я' - (('А' - c + (this.n - 1)))) : (c - this.n)) :
(c - this.n < 'а' ? ('я' - (('а' - c + (this.n - 1)))) : (c - this.n)));
else this.outputphr += c;
}
}
public override string ToString()
{
return string.Format("Encoded string: {0}",this.outputphr);
}
}
}