Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
enigma
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { static String mMessage = "Esperar algo mejor para el futuro me parece una fantasia. La naturaleza humana no cambia."; public class Rotor { static int mPrime = 122949823; int[] mEncode, mDecode; int mMax, mPosition; public Rotor(int pMax) { mPosition = 0; mMax = pMax; mEncode = new int[mMax]; mDecode = new int[mMax]; InitRotor(); } void InitRotor() { List<int> oIndexes = new List<int>(); for (int i=0; i<mMax; i++) oIndexes.Add(i); for (int i=0; i<mMax; i++) { int iIndex = (mPrime % (mMax+1-i)) - 1; mEncode[i] = oIndexes[iIndex] - i; mDecode[oIndexes[iIndex]] = i - oIndexes[iIndex]; oIndexes.RemoveAt(iIndex); } /* Console.WriteLine("Init ------------------"); for (int i=0; i<mMax; i++) { Console.Write(mEncode[i]); Console.Write(" "); Console.WriteLine(mDecode[i]); } Console.WriteLine("------------------"); */ } public int GetEncodedInt(int iVal) { int iIndex = (mPosition + iVal) % mMax; int iResult = (iVal + mEncode[iIndex] + mMax) % mMax; return iResult; } public int GetDecodedInt(int iVal) { int iIndex = (mPosition + iVal) % mMax; int iResult = (iVal + mDecode[iIndex] + mMax) % mMax; return iResult; } public bool Rotate() { mPosition++; if (mPosition >= mMax) { mPosition = mPosition % mMax; return true; } return false; } public void SetPosition(int pPos) { mPosition = pPos; } } public class EnigmaMachine { Rotor[] mRotors; int mRotorMax = 5; int mValueMax = 300; public EnigmaMachine() { mRotors = new Rotor[mRotorMax]; for (int i=0; i<mRotorMax; i++) { mRotors[i] = new Rotor(mValueMax); } } public void Reset() { for (int i=0; i<mRotorMax; i++) mRotors[i].SetPosition(0); } void Rotate() { bool bFullTurn = true; for (int i=0; i<mRotorMax && bFullTurn; i++) bFullTurn = mRotors[i].Rotate(); } public int Encode(int pVal) { int iValue = pVal; for (int i=0; i<mRotorMax; i++) iValue = mRotors[i].GetEncodedInt(iValue); Rotate(); return iValue; } public int Decode(int pVal) { int iValue = pVal; for (int i=0; i<mRotorMax; i++) iValue = mRotors[mRotorMax-1-i].GetDecodedInt(iValue); Rotate(); return iValue; } } public static void Main(string[] args) { int n = 12; Rotor oRotor = new Rotor(n); int[] iCoded = new int[n]; for (int i=0; i<n; i++) { iCoded[i] = oRotor.GetEncodedInt(7); // iCoded[i] = oRotor.GetEncodedInt(7); Console.WriteLine(iCoded[i]); } Console.WriteLine("----------"); oRotor.SetPosition(0); for (int i=0; i<n; i++) { int iVal = oRotor.GetDecodedInt(iCoded[i]); Console.WriteLine(iVal); } n = 30000; EnigmaMachine oEnigma = new EnigmaMachine(); iCoded = new int[n]; for (int i=0; i<n; i++) { iCoded[i] = oEnigma.Encode(145); } Console.WriteLine("----------"); oEnigma.Reset(); for (int i=0; i<n; i++) { if (oEnigma.Decode(iCoded[i]) != 145) { i = n; Console.WriteLine("FAILED"); return; } } Console.WriteLine("success"); } } }
run
|
edit
|
history
|
help
0
How do I calculate someone's age in C#?
SpawnController
My First Hello world
Triangle
Finding a Substring in a Main String
test1
Uninitialized-variable example.
pascal
CodeAdvent
dfg