Zaczniemy nasz tekst od szyfru Playfair, gdyż jego innowacyjność dała początek szyfrom dwukwadratowym i czterokwadratowym. Wymyślony w roku 1854 przez Charlesa Wheatstone’a szyfra należy do rodziny szyfrów podstawieniowych poligramowych, polega na zastąpieniu par liter tekstu jawnego inną parą generowaną przez określony złożony system. System ten bazuje na słowie kluczu i tablicy o wymiarach 5×5. Jeśli za klucz przyjmiemy słowo SEJF, litery alfabetu łacińskiego zapisujemy w tablicy, zaczynając od słowa klucza, obie strony muszą wcześniej ustalić, w jakiej konfiguracji będzie wpisywany klucz i reszta liter. Jak łatwo się domyślić liter w alfabecie łacińskim jest 26 a kwadrat 5×5 generuje 25 pól, toteż litery „I” i „J” możemy potraktować wspólnie lub pominąć literę rzadko występującą w obiegu, taką jak X lub V.
Jeśli litery w kluczu powtarzają się to następna litera, zostaje pominięta (DOROTA–>DORTA). Tekst jawny dzielony jest na diagramy (pary liter), które powinny składać się z dwóch różnych od siebie liter, w razie konieczności można dodać mało używaną literę jako separator, również, jeśli liter w jawnej wiadomości jest nieparzysta, ilość należy do ostatniej litery dodać mało występującą literę. Tekst „tajny szyfr” podzielimy na:
ta-jn-ys-zy-fr
Słowo „message” podzielimy na:
me-sx-sa-ge
dodajemy mało używaną literę „x” aby odseparować powtarzające się litery.
Słowo „szyfr” podzielimy następująco:
sz-yf-rx
W pełnym zdaniu pozbywamy się spacji i znaków interpunkcyjnych:
tekst „To jest tajna wiadomość!” przekształcimy na „tojesttajnawiadomosc” i podzielimy:
to-je-st-ta-jn-aw-ia-do-mo-sc
Szyfrując tekst, kierujemy się następującymi warunkami:
- Jeśli obie litery są w tym samym wierszu, zastępujemy je występującymi po nich (cyklicznie) literami.
- Jeśli obie litery są w tej samej kolumnie, zastępujemy je literami, które są pod nimi.
- Jeśli litery, znajdują się na pozycji x,y i a,b to zastępujemy je literami znajdującymi się na pozycji x,b i a,y
Przykład:
Gdy znamy już zasady szyfru Playfair, zrobimy sobie program, który będzie szyfrował tekst przy użyciu tego sposobu. Będzie to bardzo prosta aplikacja.
Nasz ukryty tekst przy użyciu klucza „SZYFR” to XAGFVAUYLFCIGC
Po odszyfrowaniu:
Kod aplikacji dostępny tutaj:
Imports System
Imports System.Text
Module Program
'skomponowana tabela
Dim composedTable As New List(Of String)
'tabela startowa, jej modyfikacja utrudnia złamanie szyfru
Dim startTable As New List(Of String)(New String() {"A", "B", "C", "D", "E",
"F", "G", "H", "I", "K",
"L", "M", "N", "O", "P",
"Q", "R", "S", "T", "W",
"U", "V", "X", "Y", "Z"})
'tekst do ukrycia lub odszyfrowania
Dim hiddenText As String
'tekst po zaszyfrowaniu
Dim textAfterEncription As String
'tekst po odszyfrowaniu
Dim textAfterDecription As String
Sub Main(args As String())
''''proste menu użytkownika
Console.WriteLine("Co chesz zrobić? Wpisz cyfrę zadania." + vbNewLine + "1.Zaszyfruj tekst." _
+ vbNewLine + "2.Odszyfruj tekst.")
Dim zad As String
Do
zad = Console.ReadLine()
If zad = "1" Or zad = "2" Then
Exit Do
Else
Console.WriteLine("możesz wybrać tylko cyfrę 1 lub 2.")
End If
Loop
'''''
If zad = 1 Then
'użytkownik chce zaszyfrować tekst
Console.Write("Wprowadź tekst do ukrycia: ")
'pobierz tekst do zaszyfrowania od użytkownika
hiddenText = formatText(textFromConsole(), False)
'wyświetl użytkownikowi tekst po formatowaniu
'--zamienia polskie znaki, usówa spacje, przecinki ip
displayFormatedText(hiddenText)
'pobiera od użytkownika hasło do szyfrowania
Console.Write("Wprowadź hasło: ")
Dim klucz As String = Console.ReadLine()
'formatuje klucz
klucz = formatText(klucz, True)
'usówa powtarzające się litery
klucz = formatKey(klucz)
'wyświetla sformatowany klucz
Console.Write("Twoje hasło po formatowaniu: " + klucz + vbNewLine)
'generuje i wyświetla sformatowaną tabele
Console.Write(vbNewLine + "Tabela szyfrująca:" + vbNewLine)
composedTable = generatePlayFairTable(klucz)
displayTable()
'szyfruje i wyświetla zaszyfrowaną wiadomość
Console.Write(vbNewLine + "Tekst zaszyfrowany:" + vbNewLine)
textAfterEncription = encryptText(hiddenText, composedTable, True)
displayFormatedText(textAfterEncription)
Else
Console.Write("Wprowadź zaszyfrowany tekst: ")
hiddenText = formatText(textFromConsole(), False)
Console.Write("Wprowadź hasło: ")
Dim klucz As String = Console.ReadLine()
klucz = formatText(klucz, True)
klucz = formatKey(klucz)
Console.Write("Twoje hasło po formatowaniu: " + klucz + vbNewLine)
Console.Write(vbNewLine + "Tabela szyfrująca:" + vbNewLine)
composedTable = generatePlayFairTable(klucz)
displayTable()
Console.Write(vbNewLine + "Tekst odszyfrowany:" + vbNewLine)
textAfterDecription = encryptText(hiddenText, composedTable, False)
displayFormatedText(textAfterDecription)
End If
Console.ReadLine()
End Sub
Private Function textFromConsole() As String
'element ten służy do pobrania tekstu od użytkownika
Dim tekstDoUkrycia = New StringBuilder()
While True
Dim cki As ConsoleKeyInfo = Console.ReadKey(True)
Select Case cki.Key
Case ConsoleKey.Enter
' Koniec wprowadzania
Console.WriteLine()
Exit While
Case ConsoleKey.Backspace
' Usówanie ostatniego znaku
If tekstDoUkrycia.Length > 0 Then
tekstDoUkrycia.Remove(tekstDoUkrycia.Length - 1, 1)
Console.Write(vbBack & " " & vbBack)
End If
Case Else
' Dodaje znaki do zmiennej string
tekstDoUkrycia.Append(cki.KeyChar)
Console.Write(cki.KeyChar)
End Select
End While
Return tekstDoUkrycia.ToString
End Function
Private Function formatText(text As String, isKey As Boolean) As String
'formatuje nasz tekst, usówa spacje i zamienia wybrane elementy
Dim formatedTekst As String = text.Trim.ToUpper
formatedTekst = formatedTekst.Replace(" ", "")
formatedTekst = formatedTekst.Replace(",", "")
formatedTekst = formatedTekst.Replace("!", "")
formatedTekst = formatedTekst.Replace("?", "")
formatedTekst = formatedTekst.Replace("Ą", "A")
formatedTekst = formatedTekst.Replace("Ę", "E")
formatedTekst = formatedTekst.Replace("Ó", "O")
formatedTekst = formatedTekst.Replace("Ś", "S")
formatedTekst = formatedTekst.Replace("Ł", "L")
formatedTekst = formatedTekst.Replace("Ż", "Z")
formatedTekst = formatedTekst.Replace("Ź", "Z")
formatedTekst = formatedTekst.Replace("Ć", "C")
formatedTekst = formatedTekst.Replace("Ń", "N")
'ten element zasługuje na szczególną uwagę ponieważ jest odpowiedzialny za
'zamiane litery J na literę I
formatedTekst = formatedTekst.Replace("J", "I")
If isKey = False Then
StartOver:
For i As Integer = 0 To formatedTekst.Length - 1
If Not ((i + 1) Mod 2) = 0 Then
'nieparzysta litera
'nie ostatnia
If Not i = formatedTekst.Length - 1 Then
'następna litera jest taka sama jak pierwsza
If formatedTekst(i) = formatedTekst(i + 1) Then
Select Case formatedTekst(i)
'jeśli literą powtarzającą się jest X wtedy dodajemy Y
Case "X"
formatedTekst = formatedTekst.Insert(i + 1, "Y")
GoTo StartOver
Case Else
formatedTekst = formatedTekst.Insert(i + 1, "X")
GoTo StartOver
End Select
End If
Else
formatedTekst = formatedTekst.Insert(i + 1, "X")
End If
End If
Next
End If
Return formatedTekst
End Function
Private Function formatKey(key As String) As String
'usówa powtarzające się litery
Dim formatedKey As String = ""
For i As Integer = 0 To key.Length - 1
If Not formatedKey.Contains(key(i)) Then
formatedKey += key(i)
End If
Next
Return formatedKey
End Function
Private Function generatePlayFairTable(key As String) As List(Of String)
'zwraca sformatowaną tabelę połączoną z kluczem
Dim newTable As New List(Of String)
newTable = startTable
For i As Integer = key.Length - 1 To 0 Step -1
For j As Integer = 0 To newTable.Count - 1
If key(i) = newTable(j) Then
newTable.RemoveAt(j)
Exit For
End If
Next
newTable.Insert(0, key(i))
Next
Return newTable
End Function
Private Sub displayFormatedText(text As String)
'wyświetla sformatowany tekst
For i As Integer = 0 To text.Length - 1
If ((i + 1) Mod 2) = 0 Then
Console.Write(text(i - 1) + text(i)) ' + "-")
End If
Next
Console.Write(vbNewLine)
End Sub
Private Sub displayTable()
'wyświetli tabele
For i As Integer = 0 To composedTable.Count - 1
Console.Write(composedTable(i) + ",")
If ((i + 1) Mod 5) = 0 Then
Console.Write(vbNewLine)
End If
Next
End Sub
Private Function encryptText(text As String, encryptTable As List(Of String), crypt As Boolean) As String
'funkcja szyfrująca i deszyfrująca tekst
Dim cryptedText As String = ""
Dim myKeyArray(5, 5) As String
Dim rowNum As Integer = 0
Dim columnNum As Integer = 0
For i As Integer = 0 To composedTable.Count - 1
myKeyArray(rowNum, columnNum) = composedTable(i)
columnNum += 1
If ((i + 1) Mod 5) = 0 Then
rowNum += 1
columnNum = 0
End If
Next
For i As Integer = 0 To text.Length - 1
If ((i + 1) Mod 2) = 0 Then
Dim indexOfFirstLetter As Integer() = indexOf(text(i - 1))
Dim indexOfSecoundLetter As Integer() = indexOf(text(i))
If indexOfFirstLetter(0) = indexOfSecoundLetter(0) Then
'Równe wiersze
cryptedText += sameColumn(indexOfFirstLetter, indexOfSecoundLetter, myKeyArray, crypt)
ElseIf indexOfFirstLetter(1) = indexOfSecoundLetter(1) Then
'Równe kolumny
cryptedText += sameRow(indexOfFirstLetter, indexOfSecoundLetter, myKeyArray, crypt)
Else
cryptedText += diffColumnRow(indexOfFirstLetter, indexOfSecoundLetter, myKeyArray, crypt)
End If
End If
Next
Return cryptedText
End Function
Private Function indexOf(letter As String) As Integer()
'sprawdza indeks litery w tabeli
Dim index As Integer = composedTable.IndexOf(letter) + 1
Dim rowIndex = index Mod 5
If rowIndex = 0 Then
rowIndex = 5
End If
Return {rowIndex - 1, Math.Ceiling(index / 5) - 1}
End Function
Private Function sameRow(firstLetter As Integer(), secoundLetter As Integer(), keyArray(,) As String,
left As Boolean) As String
'wiemy, że wiersze są róne więc należy przeprowadzić transformacje -1 w lewo
'jeśli kolumna ma index 0 wtedy będzie to index 5
Dim cipheredtext As String = ""
Dim columnNum As Integer = firstLetter(1)
If left = True Then
'odszyfruj, jeden w prawo
If firstLetter(0) + 1 > 4 Then
cipheredtext += keyArray(columnNum, 0)
Else
cipheredtext += keyArray(columnNum, firstLetter(0) + 1)
End If
If secoundLetter(0) + 1 > 4 Then
cipheredtext += keyArray(columnNum, 0)
Else
cipheredtext += keyArray(columnNum, secoundLetter(0) + 1)
End If
Else
'szyfruj, jeden w lewo
If firstLetter(0) - 1 < 0 Then
cipheredtext += keyArray(columnNum, 4)
Else
cipheredtext += keyArray(columnNum, firstLetter(0) - 1)
End If
If secoundLetter(0) - 1 < 0 Then
cipheredtext += keyArray(columnNum, 4)
Else
cipheredtext += keyArray(columnNum, secoundLetter(0) - 1)
End If
End If
Return cipheredtext
End Function
Private Function sameColumn(firstLetter As Integer(), secoundLetter As Integer(), keyArray(,) As String,
down As Boolean) As String
'wiemy, że wiersze są róne więc należy przeprowadzić transformacje -1 w lewo
'jeśli kolumna ma index 0 wtedy będzie to index 5
Dim cipheredtext As String = ""
Dim rowNum As Integer = firstLetter(0)
If down = True Then
'szyfruj, jedne w dół
If firstLetter(1) + 1 > 4 Then
cipheredtext += keyArray(0, rowNum)
Else
cipheredtext += keyArray(firstLetter(1) + 1, rowNum)
End If
If secoundLetter(1) + 1 > 4 Then
cipheredtext += keyArray(0, rowNum)
Else
cipheredtext += keyArray(secoundLetter(1) + 1, rowNum)
End If
Else
'odszyfruj, jeden w góre
If firstLetter(1) - 1 < 0 Then
cipheredtext += keyArray(4, rowNum)
Else
cipheredtext += keyArray(firstLetter(1) - 1, rowNum)
End If
If secoundLetter(1) - 1 < 0 Then
cipheredtext += keyArray(4, rowNum)
Else
cipheredtext += keyArray(secoundLetter(1) - 1, rowNum)
End If
End If
Return cipheredtext
End Function
Private Function diffColumnRow(firstLetter As Integer(), secoundLetter As Integer(), keyArray(,) As String,
crypt As Boolean) As String
Dim cipheredtext As String = ""
If crypt = True Then
'szyfruj
cipheredtext += keyArray(firstLetter(1), secoundLetter(0))
cipheredtext += keyArray(secoundLetter(1), firstLetter(0))
Else
'odszyfruj
cipheredtext += keyArray(firstLetter(1), secoundLetter(0))
cipheredtext += keyArray(secoundLetter(1), firstLetter(0))
End If
Return cipheredtext
End Function
End Module
projekt: Playfair
sama aplikacja: PlayFair_aplikacja
Wiemy już, czym jest szyfr PlayFair, ale czy jego stosowanie ma sens w obecnych czasach? Nie można przy jego użyciu szyfrować cyfr i ma wiele ograniczeń co do ilości liter. Każde rozszerzenie tego szyfru wiąże się z koniecznością powiększania wielkości naszej tabeli. Jeśli przyjmiemy, że w języku polskim jest 32 litery wtedy najbliższy kwadrat, który możemy wygenerować, będzie złożony z sześciu wierszy i kolumn a i tak będziemy musieli dodać znaki które nie są literami. Przekształcenie tego programu wyglądało by jak:
Imports System
Imports System.Text
Module Program
'skomponowana tabela
Dim composedTable As New List(Of String)
'tabela startowa, jej modyfikacja utrudnia złamanie szyfru
Dim startTable As New List(Of String)(New String() {"A", "Ą", "B", "C", "Ć", "D",
"E", "Ę", "F", "G", "H", "I",
"J", "K", "L", "Ł", "M", "N",
"Ń", "O", "Ó", "P", "R", "S",
"Ś", "T", "U", "W", "Y", "Z",
"Ź", "Ż", ",", ".", "!", "?"})
'tekst do ukrycia lub odszyfrowania
Dim hiddenText As String
'tekst po zaszyfrowaniu
Dim textAfterEncription As String
'tekst po odszyfrowaniu
Dim textAfterDecription As String
Sub Main(args As String())
''''proste menu użytkownika
Console.WriteLine("Co chesz zrobić? Wpisz cyfrę zadania." + vbNewLine + "1.Zaszyfruj tekst." _
+ vbNewLine + "2.Odszyfruj tekst.")
Dim zad As String
Do
zad = Console.ReadLine()
If zad = "1" Or zad = "2" Then
Exit Do
Else
Console.WriteLine("możesz wybrać tylko cyfrę 1 lub 2.")
End If
Loop
'''''
If zad = 1 Then
'użytkownik chce zaszyfrować tekst
Console.Write("Wprowadź tekst do ukrycia: ")
'pobierz tekst do zaszyfrowania od użytkownika
hiddenText = formatText(textFromConsole(), False)
'wyświetl użytkownikowi tekst po formatowaniu
'--zamienia polskie znaki, usówa spacje, przecinki ip
displayFormatedText(hiddenText)
'pobiera od użytkownika hasło do szyfrowania
Console.Write("Wprowadź hasło: ")
Dim klucz As String = Console.ReadLine()
'formatuje klucz
klucz = formatText(klucz, True)
'usówa powtarzające się litery
klucz = formatKey(klucz)
'wyświetla sformatowany klucz
Console.Write("Twoje hasło po formatowaniu: " + klucz + vbNewLine)
'generuje i wyświetla sformatowaną tabele
Console.Write(vbNewLine + "Tabela szyfrująca:" + vbNewLine)
composedTable = generatePlayFairTable(klucz)
displayTable()
'szyfruje i wyświetla zaszyfrowaną wiadomość
Console.Write(vbNewLine + "Tekst zaszyfrowany:" + vbNewLine)
textAfterEncription = encryptText(hiddenText, composedTable, True)
displayFormatedText(textAfterEncription)
Else
Console.Write("Wprowadź zaszyfrowany tekst: ")
hiddenText = formatText(textFromConsole(), False)
Console.Write("Wprowadź hasło: ")
Dim klucz As String = Console.ReadLine()
klucz = formatText(klucz, True)
klucz = formatKey(klucz)
Console.Write("Twoje hasło po formatowaniu: " + klucz + vbNewLine)
Console.Write(vbNewLine + "Tabela szyfrująca:" + vbNewLine)
composedTable = generatePlayFairTable(klucz)
displayTable()
Console.Write(vbNewLine + "Tekst odszyfrowany:" + vbNewLine)
textAfterDecription = encryptText(hiddenText, composedTable, False)
displayFormatedText(textAfterDecription)
End If
Console.ReadLine()
End Sub
Private Function textFromConsole() As String
'element ten służy do pobrania tekstu od użytkownika
Dim tekstDoUkrycia = New StringBuilder()
While True
Dim cki As ConsoleKeyInfo = Console.ReadKey(True)
Select Case cki.Key
Case ConsoleKey.Enter
' Koniec wprowadzania
Console.WriteLine()
Exit While
Case ConsoleKey.Backspace
' Usówanie ostatniego znaku
If tekstDoUkrycia.Length > 0 Then
tekstDoUkrycia.Remove(tekstDoUkrycia.Length - 1, 1)
Console.Write(vbBack & " " & vbBack)
End If
Case Else
' Dodaje znaki do zmiennej string
tekstDoUkrycia.Append(cki.KeyChar)
Console.Write(cki.KeyChar)
End Select
End While
Return tekstDoUkrycia.ToString
End Function
Private Function formatText(text As String, isKey As Boolean) As String
'formatuje nasz tekst, usówa spacje i zamienia wybrane elementy
Dim formatedTekst As String = text.Trim.ToUpper
formatedTekst = formatedTekst.Replace(" ", "")
If isKey = False Then
StartOver:
For i As Integer = 0 To formatedTekst.Length - 1
If Not ((i + 1) Mod 2) = 0 Then
'nieparzysta litera
'nie ostatnia
If Not i = formatedTekst.Length - 1 Then
'następna litera jest taka sama jak pierwsza
If formatedTekst(i) = formatedTekst(i + 1) Then
Select Case formatedTekst(i)
'jeśli literą powtarzającą się jest X wtedy dodajemy Y
Case "X"
formatedTekst = formatedTekst.Insert(i + 1, "Y")
GoTo StartOver
Case Else
formatedTekst = formatedTekst.Insert(i + 1, "X")
GoTo StartOver
End Select
End If
Else
formatedTekst = formatedTekst.Insert(i + 1, "X")
End If
End If
Next
End If
Return formatedTekst
End Function
Private Function formatKey(key As String) As String
'usówa powtarzające się litery
Dim formatedKey As String = ""
For i As Integer = 0 To key.Length - 1
If Not formatedKey.Contains(key(i)) Then
formatedKey += key(i)
End If
Next
Return formatedKey
End Function
Private Function generatePlayFairTable(key As String) As List(Of String)
'zwraca sformatowaną tabelę połączoną z kluczem
Dim newTable As New List(Of String)
newTable = startTable
For i As Integer = key.Length - 1 To 0 Step -1
For j As Integer = 0 To newTable.Count - 1
If key(i) = newTable(j) Then
newTable.RemoveAt(j)
Exit For
End If
Next
newTable.Insert(0, key(i))
Next
Return newTable
End Function
Private Sub displayFormatedText(text As String)
'wyświetla sformatowany tekst
For i As Integer = 0 To text.Length - 1
If ((i + 1) Mod 2) = 0 Then
Console.Write(text(i - 1) + text(i)) ' + "-")
End If
Next
Console.Write(vbNewLine)
End Sub
Private Sub displayTable()
'wyświetli tabele
For i As Integer = 0 To composedTable.Count - 1
Console.Write(composedTable(i) + ",")
If ((i + 1) Mod 6) = 0 Then
Console.Write(vbNewLine)
End If
Next
End Sub
Private Function encryptText(text As String, encryptTable As List(Of String), crypt As Boolean) As String
'funkcja szyfrująca i deszyfrująca tekst
Dim cryptedText As String = ""
Dim myKeyArray(6, 6) As String
Dim rowNum As Integer = 0
Dim columnNum As Integer = 0
For i As Integer = 0 To composedTable.Count - 1
myKeyArray(rowNum, columnNum) = composedTable(i)
columnNum += 1
If ((i + 1) Mod 6) = 0 Then
rowNum += 1
columnNum = 0
End If
Next
For i As Integer = 0 To text.Length - 1
If ((i + 1) Mod 2) = 0 Then
Dim indexOfFirstLetter As Integer() = indexOf(text(i - 1))
Dim indexOfSecoundLetter As Integer() = indexOf(text(i))
If indexOfFirstLetter(0) = indexOfSecoundLetter(0) Then
'Równe wiersze
cryptedText += sameColumn(indexOfFirstLetter, indexOfSecoundLetter, myKeyArray, crypt)
ElseIf indexOfFirstLetter(1) = indexOfSecoundLetter(1) Then
'Równe kolumny
cryptedText += sameRow(indexOfFirstLetter, indexOfSecoundLetter, myKeyArray, crypt)
Else
cryptedText += diffColumnRow(indexOfFirstLetter, indexOfSecoundLetter, myKeyArray, crypt)
End If
End If
Next
Return cryptedText
End Function
Private Function indexOf(letter As String) As Integer()
'sprawdza indeks litery w tabeli
Dim index As Integer = composedTable.IndexOf(letter) + 1
Dim rowIndex = index Mod 6
If rowIndex = 0 Then
rowIndex = 6
End If
Return {rowIndex - 1, Math.Ceiling(index / 6) - 1}
End Function
Private Function sameRow(firstLetter As Integer(), secoundLetter As Integer(), keyArray(,) As String,
left As Boolean) As String
'wiemy, że wiersze są róne więc należy przeprowadzić transformacje -1 w lewo
'jeśli kolumna ma index 0 wtedy będzie to index 5
Dim cipheredtext As String = ""
Dim columnNum As Integer = firstLetter(1)
If left = True Then
'odszyfruj, jeden w prawo
If firstLetter(0) + 1 > 5 Then
cipheredtext += keyArray(columnNum, 0)
Else
cipheredtext += keyArray(columnNum, firstLetter(0) + 1)
End If
If secoundLetter(0) + 1 > 5 Then
cipheredtext += keyArray(columnNum, 0)
Else
cipheredtext += keyArray(columnNum, secoundLetter(0) + 1)
End If
Else
'szyfruj, jeden w lewo
If firstLetter(0) - 1 < 0 Then
cipheredtext += keyArray(columnNum, 5)
Else
cipheredtext += keyArray(columnNum, firstLetter(0) - 1)
End If
If secoundLetter(0) - 1 < 0 Then
cipheredtext += keyArray(columnNum, 5)
Else
cipheredtext += keyArray(columnNum, secoundLetter(0) - 1)
End If
End If
Return cipheredtext
End Function
Private Function sameColumn(firstLetter As Integer(), secoundLetter As Integer(), keyArray(,) As String,
down As Boolean) As String
'wiemy, że wiersze są róne więc należy przeprowadzić transformacje -1 w lewo
'jeśli kolumna ma index 0 wtedy będzie to index 5
Dim cipheredtext As String = ""
Dim rowNum As Integer = firstLetter(0)
If down = True Then
'szyfruj, jedne w dół
If firstLetter(1) + 1 > 5 Then
cipheredtext += keyArray(0, rowNum)
Else
cipheredtext += keyArray(firstLetter(1) + 1, rowNum)
End If
If secoundLetter(1) + 1 > 5 Then
cipheredtext += keyArray(0, rowNum)
Else
cipheredtext += keyArray(secoundLetter(1) + 1, rowNum)
End If
Else
'odszyfruj, jeden w góre
If firstLetter(1) - 1 < 0 Then
cipheredtext += keyArray(5, rowNum)
Else
cipheredtext += keyArray(firstLetter(1) - 1, rowNum)
End If
If secoundLetter(1) - 1 < 0 Then
cipheredtext += keyArray(5, rowNum)
Else
cipheredtext += keyArray(secoundLetter(1) - 1, rowNum)
End If
End If
Return cipheredtext
End Function
Private Function diffColumnRow(firstLetter As Integer(), secoundLetter As Integer(), keyArray(,) As String,
crypt As Boolean) As String
Dim cipheredtext As String = ""
If crypt = True Then
'szyfruj
cipheredtext += keyArray(firstLetter(1), secoundLetter(0))
cipheredtext += keyArray(secoundLetter(1), firstLetter(0))
Else
'odszyfruj
cipheredtext += keyArray(firstLetter(1), secoundLetter(0))
cipheredtext += keyArray(secoundLetter(1), firstLetter(0))
End If
Return cipheredtext
End Function
End Module
Przykład użycia:
Można pójść dalej, jeśli w języku poskim jest 32 litery i podzielimy je na małe i duże otrzymamy okrągłą liczbę 64 = 8 * 8
Imports System
Imports System.Text
Module Program
Dim startTable As New List(Of String)(New String() {"A", "a", "B", "b", "Ą", "ą", "C", "c",
"Ć", "ć", "D", "d", "E", "e", "Ę", "ę",
"F", "f", "G", "g", "H", "h", "I", "i",
"J", "j", "K", "k", "L", "l", "Ł", "ł",
"M", "m", "N", "n", "Ń", "ń", "O", "o",
"Ó", "ó", "P", "p", "Q", "q", "R", "r",
"S", "s", "T", "t", "U", "u", "W", "w",
"Y", "y", "Z", "z", "Ź", "ź", "Ż", "ż"})








