-Поиск по дневнику

Поиск сообщений в Eski_the_Goblin

 -Подписка по e-mail

 

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 16.08.2009
Записей:
Комментариев:
Написано: 113


Криптуха по-старинке

Вторник, 02 Февраля 2010 г. 15:19 + в цитатник
Не так давно я увлекся криптографией. И решил разбобраться во всем с самого начала - то есть со старых шифров, типа Цезаря, простой одноалфавитной замены, и прочего. В частности, заинтересовал меня шифр Виженера, который два века считался невзламываемым принципиально, т.к. в нем использовалось неизвестное количество неизвестных и шифроалфавитов, т.е. это был многоалфавитный шифр замены. А для лучше понимания процесса шифрования я решил алгоритм формализовать и написать код. На чем? Конечно, на тех языках, которыми сам сейчас заинтересовался - Python и Java. Сначала был написан под на Java, и выглядел от так:


import java.lang.String;

public class Vijener
{
public static void main(String[] args)
{
String OpenText = "TESTMESSAGE";
String Key = "TESTKEY";
String CryptedText = "";

int OpenIter=0;//итератор открытого сообщения
int KeyIter=0;//итератор ключа

for (OpenIter=0; OpenIter(Key.length()-1))
{
KeyIter = KeyIter-Key.length();
continue;
}

int OpenIndex=256; //это индекс символа открытого текста
int KeyIndex=256; //это индекс символа ключа

if(OpenText.charAt(OpenIter) == 'A')
{
OpenIndex=0;
}
if(OpenText.charAt(OpenIter) == 'B')
{
OpenIndex=1;
}
if(OpenText.charAt(OpenIter) == 'C')
{
OpenIndex=2;
}
if(OpenText.charAt(OpenIter) == 'D')
{
OpenIndex=3;
}
if(OpenText.charAt(OpenIter) == 'E')
{
OpenIndex=4;
}
if(OpenText.charAt(OpenIter) == 'F')
{
OpenIndex=5;
}
if(OpenText.charAt(OpenIter) == 'G')
{
OpenIndex=6;
}
if(OpenText.charAt(OpenIter) == 'H')
{
OpenIndex=7;
}
if(OpenText.charAt(OpenIter) == 'I')
{
OpenIndex=8;
}
if(OpenText.charAt(OpenIter) == 'J')
{
OpenIndex=9;
}
if(OpenText.charAt(OpenIter) == 'K')
{
OpenIndex=10;
}
if(OpenText.charAt(OpenIter) == 'L')
{
OpenIndex=11;
}
if(OpenText.charAt(OpenIter) == 'M')
{
OpenIndex=12;
}
if(OpenText.charAt(OpenIter) == 'N')
{
OpenIndex=13;
}
if(OpenText.charAt(OpenIter) == 'O')
{
OpenIndex=14;
}
if(OpenText.charAt(OpenIter) == 'P')
{
OpenIndex=15;
}
if(OpenText.charAt(OpenIter) == 'Q')
{
OpenIndex=16;
}
if(OpenText.charAt(OpenIter) == 'R')
{
OpenIndex=17;
}
if(OpenText.charAt(OpenIter) == 'S')
{
OpenIndex=18;
}
if(OpenText.charAt(OpenIter) == 'T')
{
OpenIndex=19;
}
if(OpenText.charAt(OpenIter) == 'U')
{
OpenIndex=20;
}
if(OpenText.charAt(OpenIter) == 'V')
{
OpenIndex=21;
}
if(OpenText.charAt(OpenIter) == 'W')
{
OpenIndex=22;
}
if(OpenText.charAt(OpenIter) == 'X')
{
OpenIndex=23;
}
if(OpenText.charAt(OpenIter) == 'Y')
{
OpenIndex=24;
}
if(OpenText.charAt(OpenIter) == 'Z')
{
OpenIndex=25;
}

if(Key.charAt(KeyIter) == 'A')
{
KeyIndex=0;
}
if(Key.charAt(KeyIter) == 'B')
{
KeyIndex=1;
}
if(Key.charAt(KeyIter) == 'C')
{
KeyIndex=2;
}
if(Key.charAt(KeyIter) == 'D')
{
KeyIndex=3;
}
if(Key.charAt(KeyIter) == 'E')
{
KeyIndex=4;
}
if(Key.charAt(KeyIter) == 'F')
{
KeyIndex=5;
}
if(Key.charAt(KeyIter) == 'G')
{
KeyIndex=6;
}
if(Key.charAt(KeyIter) == 'H')
{
KeyIndex=7;
}
if(Key.charAt(KeyIter) == 'I')
{
KeyIndex=8;
}
if(Key.charAt(KeyIter) == 'J')
{
KeyIndex=9;
}
if(Key.charAt(KeyIter) == 'K')
{
KeyIndex=10;
}
if(Key.charAt(KeyIter) == 'L')
{
KeyIndex=11;
}
if(Key.charAt(KeyIter) == 'M')
{
KeyIndex=12;
}
if(Key.charAt(KeyIter) == 'N')
{
KeyIndex=13;
}
if(Key.charAt(KeyIter) == 'O')
{
KeyIndex=14;
}
if(Key.charAt(KeyIter) == 'P')
{
KeyIndex=15;
}
if(Key.charAt(KeyIter) == 'Q')
{
KeyIndex=16;
}
if(Key.charAt(KeyIter) == 'R')
{
KeyIndex=17;
}
if(Key.charAt(KeyIter) == 'S')
{
KeyIndex=18;
}
if(Key.charAt(KeyIter) == 'T')
{
KeyIndex=19;
}
if(Key.charAt(KeyIter) == 'U')
{
KeyIndex=20;
}
if(Key.charAt(KeyIter) == 'V')
{
KeyIndex=21;
}
if(Key.charAt(KeyIter) == 'W')
{
KeyIndex=22;
}
if(Key.charAt(KeyIter) == 'X')
{
KeyIndex=23;
}
if(Key.charAt(KeyIter) == 'Y')
{
KeyIndex=24;
}
if(Key.charAt(KeyIter) == 'Z')
{
KeyIndex=25;
}

int CryptIndex = OpenIndex+KeyIndex;
if (CryptIndex>25)
{
CryptIndex=CryptIndex-26;
}

//определяем символ шифротекста
if (CryptIndex==0)
{
CryptedText = CryptedText + 'A';
}
if (CryptIndex==1)
{
CryptedText = CryptedText + 'B';
}
if (CryptIndex==2)
{
CryptedText = CryptedText + 'C';
}
if (CryptIndex==3)
{
CryptedText = CryptedText + 'D';
}
if (CryptIndex==4)
{
CryptedText = CryptedText + 'E';
}
if (CryptIndex==5)
{
CryptedText = CryptedText + 'F';
}
if (CryptIndex==6)
{
CryptedText = CryptedText + 'G';
}
if (CryptIndex==7)
{
CryptedText = CryptedText + 'H';
}
if (CryptIndex==8)
{
CryptedText = CryptedText + 'I';
}
if (CryptIndex==9)
{
CryptedText = CryptedText + 'J';
}
if (CryptIndex==10)
{
CryptedText = CryptedText + 'K';
}
if (CryptIndex==11)
{
CryptedText = CryptedText + 'L';
}
if (CryptIndex==12)
{
CryptedText = CryptedText + 'M';
}
if (CryptIndex==13)
{
CryptedText = CryptedText + 'N';
}
if (CryptIndex==14)
{
CryptedText = CryptedText + 'O';
}
if (CryptIndex==15)
{
CryptedText = CryptedText + 'P';
}
if (CryptIndex==16)
{
CryptedText = CryptedText + 'Q';
}
if (CryptIndex==17)
{
CryptedText = CryptedText + 'R';
}
if (CryptIndex==18)
{
CryptedText = CryptedText + 'S';
}
if (CryptIndex==19)
{
CryptedText = CryptedText + 'T';
}
if (CryptIndex==20)
{
CryptedText = CryptedText + 'U';
}
if (CryptIndex==21)
{
CryptedText = CryptedText + 'V';
}
if (CryptIndex==22)
{
CryptedText = CryptedText + 'W';
}
if (CryptIndex==23)
{
CryptedText = CryptedText + 'X';
}
if (CryptIndex==24)
{
CryptedText = CryptedText + 'Y';
}
if (CryptIndex==25)
{
CryptedText = CryptedText + 'Z';
}

}
System.out.println(CryptedText);
}
}

Уродливо, не так ли? А потому родился второй вариант, на Python. Значительно более удобоваримый, но не из-за языка, я просто немножко подумал =))

#! /usr/bin/python3.0

OpenMessage = "OPENTEXTOPENTEXTOPENTEXTOPENTEXT"
OpenAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
##CryptedMessage
IndexTable = []

Key = "TESTKEY"
x = 0
y = 0
AlphabetIter = 0

for x in range(len(OpenMessage)):
##if y >= len(Key):
##y = y-len(Key)
##not worst idea, i think
for AlphabetIter in range(len(OpenAlphabet)):
if OpenMessage[x] == OpenAlphabet[AlphabetIter]:
IndexTable.append(AlphabetIter)
y = y + 1
continue

print(IndexTable)

это далеко не весь алгоритм, но самый уродский кусок я отпимизировал. Как напишу весь код - напишу из этого добра статью для хабра. Авось хабралюдям понравится
Рубрики:  просто так
фрикинг
Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку