· 4 years ago · Apr 16, 2021, 08:08 PM
1----------------------------------------- ШИФР МЕТОДОМ ПЕРЕСТАНОВКИ (kotlin) -----------------------------------------
2fun main(vararg args: String) {
3 var cryptoInput: List<String> = listOf("А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ь", "Ы", "Ъ", "Э", "Ю", "Я")
4 var cryptoOutput: List<String> = listOf("Я", "Ю", "Э", "Ъ", "Ы", "Ь", "Щ", "Ш", "Ч", "Ц", "Х", "Ф", "У", "Т", "С", "Р", "О", "П", "Н", "М", "Л", "К", "Й", "И", "З", "Ж", "Ё", "Е", "Д", "Г", "В", "Б", "А")
5 var str: String = readLine()!!
6 var outputStr: String = crypting(str, cryptoInput, cryptoOutput)
7 print(outputStr)
8 println()
9 print(unCrypting(outputStr, cryptoInput, cryptoOutput))
10}
11
12fun crypting(str: String, cInput: List<String>, cOutput: List<String>): String {
13 var strOutput: String = ""
14 for(letter in str) {
15 for (i in 0..cInput.size - 1) {
16 if (letter.toLowerCase() == cInput[i].single().toLowerCase()) {
17 strOutput += cOutput.get(i)
18 }
19 }
20 }
21 return strOutput
22}
23
24fun unCrypting(str: String, cInput: List<String>, cOutput: List<String>): String {
25 var strOutput: String = ""
26 for(letter in str) {
27 for (i in 0..cOutput.size - 1) {
28 if (letter.toLowerCase() == cOutput[i].single().toLowerCase()) {
29 strOutput += cInput.get(i)
30 }
31 }
32 }
33 return strOutput
34}
35
36----------------------------------------- ШИФР ЦЕЗАРЯ (C#) -----------------------------------------
37using System;
38class Program
39{
40 static void Main(string[] args)
41 {
42 Caesar caesar = new Caesar();
43 Console.Write("Введите текст: ");
44 string message = Console.ReadLine();
45 Console.Write("Введите ключ: ");
46 int secretKey = Convert.ToInt32(Console.ReadLine());
47 string encryptedText = cipher.Encrypt(message, secretKey);
48 Console.WriteLine("Зашифрованное сообщение: {0}", encryptedText);
49 Console.WriteLine("Расшифрованное сообщение: {0}", caesar.Decrypt(encryptedText, secretKey));
50 Console.ReadLine();
51 }
52}
53public class Caesar
54{
55 const string alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
56 private string CodeEncode(string text, int k)
57 {
58 string fullAlfabet = alfabet + alfabet.ToLower();
59 int letterQty = fullAlfabet.Length;
60 string retVal;
61 for (int i = 0; i < text.Length; i++)
62 {
63 string c = text[i];
64 var index = fullAlfabet.IndexOf(c);
65 if (index < 0)
66 {
67 retVal += c.ToString();
68 }
69 else
70 {
71 var codeIndex = (letterQty + index + k) % letterQty;
72 retVal += fullAlfabet[codeIndex];
73 }
74 }
75 return retVal;
76 }
77
78 //шифрование текста
79 public string Encrypt(string plainMessage, int key)
80 => CodeEncode(plainMessage, key);
81
82 //дешифрование текста
83 public string Decrypt(string encryptedMessage, int key)
84 => CodeEncode(encryptedMessage, -key);
85}
86
87----------------------------------------- ШИФР МЕТОДОМ ГАММИРОВАНИЯ (C#) -----------------------------------------
88class Program
89{
90 static void Main(string[] args)
91 {
92 Console.Write("Введите текст: ");
93 string text = Console.ReadLine();
94 Console.Write("Введите ключ: ");
95 string key = Convert.ToInt32(Console.ReadLine());
96
97 string crypt = Xor.Encryption(text, key); //шифрование. вернет "ыкык", если текст будет «мама» а ключ «ок»
98 string decrypt = Xor.Decryption(crypt, key); // дешифрование. вернет "мама"
99
100 Console.WriteLine(crypt + “ ” + decrypt)
101 }
102}
103
104static class Xor
105 {
106 static private string alf = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя 0123456789";
107 static private int k, x, z ;
108 static private string res;
109
110 static public string Encryption(string source, string key)
111 {
112 res = string.Empty;
113
114 while (key.Length < source.Length)
115 {
116 key += key;
117 if (key.Length > source.Length) key = key.Remove(source.Length);
118 }
119 for (int i = 0; i < source.Length; i++)
120 {
121 for (int id = 0; id < alf.Length; id++)
122 {
123 if (key[i] == alf[id]) k = id;
124 if (source[i] == alf[id]) x = id;
125 z = (x + k) % alf.Length;
126 }
127 res += alf[z];
128 }
129 return res;
130 }
131
132 static public string Decryption(string source, string key)
133 {
134 res = string.Empty;
135
136 while (key.Length < source.Length)
137 {
138 key += key;
139 if (key.Length > source.Length) key = key.Remove(source.Length);
140 }
141 for (int i = 0; i < source.Length; i++)
142 {
143 for (int id = 0; id < alf.Length; id++)
144 {
145 if (key[i] == alf[id]) k = id;
146 if (source[i] == alf[id]) x = id;
147 z = ((source[i] - key[i]) + alf.Length) % alf.Length;
148 }
149 res += alf[z];
150 }
151 return res;
152 }
153 }
154
155