Ejemplo n.º 1
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
        }
Ejemplo n.º 2
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);
        }