Пример #1
0
        public void AESinCBC_Encrypt_Decrypt_Test()  //CBC = Cipher Block Chaining
        {
            int BLOCKSIZE = 16;

            string OriginalMessage = "Now I rock a house party at the drop of a hat\nAnd I beat a body down with an aluminum bat";
            var    bytes           = MyConvert.TextToByteArray(OriginalMessage);
            var    PaddedBytes     = Pad.AddPkcs7(bytes, 128); //Pad the message to 128 bytes

            //The Key (16 bytes)
            string key      = "YELLOW SUBMARINE";
            string HexKey   = MyConvert.HexEncodePlainText(key);
            var    Keybytes = MyConvert.HexToByteArray(HexKey);

            //Initialization Vector (padded to BLOCKSIZE bytes)
            string IVHex   = Pad.PadHex(BLOCKSIZE * 2, "00");
            var    IVBytes = MyConvert.HexToByteArray(IVHex);

            //Encrypt
            var CiperBytes = MyCrypto.AES_CBC_Encrypt(PaddedBytes, Keybytes, IVBytes, BLOCKSIZE);

            //Decrypt
            var PlainBytes    = MyCrypto.AES_CBC_Decrypt(CiperBytes, Keybytes, IVBytes, BLOCKSIZE);
            var UnPaddedBytes = Pad.RemovePkcs7(PlainBytes); //Unpad the message
            var hexPlain      = MyConvert.BytesToHex(UnPaddedBytes);
            var PlainText     = MyConvert.HexToAscii(hexPlain);

            Assert.IsTrue(OriginalMessage == PlainText);
        }
Пример #2
0
        public void Implement_CBC_mode_Test()  //CBC = Cipher Block Chaining
        {
            var BLOCKSIZE = 16;

            //https://cryptopals.com/sets/2/challenges/10
            string str   = Util.GetFile(10);
            string Hex   = MyConvert.Base64ToHex(str);
            var    bytes = MyConvert.HexToByteArray(Hex);

            //Initialization Vector (padded to BLOCKSIZE bytes)
            string IVHex   = Pad.PadHex(BLOCKSIZE * 2, "00");
            var    IVBytes = MyConvert.HexToByteArray(IVHex);

            //The Key
            string key      = "YELLOW SUBMARINE";
            string HexKey   = MyConvert.HexEncodePlainText(key);
            var    Keybytes = MyConvert.HexToByteArray(HexKey);

            //Perform my version of CBC
            var decryptedBytes = MyCrypto.AES_CBC_Decrypt(bytes, Keybytes, IVBytes, BLOCKSIZE);
            var HexResult      = MyConvert.BytesToHex(decryptedBytes);
            var Plain          = MyConvert.HexToAscii(HexResult);

            Assert.IsTrue("I'm back and I'm ringin' " == Plain.Substring(0, 25));  //25 is at least two blocks (of16) so i know i have the algo correct
        }
Пример #3
0
        public void DetectAES_ECB_Test()  //ECB = Electronic Codebook
        {
            //https://cryptopals.com/sets/1/challenges/8
            string str        = Util.GetFile(8);
            var    Chunks     = Util.Split(str, 32);
            var    Duplicates = Chunks.GroupBy(x => x).Where(g => g.Count() > 1).Select(s => s.Key).ToList();

            var bytes = MyConvert.HexToByteArray(Duplicates.First());
            var Hex   = MyConvert.BytesToHex(bytes);
            var B64   = MyConvert.HexToBase64(Hex);

            Assert.IsTrue("CGSa9w3Ab0/V0tacdEzSg" == B64);
        }
Пример #4
0
        public void AESinECB_Test()  //ECB = Electronic Codebook
        {
            //https://cryptopals.com/sets/1/challenges/7
            string str   = Util.GetFile(7);
            string Hex   = MyConvert.Base64ToHex(str);
            var    bytes = MyConvert.HexToByteArray(Hex);

            string key      = "YELLOW SUBMARINE";
            string HexKey   = MyConvert.HexEncodePlainText(key);
            var    Keybytes = MyConvert.HexToByteArray(HexKey);

            var result    = MyCrypto.AES_ECB_Decrypt(bytes, Keybytes);
            var HexResult = MyConvert.BytesToHex(result);
            var Plain     = MyConvert.HexToAscii(HexResult);

            Assert.IsTrue("I'm back and I'm ringin' " == Plain.Substring(0, 25));
        }
Пример #5
0
        public void AESinECB_Encrypt_Decrypt_Test()  //ECB = Electronic Codebook
        {
            string OriginalMessage = "Now is the time for all good men to come to the aid of their country";
            var    bytes           = MyConvert.TextToByteArray(OriginalMessage);
            var    PaddedBytes     = Pad.AddPkcs7(bytes, 128); //Pad the message to 128 bytes

            //The Key (16 bytes)
            string key      = "YELLOW SUBMARINE";
            string HexKey   = MyConvert.HexEncodePlainText(key);
            var    Keybytes = MyConvert.HexToByteArray(HexKey);

            //Encrypt
            var CiperBytes = MyCrypto.AES_ECB_Encrypt(PaddedBytes, Keybytes);

            //Decrypt
            var PlainBytes    = MyCrypto.AES_ECB_Decrypt(CiperBytes, Keybytes);
            var UnPaddedBytes = Pad.RemovePkcs7(PlainBytes); //Unpad the message
            var hexPlain      = MyConvert.BytesToHex(UnPaddedBytes);
            var PlainText     = MyConvert.HexToAscii(hexPlain);

            Assert.IsTrue(OriginalMessage == PlainText);
        }