• 08-12-2024, 23:04:12
    #1
    Almanların meşhur Enigma makinesinin şifreleme algoritmasını programlama dilinde yazmak için, Enigma'nın temel işleyişine uygun bir sınıf yapısı oluşturabiliriz. Bu işlemde rotorlar, yansıtıcı (reflector) ve harf harfine dönüşüm gibi adımlar bulunur.

    VB.Net Örnek Kodu :

    Imports System
    
    Module Program
        Sub Main()
            ' Örnek kullanım
            Dim enigma As New EnigmaMachine()
            enigma.SetRotorPositions("A", "A", "A")
            Dim plaintext As String = "HELLO"
            Dim ciphertext As String = enigma.EncryptMessage(plaintext)
            Console.WriteLine("Şifreli Mesaj: " & ciphertext)
        End Sub
    End Module
    
    Public Class EnigmaMachine
        Private Reflector As String = "YRUHQSLDPXNGOKMIEBFZCWVJAT" ' Basit bir yansıtıcı
        Private Rotor1 As String = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
        Private Rotor2 As String = "AJDKSIRUXBLHWTMCQGZNPYFVOE"
        Private Rotor3 As String = "BDFHJLCPRTXVZNYEIWGAKMUSQO"
        Private Notches As Integer() = {16, 4, 21} ' Dönme konumları
        Private RotorPositions As Integer() = {0, 0, 0} ' A, A, A
    
        Public Sub SetRotorPositions(pos1 As Char, pos2 As Char, pos3 As Char)
            RotorPositions(0) = Asc(pos1) - Asc("A"c)
            RotorPositions(1) = Asc(pos2) - Asc("A"c)
            RotorPositions(2) = Asc(pos3) - Asc("A"c)
        End Sub
    
        Public Function EncryptMessage(message As String) As String
            Dim result As String = ""
            For Each ch In message
                If Char.IsLetter(ch) Then
                    StepRotors()
                    result &= EncryptCharacter(Char.ToUpper(ch))
                End If
            Next
            Return result
        End Function
    
        Private Sub StepRotors()
            ' Rotor 1 her harften sonra bir kez döner
            RotorPositions(0) = (RotorPositions(0) + 1) Mod 26
            ' Rotor 2'yi döndürme
            If RotorPositions(0) = Notches(0) Then
                RotorPositions(1) = (RotorPositions(1) + 1) Mod 26
            End If
            ' Rotor 3'ü döndürme
            If RotorPositions(1) = Notches(1) Then
                RotorPositions(2) = (RotorPositions(2) + 1) Mod 26
            End If
        End Sub
    
        Private Function EncryptCharacter(ch As Char) As Char
            Dim index As Integer = Asc(ch) - Asc("A"c)
    
            ' Rotorlar üzerinden ileri dönüşüm
            index = MapThroughRotor(index, Rotor1, RotorPositions(0))
            index = MapThroughRotor(index, Rotor2, RotorPositions(1))
            index = MapThroughRotor(index, Rotor3, RotorPositions(2))
    
            ' Yansıtıcı
            index = Asc(Reflector(index)) - Asc("A"c)
    
            ' Rotorlar üzerinden geri dönüşüm (ters yönde)
            index = MapThroughRotor(index, Rotor3, RotorPositions(2), True)
            index = MapThroughRotor(index, Rotor2, RotorPositions(1), True)
            index = MapThroughRotor(index, Rotor1, RotorPositions(0), True)
    
            ' Şifreli harfi oluştur
            Return Chr(index + Asc("A"c))
        End Function
    
        Private Function MapThroughRotor(index As Integer, rotor As String, position As Integer, Optional reverse As Boolean = False) As Integer
            If reverse Then
                ' Ters yönde rotor haritalaması
                Dim ch As Char = Chr((index + position) Mod 26 + Asc("A"c))
                index = rotor.IndexOf(ch)
                index = (index - position + 26) Mod 26
            Else
                ' İleri yönde rotor haritalaması
                Dim ch As Char = rotor((index + position) Mod 26)
                index = Asc(ch) - Asc("A"c)
                index = (index - position + 26) Mod 26
            End If
            Return index
        End Function
    End Class
  • 09-12-2024, 00:38:37
    #2
    güzel Peki kırılmaması için neler yapılabilir