Криптографията през вековете – от глаголицата до съвременните компютри

https://www.canva.com/design/DAFzOhw9KDg/7XVttzS2mxhA1Fwj9S1Rjw/edit

https://www.youtube.com/watch?v=ELHbjiTx8ks

Console.WriteLine("=== ШИФЪР НА ЦЕЗАР ===");

string alphabet = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ";

Console.Write("Въведете дума за кодиране: ");
string text = Console.ReadLine().ToUpper();

int key = 3;
string encoded = "";

Console.Clear();

for (int i = 0; i < text.Length; i++)
{
    char c = text[i];
    int index = alphabet.IndexOf(c);

    if (index != -1)
    {
        int newIndex = (index + key) % alphabet.Length;
        encoded += alphabet[newIndex];
    }
    else
    {
        encoded += c;
    }
}

Console.WriteLine("Кодираната дума е: " + encoded);

string guess = "";

while (guess != text)
{
    Console.Write("Каква е била оригиналната дума? ");
    guess = Console.ReadLine().ToUpper();

    if (guess == text)
        Console.WriteLine("Браво! Познахте!");
    else
        Console.WriteLine("Грешно! Опитайте пак.");
}

string decoded = "";

for (int i = 0; i < encoded.Length; i++)
{
    char c = encoded[i];
    int index = alphabet.IndexOf(c);

    if (index != -1)
    {
        int newIndex = (index - key + alphabet.Length) % alphabet.Length;
        decoded += alphabet[newIndex];
    }
    else
    {
        decoded += c;
    }
}

Console.WriteLine("Разкодирана дума: " + decoded);
string alphabet = "абвгдежзийклмнопрстуфхцшщъьюя";

Console.Write("Въведи текст: ");
string text = Console.ReadLine().ToLower();

Console.Write("Въведи ключ: ");
string key = Console.ReadLine().ToLower();

string cipher = "";

for (int i = 0; i < text.Length; i++)
{
    char output = text[i];
    char p = text[i];
    if (alphabet.IndexOf(p) != -1)
    {
        char k = key[i % key.Length];

        int pIndex = alphabet.IndexOf(p);
        int kIndex = alphabet.IndexOf(k);

        int cIndex = (pIndex + kIndex) % alphabet.Length;
        output = alphabet[cIndex];
    }


    cipher += output;

}
Console.WriteLine("Кодирана дума (Виженер): " + cipher);

Console.Clear();
Console.WriteLine(key);
Console.WriteLine("Вашата кодирана дума: " + cipher);
//string guess = Console.ReadLine();
Console.Write("Въведи ключ за декодиране: ");
string key2 = Console.ReadLine().ToLower();
string decoded = "";

for (int i = 0; i < cipher.Length; i++)
{
    char output2 = text[i];
    char c = cipher[i];
    if (alphabet.IndexOf(c) != -1)
    {
        char k = key[i % key.Length];

        int cIndex = alphabet.IndexOf(c);
        int kIndex = alphabet.IndexOf(k);

        int pIndex = (cIndex - kIndex + alphabet.Length) % alphabet.Length;
        output2 = alphabet[pIndex];
    }

    decoded += output2;
}
Console.WriteLine("Декодираната дума: " + decoded);

using Enigma_fixed;
using System;
using System.Drawing;
Rotor rotor1 = new Rotor(„ФЖАУТКПШМЗОБЧГНЦЙЮЯСВРЛДХИЕЩЪЬ“);
Rotor rotor2 = new Rotor(„ЮЛЩЗТЦПЧХЖАБВГДЕИЙКМНОРСУФШЪЬЯ“);
Rotor rotor3 = new Rotor(„ШАЬРЙФНЦЕДЯЛЗЪУМГПИХОЧБТЖСКВЮЩ“);
Rotor[] rotors = { rotor1, rotor2, rotor3 };
bool usePlugboard = true;
//string reflector = „ЯЮЬЪЩШЧЦХФУТСРПОНМЛКЙИЗЖЕДГВБА“;
Dictionary PlugBoard = new Dictionary(){
{‘Я’,’Б’},{‘Б’,’Я’},{‘Ь’,’И’},{‘И’,’Ь’},{‘Щ’,’Л’},{‘Л’,’Щ’},{‘Ч’,’Д’},{‘Д’,’Ч’},{‘Х’,’Р’},{‘Р’,’Х’},{‘У’,’П’},{‘П’,’У’},{‘С’,’Е’},{‘Е’,’С’},{‘Т’,’Н’},{‘Н’,’Т’},{‘О’,’Ъ’},{‘Ъ’,’О’},{‘Ш’,’Й’},{‘Й’,’Ш’},{‘К’,’Ц’},{‘Ц’,’К’},{‘З’,’В’},{‘В’,’З’},{‘Ф’,’Ю’},{‘Ю’,’Ф’},{‘Г’,’Ж’},{‘Ж’,’Г’},{‘М’,’А’},{‘А’,’М’}};

char encodeAll(Rotor[] rotors, char input)
{
char result = input;
foreach (Rotor rotor in rotors)
{
char last = result;
result = rotor.encode(result);
//if (rotor == rotor3)
//{
//Console.WriteLine($“encode – input:{last},result:{result}“);
// }
}
return result;
}
char decodeAll(Rotor[] rotors, char input)
{
char result = input;
for (int i = 0; i < rotors.Length; i++)
{
char last = result;
result = rotors[rotors.Length – 1 – i].decode(result);
//if (rotors[rotors.Length – 1 – i] == rotor3)
//{
// Console.WriteLine($“decode – input:{last},result:{result}“);
//}
}
return result;
}

bool play = true;

//UI
Console.WriteLine(„1 – шифровай \n 2 – разшифровай \n стоп – да спреш програма“);
do
{
string input = Console.ReadLine();
switch (input)
{
case „1“:
Console.WriteLine(„съобщение:“);
string result =““;
string message = Console.ReadLine().ToUpper();
for (int i = 0; i< rotors.Length; i++) {
Console.WriteLine($“индекс на ротор {i}“);
rotors[i].index = int.Parse(Console.ReadLine());
}
Console.WriteLine(„да се ползва ли плъгборда? да/не“);
string plugboard = Console.ReadLine();
foreach (char originalLetter in message)
{
char boarded;
if (plugboard.ToLower() == „да“)
{
char a = encodeAll(rotors, PlugBoard[originalLetter]);
boarded = PlugBoard[a];
}
else
{
boarded = encodeAll(rotors, originalLetter);
}
result += boarded;
}
Console.WriteLine(result);
for (int i = 0; i < rotors.Length; i++)
{
Console.WriteLine($“индекс на ротор {i}: {rotors[i].index}“);
}
break;
case „2“:
Console.WriteLine(„съобщение:“);
string d_result =““;
string d_message = Console.ReadLine().ToUpper();
for (int i = 0; i< rotors.Length; i++) {
Console.WriteLine($“индекс на ротор {i}“);
rotors[i].index = int.Parse(Console.ReadLine());
}
Console.WriteLine(„да се ползва ли плъгборда? да/не“);
string d_plugboard = Console.ReadLine();
foreach (Rotor rotor in rotors)
{
rotor.index++;
}
for (int i = 0; i< d_message.Length; i++)
{
char boarded;
char originalLetter = d_message[d_message.Length – 1 – i];
// char boarded = originalLetter;
if (d_plugboard.ToLower() == „да“)
{
boarded = PlugBoard[decodeAll(rotors, PlugBoard[originalLetter])];
}
else
{
boarded = decodeAll(rotors, originalLetter);
}
d_result = boarded + d_result;
}
Console.WriteLine(d_result);
break;
case „stop“:
play = false;
break;
}
}
while (play) ;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Enigma_fixed
{
internal class Rotor
{
const string alphabet = „АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ“;
static Dictionary alphabetToIndex = new Dictionary { { ‘А’, 0 }, { ‘Б’, 1 }, { ‘В’, 2 }, { ‘Г’, 3 }, { ‘Д’, 4 }, { ‘Е’, 5 }, { ‘Ж’, 6 }, { ‘З’, 7 }, { ‘И’, 8 }, { ‘Й’, 9 }, { ‘К’, 10 }, { ‘Л’, 11 }, { ‘М’, 12 }, { ‘Н’, 13 }, { ‘О’, 14 }, { ‘П’, 15 }, { ‘Р’, 16 }, { ‘С’, 17 }, { ‘Т’, 18 }, { ‘У’, 19 }, { ‘Ф’, 20 }, { ‘Х’, 21 }, { ‘Ц’, 22 }, { ‘Ч’, 23 }, { ‘Ш’, 24 }, { ‘Щ’, 25 }, { ‘Ъ’, 26 }, { ‘Ь’, 27 }, { ‘Ю’, 28 }, { ‘Я’, 29 } };
public Dictionary changed = new Dictionary();
public Dictionary unchanged = new Dictionary();
public int index = -1;//да почнем от 0
public string RotorAlphabet;
public Rotor(string alpha)
{
this.RotorAlphabet = alpha;
for (int i = 0; i < alpha.Length; i++) { changed[alphabet[i]] = alpha[i]; unchanged[alpha[i]] = alphabet[i]; } } public char encode(char input) { if (alphabetToIndex.ContainsKey(input)) { index++; return changed[alphabet[(alphabetToIndex[input] + index) % 30]]; } return input; } public char decode(char input) { if (alphabetToIndex.ContainsKey(input)) { //if (index >= 1)
//{
// index–;
//}
//else
//{
// index = 30;
//}
index–;
//Console.WriteLine($“index:{index}, RotorAlphabet: {RotorAlphabet}, ABS:{Math.Abs(RotorAlphabet.IndexOf(input) – index)}, „);
return unchanged[RotorAlphabet[(((RotorAlphabet.IndexOf(input) – index) % 30) + 30) % 30]];
}
return input;
}
public void showDic()
{
foreach (char ch in unchanged.Keys)
{
Console.WriteLine($“{ch}:{unchanged[ch]}“);
}
}
}
}

char[] abc = new String(„АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯ“).ToCharArray();
Dictionary plugboard = new Dictionary();
Random random = new Random();
Console.WriteLine(„{„);
for (int i = abc.Length – 1; i > 0; i -= 2)
{
int num = random.Next(0, i);
plugboard[abc[i]] = abc[num];
plugboard[abc[num]] = abc[i];
char next = abc[i – 1];
abc[i – 1] = abc[num];
Console.Write($“{{‘{abc[i]}’,'{abc[num]}’}},{{‘{abc[num]}’,'{abc[i]}’}},“);
abc[num] = next;
}
Console.WriteLine(„}“);
// n = 30
// -3 => n + (-3) => 27
// 33 => 33 % n => 3
Console.WriteLine(-62 % 30);

Leave a Comment

Вашият имейл адрес няма да бъде публикуван. Задължителните полета са отбелязани с *