Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Copyright (c) 2015 Mark R V Murray. All rights reserved.
- --
- with Ada.Command_line; use Ada.Command_Line;
- with Ada.Sequential_IO;
- procedure Temp is
- MasterKey : Integer;
- SBoxCount : Integer;
- SBoxCycle : Integer;
- Encrypt : Boolean;
- subtype ASCII is Character range ' ' .. '~';
- type SBox is Array(ASCII) of ASCII;
- package Char_IO is new Ada.Sequential_IO(Character); use Char_IO;
- infile, outfile : Char_IO.File_Type;
- -- PRNG numbers pulled off the interwebs
- X_n : Integer;
- a : Constant Integer := 1664525;
- c : Constant Integer := 1013904223;
- m : constant Integer := 2**30;
- temp : Character;
- procedure random_ascii_setup(seed : Integer) is
- begin
- X_n := seed;
- end random_ascii_setup;
- function random_ascii(lower, upper : ASCII) return ASCII is
- Mm : Integer;
- begin
- Mm := ASCII'Pos(upper) - ASCII'Pos(lower) + 1;
- X_n := (a*X_n + c) mod m;
- return ASCII'Val((X_n mod Mm) + ASCII'Pos(lower));
- end random_ascii;
- procedure shuffle(a : in out SBox) is
- t, ch2 : ASCII;
- begin
- for ch1 in reverse ASCII'Val(ASCII'Pos(a'First) + 1) .. a'Last loop
- ch2 := random_ascii(ASCII'First, ch1);
- t := a(ch1);
- a(ch1) := a(ch2);
- a(ch2) := t;
- end loop;
- end shuffle;
- begin
- -- Using the CL arguments like this is not very elegant :-(
- MasterKey := Integer'Value(Argument(1));
- SBoxCount := Integer'Value(Argument(2));
- SBoxCycle := Integer'Value(Argument(3));
- Dynamic: declare
- subtype SBoxRange is Natural range 1 .. SBoxCount;
- SBoxTally : SBoxRange;
- s_box, s_box_reverse : Array(SBoxRange) of SBox;
- begin
- if Masterkey < 0 then
- Masterkey := -MasterKey;
- Encrypt := False;
- else
- Encrypt := True;
- end if;
- random_ascii_setup(MasterKey);
- for box in SBoxRange loop
- for ch in ASCII loop
- s_box(box)(ch) := ch;
- end loop;
- shuffle(s_box(box));
- for ch in ASCII loop
- s_box_reverse(box)(s_box(box)(ch)) := ch;
- end loop;
- end loop;
- -- Using the CL arguments like this is not very elegant :-(
- Open(File => infile, Mode => Char_IO.In_File, Name => Argument(4));
- Create(File => outfile, Mode => Char_IO.Out_File, Name => Argument(5));
- SBoxTally := SBoxRange'First;
- while not End_Of_File(File => infile) loop
- Read(File => infile, Item => temp);
- if temp in ASCII then
- if Encrypt then
- Write(File => outfile, Item => s_box(SBoxTally)(temp));
- else
- Write(File => outfile, Item => s_box_reverse(SBoxTally)(temp));
- end if;
- else
- -- Emit every out-of-range input char unchanged.
- Write(File => outfile, Item => temp);
- end if;
- if SBoxTally = SBoxRange'Last then
- SBoxTally := SBoxRange'First;
- else
- SBoxTally := SBoxTally + 1;
- end if;
- end loop;
- Close(File => outfile);
- Close(File => infile);
- end Dynamic;
- end Temp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement