Exemple #1
0
        public static byte[] Decrypt()
        {
            // Determine block size of encryption oracle
            var blockSize = FindBlockSize();

            // Ensure encryption oracle is using ECB mode
            if (!ConfirmEcbMode(blockSize))
            {
                throw new InvalidOperationException("Encryption oracle is not encrypting in ECB mode.");
            }
            var knownBytes = new List <byte>();
            var blockIndex = 0;

            while (true)
            {
                var b = DecryptNextByte(blockSize, knownBytes, blockIndex);
                if (b == -1)
                {
                    break;
                }
                knownBytes.Add((byte)b);
                if (knownBytes.Count % blockSize == 0)
                {
                    blockIndex++;
                }
            }
            var unpadded = PaddingUtil.RemovePad(knownBytes.ToArray());

            return(unpadded);
        }
Exemple #2
0
        public static byte[] Decrypt()
        {
            // Determine block size of encryption oracle
            var blockSize = FindBlockSize();
            // Determine prefix size of encryption oracle
            var prefixSize = FindPrefixSize(blockSize);

            var knownBytes = new List <byte>();
            var blockIndex = (int)Math.Ceiling((double)prefixSize / blockSize);

            while (true)
            {
                var b = DecryptNextByte(blockSize, prefixSize, knownBytes, blockIndex);
                if (b == -1)
                {
                    break;
                }
                knownBytes.Add((byte)b);
                if (knownBytes.Count % blockSize == 0)
                {
                    blockIndex++;
                }
            }
            var unpadded = PaddingUtil.RemovePad(knownBytes.ToArray());

            return(unpadded);
        }
        public static Dictionary <string, string> DecryptProfile(byte[] encrypted)
        {
            var decrypted = AesEcb.Decrypt(Key, encrypted);
            var encoded   = PaddingUtil.RemovePad(decrypted);
            var decoded   = KvpParser.Decode(System.Text.Encoding.ASCII.GetString(encoded));

            return(decoded);
        }
Exemple #4
0
        public void Challenge7()
        {
            var keyStr = "YELLOW SUBMARINE";
            var key    = System.Text.Encoding.ASCII.GetBytes(keyStr);
            var data   = Convert.FromBase64String(Set1Data.Challenge7Input);

            var decrypted = AesEcb.Decrypt(key, data);

            decrypted = PaddingUtil.RemovePad(decrypted);
            var clearText = System.Text.Encoding.ASCII.GetString(decrypted);

            Assert.AreEqual(Set1Data.Challenge7Solution, clearText);
        }
Exemple #5
0
        public static byte[] Decrypt(out int randIndex)
        {
            var c      = EncryptionOracle(out randIndex);
            var blocks = c.Chunks(BlockSizeBytes);

            blocks.Insert(0, Iv);

            var knownBytes = new List <byte>();

            for (int i = 0; i < blocks.Count - 1; i++)
            {
                var previousBlock = blocks[i];
                var targetBlock   = blocks[i + 1];
                var decrypted     = Decrypt(targetBlock, previousBlock);
                knownBytes.AddRange(decrypted);
            }

            return(PaddingUtil.RemovePad(knownBytes.ToArray()));
        }
Exemple #6
0
        public static byte[] Decrypt(byte[] key, byte[] iv, byte[] data, bool removePadding = true)
        {
            var blocks        = data.Chunks(BlockSizeBytes);
            var clearText     = new byte[data.Length];
            int index         = 0;
            var previousBlock = iv;

            foreach (var block in blocks)
            {
                var blockArray = block.ToArray();
                var decrypted  = AesEcb.Decrypt(key, blockArray);
                var xord       = XorUtil.Xor(decrypted, previousBlock);
                Array.Copy(xord, 0, clearText, index, BlockSizeBytes);
                previousBlock = blockArray;
                index        += BlockSizeBytes;
            }
            if (removePadding)
            {
                return(PaddingUtil.RemovePad(clearText));
            }
            return(clearText);
        }