Beispiel #1
0
 public void Test_Cipher_With_0_Shift()
 {
     sut = new ShiftCipher(0);
     Assert.AreEqual('A', sut.Encipher('A'));
     Assert.AreEqual('M', sut.Encipher('M'));
     Assert.AreEqual('Z', sut.Encipher('Z'));
 }
Beispiel #2
0
 public void Test_Cipher_With_Negative_1_Shift()
 {
     sut = new ShiftCipher(-1);
     Assert.AreEqual('Z', sut.Encipher('A'));
     Assert.AreEqual('P', sut.Encipher('Q'));
     Assert.AreEqual('Y', sut.Encipher('Z'));
 }
Beispiel #3
0
 public void Test_Cipher_With_11_Shift()
 {
     sut = new ShiftCipher(11);
     Assert.AreEqual('L', sut.Encipher('A'));
     Assert.AreEqual('A', sut.Encipher('P'));
     Assert.AreEqual('K', sut.Encipher('Z'));
 }
        public void EncryptNoKeyTest()
        {
            ShiftCipher myCipher = new ShiftCipher();

            TestCtor(myCipher);
            myCipher.Encrypt(TEST_STR);
        }
        public void DecryptNoKeyTest()
        {
            ShiftCipher myCipher = new ShiftCipher();

            TestCtor(myCipher);
            myCipher.Decrypt(Convert.ToBase64String(Encoding.ASCII.GetBytes(TEST_STR)));
        }
Beispiel #6
0
 public void Test_Cipher_With_Negative_64_Shift()
 {
     // Cipher of -64 is a -12 Cipher is a +14 Cipher
     sut = new ShiftCipher(-64);
     Assert.AreEqual('O', sut.Encipher('A'));
     Assert.AreEqual('S', sut.Encipher('E'));
     Assert.AreEqual('H', sut.Encipher('T'));
 }
Beispiel #7
0
 public void Test_Cipher_With_Negative_15_Shift()
 {
     // Cipher of -15 is a +11 Cipher
     sut = new ShiftCipher(-15);
     Assert.AreEqual('L', sut.Encipher('A'));
     Assert.AreEqual('P', sut.Encipher('E'));
     Assert.AreEqual('E', sut.Encipher('T'));
 }
Beispiel #8
0
        private void InitializeRotor()
        {
            var dialSettingInt = EnigmaHelper.GetInt(dialSetting);

            shiftCipher1       = new ShiftCipher(dialSettingInt - pinSetting);
            shiftCipher2       = new ShiftCipher(dialSettingInt);
            substitutionCipher = new SubstitutionCipher(this.rotorMapping);
        }
Beispiel #9
0
 public void Test_Cipher_With_26_Shift()
 {
     // Cipher of 26 is a 0 Cipher
     sut = new ShiftCipher(26);
     Assert.AreEqual('A', sut.Encipher('A'));
     Assert.AreEqual('E', sut.Encipher('E'));
     Assert.AreEqual('T', sut.Encipher('T'));
 }
Beispiel #10
0
 public void Test_Cipher_With_73_Shift()
 {
     // Cipher of 73 is a +21 Cipher
     sut = new ShiftCipher(73);
     Assert.AreEqual('V', sut.Encipher('A'));
     Assert.AreEqual('B', sut.Encipher('G'));
     Assert.AreEqual('S', sut.Encipher('X'));
 }
        public void EncryptFileTest()
        {
            ShiftCipher shiftCipher = new ShiftCipher();

            shiftCipher.GenKey();
            shiftCipher.Encrypt(BASE_FILE, ENC_FILE);
            TestFileEnc();
        }
        public void EncryptStrTest()
        {
            ShiftCipher myCipher = new ShiftCipher();

            TestCtor(myCipher);
            myCipher.GenKey();
            TestStrEnc(myCipher, myCipher.Encrypt(TEST_STR));
        }
        public void SetKeyTest()
        {
            ShiftCipher shiftCipher = new ShiftCipher();

            TestCtor(shiftCipher);
            shiftCipher.SetKey(TEST_SHIFT);
            Assert.AreEqual(TEST_SHIFT, shiftCipher.GetKey(), "Incorrect key returned");
        }
        public void GenKeyTest()
        {
            ShiftCipher shiftCipher = new ShiftCipher();

            TestCtor(shiftCipher);
            shiftCipher.GenKey();
            Assert.AreNotEqual(0, shiftCipher.GetKey(), "key was not intialized");
        }
        public void Rot13()
        {
            ShiftCipher cipher = new ShiftCipher();

            const string plaintext  = "Why did the chicken cross the road?";
            const string ciphertext = "Jul qvq gur puvpxra pebff gur ebnq?";

            Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, WellKnownShiftCipherKeys.Rot13));
            Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, WellKnownShiftCipherKeys.Rot13));
        }
        public void Caesar()
        {
            ShiftCipher cipher = new ShiftCipher();

            const string plaintext  = "The quick brown fox jumps over the lazy dog.";
            const string ciphertext = "Wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj.";

            Assert.AreEqual(ciphertext, cipher.Encrypt(plaintext, WellKnownShiftCipherKeys.Caesar));
            Assert.AreEqual(plaintext, cipher.Decrypt(ciphertext, WellKnownShiftCipherKeys.Caesar));
        }
        public void DecryptStrTest()
        {
            ShiftCipher myCipher = new ShiftCipher();

            TestCtor(myCipher);
            myCipher.GenKey();
            string encStr = myCipher.Encrypt(TEST_STR);

            TestStrEnc(myCipher, encStr);
            TestStrDec(encStr, myCipher.Decrypt(encStr));
        }
        public void ROT_13_TEST()
        {
            ShiftCipher myCipher = new ShiftCipher(ShiftCipher.MODE.ROT13);

            Assert.AreEqual(ShiftCipher.MODE.ROT13, myCipher.GetMode(), "default shift mode not set correctly");
            Assert.AreEqual(0, myCipher.GetKey(), "Incorrect defualt shift amount found");
            myCipher.GenKey();
            Assert.AreEqual(ROT_13_SHIFT, myCipher.GetKey(), "Incorrect shift amount found");
            string encStr = myCipher.Encrypt(TEST_STR);

            TestStrEnc(myCipher, encStr);
            TestStrDec(encStr, myCipher.Decrypt(encStr));
        }
        /// <summary>
        /// Computes every possible transformations of the input text.
        /// </summary>
        /// <param name="text">The input text to transform.</param>
        /// <param name="charset">Charset of the input text.</param>
        /// <returns></returns>
        public static IReadOnlyDictionary <int, string> Analyze(string text, string charset = Charsets.English)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (charset == null)
            {
                throw new ArgumentNullException(nameof(charset));
            }


            var cipher = new ShiftCipher(charset);

            return(Enumerable.Range(0, charset.Length)
                   .ToDictionary(k => k, k => cipher.Encrypt(text, k))
                   );
        }
 public ActionResult Index(string inputString, string encrypt)
 {
     if (encrypt == "encrypt")
     {
         var shiftAmount = 3;
         ShiftCipher shifter = new ShiftCipher();
         var outputString = shifter.Encrypt(inputString, shiftAmount);
         ViewBag.outputString = outputString;
         return View();
     }
     else
     {
         var shiftAmount = -3;
         ShiftCipher shifter = new ShiftCipher();
         var outputString = shifter.Decrypt(inputString, shiftAmount);
         ViewBag.outputString = outputString;
         return View();
     }
 }
 static void TestStrEnc(ShiftCipher cipher, string encStr)
 {
     Assert.AreNotEqual(0, cipher.GetKey(), "Key was not Generated");
     Assert.AreNotEqual(TEST_STR, encStr, "String did not encrypt");
 }
 static void TestCtor(ShiftCipher cipher)
 {
     Assert.AreEqual(ShiftCipher.MODE.SHIFT, cipher.GetMode(), "default shift mode not set correctly");
     Assert.AreEqual(0, cipher.GetKey(), "Incorrect defualt shift amount found");
 }