Пример #1
0
        public static bool ConfirmEcbMode(int blockSize)
        {
            var b = SecureRng.GenerateRandomBytes(blockSize);
            var duplicateBlocks = new List <byte>();

            duplicateBlocks.AddRange(b);
            duplicateBlocks.AddRange(b);
            var encrypted = EncryptionOracle(duplicateBlocks.ToArray());

            return(AesEcb.IsEcbEncrypted(encrypted, blockSize));
        }
Пример #2
0
        private static byte[] TransformData(byte[] data)
        {
            var prefixLength = Rand.Next(MinRandBytesCount, MaxRandBytesCount + 1);
            var suffixLength = Rand.Next(MinRandBytesCount, MaxRandBytesCount + 1);
            var prefix       = SecureRng.GenerateRandomBytes(prefixLength);
            var suffix       = SecureRng.GenerateRandomBytes(suffixLength);
            var input        = new List <byte>();

            input.AddRange(prefix);
            input.AddRange(data);
            input.AddRange(suffix);
            return(input.ToArray());
        }
Пример #3
0
        public void TestChallenge11()
        {
            var key  = SecureRng.GenerateRandomBytes(16);
            var data = new List <byte>();

            for (int i = 0; i < EcbCbcEncryptionOracleInputLengthBytes; i++)
            {
                data.Add(0x00);
            }
            for (int i = 0; i < Challenge11TestCount; i++)
            {
                var guessedMode =
                    AesEcbCbcDetectionOracle.EncryptEcbOrCbc(data.ToArray(), out var actualMode);
                Assert.AreEqual(actualMode, guessedMode);
            }
        }
Пример #4
0
        private static byte[] EncryptionOracle(byte[] data)
        {
            if (_randomPrefix == null || _randomPrefix.Length == 0)
            {
                var r            = new Random();
                var prefixLength = r.Next(1, MaxPrefixLength + 1);
                _randomPrefix = SecureRng.GenerateRandomBytes(prefixLength);
            }
            var combined = new List <byte>();

            combined.AddRange(_randomPrefix);
            combined.AddRange(data);
            combined.AddRange(Convert.FromBase64String(EncodedSuffix));
            var padded = PaddingUtil.Pad(combined.ToArray(), BlockSizeBytes);

            return(AesEcb.Encrypt(Key, padded));
        }
Пример #5
0
        public static CipherMode EncryptEcbOrCbc(byte[] data, out CipherMode actualMode)
        {
            actualMode = Rand.Next(0, 2) == 0 ? CipherMode.ECB : CipherMode.CBC;
            var key = SecureRng.GenerateRandomBytes(KeySizeBytes);

            // Add prefix and suffix to data
            data = TransformData(data);
            byte[] encrypted;
            if (actualMode == CipherMode.ECB)
            {
                var padded = PaddingUtil.Pad(data, BlockSizeBytes);
                encrypted = AesEcb.Encrypt(key, padded);
            }
            else
            {
                var iv = SecureRng.GenerateRandomBytes(BlockSizeBytes);
                encrypted = AesCbc.Encrypt(key, iv, data);
            }
            var guessedMode = AesEcb.IsEcbEncrypted(encrypted, BlockSizeBytes)
                ? CipherMode.ECB
                : CipherMode.CBC;

            return(guessedMode);
        }