Skip to content

Commit

Permalink
GUI added
Browse files Browse the repository at this point in the history
  • Loading branch information
randomCharacter committed Jul 13, 2018
1 parent 5522214 commit aaa283e
Show file tree
Hide file tree
Showing 14 changed files with 2,309 additions and 12 deletions.
10 changes: 9 additions & 1 deletion Enigma.sln
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Enigma", "Enigma.csproj", "{125D44EE-648C-4A3B-B092-F19D66B73351}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnigmaGUI", "EnigmaGUI\EnigmaGUI.csproj", "{90422A7F-25DD-4551-BC05-8BC6ACDF22EC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Enigma", "Enigma\Enigma.csproj", "{125D44EE-648C-4A3B-B092-F19D66B73351}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{90422A7F-25DD-4551-BC05-8BC6ACDF22EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90422A7F-25DD-4551-BC05-8BC6ACDF22EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90422A7F-25DD-4551-BC05-8BC6ACDF22EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90422A7F-25DD-4551-BC05-8BC6ACDF22EC}.Release|Any CPU.Build.0 = Release|Any CPU
{125D44EE-648C-4A3B-B092-F19D66B73351}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{125D44EE-648C-4A3B-B092-F19D66B73351}.Debug|Any CPU.Build.0 = Debug|Any CPU
{125D44EE-648C-4A3B-B092-F19D66B73351}.Release|Any CPU.ActiveCfg = Release|Any CPU
{125D44EE-648C-4A3B-B092-F19D66B73351}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion Enigma.cs → Enigma/Enigma.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Enigma
namespace Enigma
{
/// <summary>
/// Enigma machine
Expand Down
File renamed without changes.
File renamed without changes.
23 changes: 13 additions & 10 deletions Program.cs → Enigma/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System;
using System.Linq;

namespace Enigma
{
/// <summary>
/// Main class
/// </summary>
internal static class Program
public static class Program
{
// Error codes
private const int InvalidArguments = 1;
Expand Down Expand Up @@ -167,7 +167,7 @@ private static bool ErrorFound(string[] args)
{
if (args.Length != 5 && args.Length != 6)
{
Console.WriteLine(System.AppDomain.CurrentDomain.FriendlyName + " reflector" + " leftRotor" + " centerRotor" + " rightRotor" + " positions" + " [plugboard]");
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + " reflector" + " leftRotor" + " centerRotor" + " rightRotor" + " positions" + " [plugboard]");
Console.WriteLine("reflector = { B, C }");
Console.WriteLine("leftRotor, centerRotor, rightRotor = { I, II, III, IV, V, VI, VII, VIII }");
Console.WriteLine("positions: start positions for reflector and all the rotors (example 'ABCD'");
Expand Down Expand Up @@ -279,26 +279,29 @@ public static void Main(string[] args)
var machine = new Enigma(_plugboard, left, center, right, reflector);
machine.SetPositions(posRef, posLeft, posCenter, posRight);

var s = Console.ReadLine();
var message = Console.ReadLine();
var messageLetters = "";

if (s == null)
if (message == null)
{
Console.WriteLine("Invalid input.");
Environment.Exit(InvalidInput);
}

foreach (var ch in s)
foreach (var ch in message)
{
if (char.IsUpper(ch))
{
continue;
messageLetters += ch;
}
else if (char.IsLower(ch))
{
messageLetters += char.ToUpper(ch);
}
Console.WriteLine("Invalid character in input (only capital letters allowed).");
Environment.Exit(InvalidInput);
}


var res = s.Aggregate("", (current, t) => current + (char) (machine.Convert(t - 'A') + 'A'));
var res = messageLetters.Aggregate("", (current, t) => current + (char) (machine.Convert(t - 'A') + 'A'));
Console.WriteLine(res);
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
80 changes: 80 additions & 0 deletions EnigmaGUI/EnigmaGUI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{90422A7F-25DD-4551-BC05-8BC6ACDF22EC}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EnigmaGUI</RootNamespace>
<AssemblyName>EnigmaGUI</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="atk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<HintPath>..\..\..\..\..\usr\lib\mono\gac\atk-sharp\2.12.0.0__35e10195dab3c99f\atk-sharp.dll</HintPath>
</Reference>
<Reference Include="gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<HintPath>..\..\..\..\..\usr\lib\mono\gac\gdk-sharp\2.12.0.0__35e10195dab3c99f\gdk-sharp.dll</HintPath>
</Reference>
<Reference Include="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<HintPath>..\..\..\..\..\usr\lib\mono\gac\glade-sharp\2.12.0.0__35e10195dab3c99f\glade-sharp.dll</HintPath>
</Reference>
<Reference Include="glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<HintPath>..\..\..\..\..\usr\lib\mono\gac\glib-sharp\2.12.0.0__35e10195dab3c99f\glib-sharp.dll</HintPath>
</Reference>
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<HintPath>..\..\..\..\..\usr\lib\mono\gac\gtk-sharp\2.12.0.0__35e10195dab3c99f\gtk-sharp.dll</HintPath>
</Reference>
<Reference Include="Mono.Posix" />
<Reference Include="pango-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<HintPath>..\..\..\..\..\usr\lib\mono\gac\pango-sharp\2.12.0.0__35e10195dab3c99f\pango-sharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="gtk-gui\generated.cs" />
<Compile Include="gtk-gui\MainWindow.cs" />
<Compile Include="MainWindow.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Enigma\Enigma.csproj">
<Project>{125d44ee-648c-4a3b-b092-f19d66b73351}</Project>
<Name>Enigma</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
223 changes: 223 additions & 0 deletions EnigmaGUI/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using System;
using System.Linq;
using Enigma;
using Gtk;

public partial class MainWindow : Gtk.Window
{
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
}

private void OnDeleteEvent(object sender, DeleteEventArgs a)
{
Application.Quit();
a.RetVal = true;
}

private static Rotor _rotor1;
private static Rotor _rotor2;
private static Rotor _rotor3;
private static Rotor _rotor4;
private static Rotor _rotor5;
private static Rotor _rotor6;
private static Rotor _rotor7;
private static Rotor _rotor8;

private static Reflector _reflectorB;
private static Reflector _reflectorC;

private static Plugboard _plugboard;

/// <summary>
/// Creates Enigma motors
/// </summary>
private static void BuildRotors()
{
// Rotor 1
int[] rotor1Cipher = { 4, 10, 12, 5, 11, 6, 3, 16, 21, 25, 13, 19, 14, 22, 24, 7, 23, 20, 18, 15, 0, 8, 1, 17, 2, 9 };
int[] rotor1Notches = { 16 };
_rotor1 = new Rotor(rotor1Cipher, rotor1Notches);
// Rotor 2
int[] rotor2Cipher = { 0, 9, 3, 10, 18, 8, 17, 20, 23, 1, 11, 7, 22, 19, 12, 2, 16, 6, 25, 13, 15, 24, 5, 21, 14, 4 };
int[] rotor2Notches = { 4 };
_rotor2 = new Rotor(rotor2Cipher, rotor2Notches);
// Rotor 3
int[] rotor3Cipher = { 1, 3, 5, 7, 9, 11, 2, 15, 17, 19, 23, 21, 25, 13, 24, 4, 8, 22, 6, 0, 10, 12, 20, 18, 16, 14 };
int[] rotor3Notches = { 21 };
_rotor3 = new Rotor(rotor3Cipher, rotor3Notches);
// Rotor 4
int[] rotor4Cipher = { 4, 18, 14, 21, 15, 25, 9, 0, 24, 16, 20, 8, 17, 7, 23, 11, 13, 5, 19, 6, 10, 3, 2, 12, 22, 1 };
int[] rotor4Notches = { 9 };
_rotor4 = new Rotor(rotor4Cipher, rotor4Notches);
// Rotor 5
int[] rotor5Cipher = { 21, 25, 1, 17, 6, 8, 19, 24, 20, 15, 18, 3, 13, 7, 11, 23, 0, 22, 12, 9, 16, 14, 5, 4, 2, 10 };
int[] rotor5Notches = { 12, 25 };
_rotor5 = new Rotor(rotor5Cipher, rotor5Notches);
// Rotor 6
int[] rotor6Cipher = { 9, 15, 6, 21, 14, 20, 12, 5, 24, 16, 1, 4, 13, 7, 25, 17, 3, 10, 0, 18, 23, 11, 8, 2, 19, 22 };
int[] rotor6Notches = { 12, 25 };
_rotor6 = new Rotor(rotor6Cipher, rotor6Notches);
// Rotor 7
int[] rotor7Cipher = { 13, 25, 9, 7, 6, 17, 2, 23, 12, 24, 18, 22, 1, 14, 20, 5, 0, 8, 21, 11, 15, 4, 10, 16, 3, 19 };
int[] rotor7Notches = { 12, 25 };
_rotor7 = new Rotor(rotor7Cipher, rotor7Notches);
// Rotor 8
int[] rotor8Cipher = { 5, 10, 16, 7, 19, 11, 23, 14, 2, 1, 9, 18, 15, 3, 25, 17, 0, 12, 4, 22, 13, 8, 20, 24, 6, 21 };
int[] rotor8Notches = { 12, 25 };
_rotor8 = new Rotor(rotor8Cipher, rotor8Notches);
}

/// <summary>
/// Creates enigma Reflectors
/// </summary>
private static void BuildReflectors()
{
int[] cipherB = { 24, 17, 20, 7, 16, 18, 11, 3, 15, 23, 13, 6, 14, 10, 12, 8, 4, 1, 5, 25, 2, 22, 21, 9, 0, 19 };
_reflectorB = new Reflector(cipherB);
int[] cipherC = { 5, 21, 15, 9, 8, 0, 14, 24, 4, 3, 17, 25, 23, 22, 6, 2, 19, 10, 20, 16, 18, 1, 13, 12, 7, 11 };
_reflectorC = new Reflector(cipherC);
}

/// <summary>
/// Creates Engima plugboard from cipher.
/// </summary>
/// <param name="cipher">Cipher that defines plugboard connections.</param>
private static void BuildPlugboard(int[] cipher = null)
{
int[] defaultCipher = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };
_plugboard = new Plugboard(cipher ?? defaultCipher);
}

/// <summary>
/// Assigns motor with given code.
/// Valid values: 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII'.
/// </summary>
/// <param name="code">Rotor code.</param>
/// <returns>Rotor with given code.</returns>
private static Rotor AssignRotor(string code)
{
switch (code)
{
case "I":
return _rotor1;
case "II":
return _rotor2;
case "III":
return _rotor3;
case "IV":
return _rotor4;
case "V":
return _rotor5;
case "VI":
return _rotor6;
case "VII":
return _rotor7;
case "VIII":
return _rotor8;
default:
return null;
}
}

/// <summary>
/// Assigns reflector with given code.
/// Valid values: 'B', 'C'
/// </summary>
/// <param name="code">Reflector code.</param>
/// <returns>Reflector with given code.</returns>
private static Reflector AssignReflector(String code)
{
switch (code)
{
case "-B-":
return _reflectorB;
case "-C-":
return _reflectorC;
default:
return null;
}
}

/// <summary>
/// Builds plugboard cipher from given string.
/// </summary>
/// <param name="code">String value of cipher.</param>
/// <returns>Array value of cipher.</returns>
private static int[] BuildPlugboardCipher(String code)
{
var cipher = new int[code.Length];
for (var i = 0; i < code.Length; i++)
{
cipher[i] = code[i] - 'A';
}

return cipher;
}

protected void SelectPlugboard(object sender, EventArgs e)
{

var value = ((ComboBox)sender).Name;
var key = ((ComboBox)sender).ActiveText;
foreach (var box in plugboard_table)
{
if (box is ComboBox)
{
var combo = box as ComboBox;
if (combo.Name == key)
{
combo.Active = value[0] - 'A';
}

}
}
}

protected string GetPlugboardString()
{
ComboBox[] boxes = { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z };
return boxes.Aggregate("", (current, box) => current + (box.ActiveText ?? box.Name));
}

protected void ButtonClick(object sender, EventArgs e)
{
BuildRotors();
var left = AssignRotor(rotorLeftType.ActiveText);
var center = AssignRotor(rotorCenterType.ActiveText);
var right = AssignRotor(rotorRightType.ActiveText);

BuildReflectors();
var reflector = AssignReflector(reflectorType.ActiveText);

var plugboardCipher = BuildPlugboardCipher(GetPlugboardString());
BuildPlugboard(plugboardCipher);

var posRef = reflectorPosition.Active;
var posLeft = rotorLeftPosotion.Active;
var posCenter = rotorCenterPosition.Active;
var posRight = rotorRightPosition.Active;

var machine = new Enigma.Enigma(_plugboard, left, center, right, reflector);
machine.SetPositions(posRef, posLeft, posCenter, posRight);

var message = inputText.Buffer.Text;
var messageLetters = "";

foreach (var ch in message)
{
if (char.IsUpper(ch))
{
messageLetters += ch;
}
else if (char.IsLower(ch))
{
messageLetters += char.ToUpper(ch);
}
}

var res = messageLetters.Aggregate("", (current, t) => current + (char) (machine.Convert(t - 'A') + 'A'));
outputText.Buffer.Text = res;
}
}
Loading

0 comments on commit aaa283e

Please sign in to comment.