Advertisement
mrvmurray

temp.adb

Jun 26th, 2018
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 2.78 KB | None | 0 0
  1. --
  2. -- Copyright (c) 2015 Mark R V Murray. All rights reserved.
  3. --
  4.  
  5. with Ada.Command_line; use Ada.Command_Line;
  6. with Ada.Sequential_IO;
  7.  
  8. procedure Temp is
  9.     MasterKey : Integer;
  10.     SBoxCount : Integer;
  11.     SBoxCycle : Integer;
  12.     Encrypt : Boolean;
  13.  
  14.     subtype ASCII is Character range ' ' .. '~';
  15.     type SBox is Array(ASCII) of ASCII;
  16.  
  17.     package Char_IO is new Ada.Sequential_IO(Character); use Char_IO;
  18.     infile, outfile : Char_IO.File_Type;
  19.  
  20.     -- PRNG numbers pulled off the interwebs
  21.     X_n : Integer;
  22.     a : Constant Integer := 1664525;
  23.     c : Constant Integer := 1013904223;
  24.     m : constant Integer := 2**30;
  25.  
  26.     temp : Character;
  27.  
  28.     procedure random_ascii_setup(seed : Integer) is
  29.     begin
  30.         X_n := seed;
  31.     end random_ascii_setup;
  32.  
  33.     function random_ascii(lower, upper : ASCII) return ASCII is
  34.         Mm : Integer;
  35.     begin
  36.         Mm := ASCII'Pos(upper) - ASCII'Pos(lower) + 1;
  37.         X_n := (a*X_n + c) mod m;
  38.         return ASCII'Val((X_n mod Mm) + ASCII'Pos(lower));
  39.     end random_ascii;
  40.  
  41.     procedure shuffle(a : in out SBox) is
  42.         t, ch2 : ASCII;
  43.     begin
  44.         for ch1 in reverse ASCII'Val(ASCII'Pos(a'First) + 1) .. a'Last loop
  45.             ch2 := random_ascii(ASCII'First, ch1);
  46.             t := a(ch1);
  47.             a(ch1) := a(ch2);
  48.             a(ch2) := t;
  49.         end loop;
  50.     end shuffle;
  51. begin
  52.     -- Using the CL arguments like this is not very elegant :-(
  53.     MasterKey := Integer'Value(Argument(1));
  54.     SBoxCount := Integer'Value(Argument(2));
  55.     SBoxCycle := Integer'Value(Argument(3));
  56.     Dynamic: declare
  57.         subtype SBoxRange is Natural range 1 .. SBoxCount;
  58.         SBoxTally : SBoxRange;
  59.         s_box, s_box_reverse : Array(SBoxRange) of SBox;
  60.     begin
  61.         if Masterkey < 0 then
  62.             Masterkey := -MasterKey;
  63.             Encrypt := False;
  64.         else
  65.             Encrypt := True;
  66.         end if;
  67.         random_ascii_setup(MasterKey);
  68.         for box in SBoxRange loop
  69.             for ch in ASCII loop
  70.                 s_box(box)(ch) := ch;
  71.             end loop;
  72.             shuffle(s_box(box));
  73.             for ch in ASCII loop
  74.                 s_box_reverse(box)(s_box(box)(ch)) := ch;
  75.             end loop;
  76.         end loop;
  77.         -- Using the CL arguments like this is not very elegant :-(
  78.         Open(File => infile, Mode => Char_IO.In_File, Name => Argument(4));
  79.         Create(File => outfile, Mode => Char_IO.Out_File, Name => Argument(5));
  80.         SBoxTally := SBoxRange'First;
  81.         while not End_Of_File(File => infile) loop
  82.             Read(File => infile, Item => temp);
  83.             if temp in ASCII then
  84.                 if Encrypt then
  85.                     Write(File => outfile, Item => s_box(SBoxTally)(temp));
  86.                 else
  87.                     Write(File => outfile, Item => s_box_reverse(SBoxTally)(temp));
  88.                 end if;
  89.             else
  90.                 -- Emit every out-of-range input char unchanged.
  91.                 Write(File => outfile, Item => temp);
  92.             end if;
  93.             if SBoxTally = SBoxRange'Last then
  94.                 SBoxTally := SBoxRange'First;
  95.             else
  96.                 SBoxTally := SBoxTally + 1;
  97.             end if;
  98.         end loop;
  99.         Close(File => outfile);
  100.         Close(File => infile);
  101.     end Dynamic;
  102. end Temp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement