Task: Chaocipher








VB: Module Module1

    ReadOnly L_ALPHABET As String = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
    ReadOnly R_ALPHABET As String = "PTLNBQDEOYSFAVZKGJRIHWXUMC"

    Enum Mode
        ENCRYPT
        DECRYPT
    End Enum

    Function Exec(text As String, mode As Mode, Optional showSteps As Boolean = False) As String
        Dim left = L_ALPHABET.ToCharArray()
        Dim right = R_ALPHABET.ToCharArray()
        Dim eText(text.Length - 1) As Char
        Dim temp(25) As Char

        For i = 0 To text.Length - 1
            If showSteps Then Console.WriteLine("{0} {1}", String.Join("", left), String.Join("", right))
            Dim index As Integer
            If mode = Mode.ENCRYPT Then
                index = Array.IndexOf(right, text(i))
                eText(i) = left(index)
            Else
                index = Array.IndexOf(left, text(i))
                eText(i) = right(index)
            End If
            If i = text.Length - 1 Then Exit For

            

            For j = index To 25
                temp(j - index) = left(j)
            Next
            For j = 0 To index - 1
                temp(26 - index + j) = left(j)
            Next
            Dim store = temp(1)
            For j = 2 To 13
                temp(j - 1) = temp(j)
            Next
            temp(13) = store
            temp.CopyTo(left, 0)

            

            For j = index To 25
                temp(j - index) = right(j)
            Next
            For j = 0 To index - 1
                temp(26 - index + j) = right(j)
            Next
            store = temp(0)
            For j = 1 To 25
                temp(j - 1) = temp(j)
            Next
            temp(25) = store
            store = temp(2)
            For j = 3 To 13
                temp(j - 1) = temp(j)
            Next
            temp(13) = store
            temp.CopyTo(right, 0)
        Next

        Return eText
    End Function

    Sub Main()
        Dim plainText = "WELLDONEISBETTERTHANWELLSAID"
        Console.WriteLine("The original plaintext is : {0}", plainText)
        Console.WriteLine(vbNewLine + "The left and right alphabets after each permutation during encryption are :" + vbNewLine)
        Dim cipherText = Exec(plainText, Mode.ENCRYPT, True)
        Console.WriteLine(vbNewLine + "The ciphertext is : {0}", cipherText)
        Dim plainText2 = Exec(cipherText, Mode.DECRYPT)
        Console.WriteLine(vbNewLine + "The recovered plaintext is : {0}", plainText2)
    End Sub

End Module




Ada: with Ada.Text_IO; use Ada.Text_IO;

procedure chao_slices is
   type iMode is (Encrypt, Decrypt);

   L_Alphabet : String := "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
   R_Alphabet : String := "PTLNBQDEOYSFAVZKGJRIHWXUMC";
   plaintext  : String := "WELLDONEISBETTERTHANWELLSAID";
   ciphertext : String (1 .. plaintext'length);
   plaintext2 : String (1 .. plaintext'length);
   offset     : Natural;

   function IndexOf (Source : String; Value : Character) return Positive is
      Result : Positive;

   begin
      for I in Source'Range loop
         if Source (I) = Value then
            Result := I;
            exit;
         end if;
      end loop;
      return Result;
   end IndexOf;

   function Exec
     (Text : String; mode : iMode; showsteps : Boolean := False) return String
   is
      etext : String (Text'First .. Text'Last);
      temp  : String (1 .. 26);
      index : Positive;
      store : Character;
      left  : String := L_Alphabet;
      right : String := R_Alphabet;
   begin
      for I in Text'Range loop
         if showsteps then
            Put_Line (left & "  " & right);
         end if;

         if mode = Encrypt then
            index     := IndexOf (Source => right, Value => Text (I));
            etext (I) := left (index);
         else
            index     := IndexOf (Source => left, Value => Text (I));
            etext (I) := right (index);
         end if;

         exit when I = Text'Last;

         
         
         

         if index > 1 then
            offset                 := 26 - index;
            temp (1 .. offset + 1) := left (index .. index + offset);

            temp (offset + 2 .. 26) := left (1 .. index - 1);
            store                   := temp (2);

            temp (2 .. 13) := temp (3 .. 14);
            temp (14)      := store;
            left           := temp;

            
            
            

            temp (1 .. offset + 1) := right (index .. index + offset);

            temp (offset + 2 .. 26) := right (1 .. index - 1);
            store                   := temp (1);

            temp (1 .. 25) := temp (2 .. 26);
            temp (26)      := store;
            store          := temp (3);

            temp (3 .. 13) := temp (4 .. 14);
            temp (14)      := store;
            right          := temp;
         end if;

      end loop;

      return etext;

   end Exec;
begin
   Put_Line ("The original text is : " & plaintext);
   New_Line;
   Put_Line
     ("The left and right alphabets after each permutation during encryption are:");
   New_Line;
   ciphertext := Exec (plaintext, Encrypt, True);
   New_Line;
   Put_Line ("The ciphertext is : " & ciphertext);
   plaintext2 := Exec (ciphertext, Decrypt);
   New_Line;
   Put_Line ("The recovered plaintext is : " & plaintext2);
end chao_slices;