public void Should_return_empty_array_if_size_is_zero()
        {
            var generator = new Rc4RandomGenerator(_key);

            var output = generator.GetRandomBytes(0);
            Assert.Equal(0U, output.Length);
        }
        public void Should_generate_different_bytes_after_each_request()
        {
            var generator = new Rc4RandomGenerator(_key);
            var first32 = generator.GetRandomBytes(32);
            var second32 = generator.GetRandomBytes(32);

            Assert.False(CryptographicBuffer
                .Compare(first32, second32));
        }
        public void Should_generate_random_bytes()
        {
            var generator = new Rc4RandomGenerator(_key);
            var output = generator.GetRandomBytes(32);

            Assert.Equal(
                "aoKkKycKsRrRIVOhRrH/afAz6lx+EaHBm6icFs9v2Kg=",
                CryptographicBuffer.EncodeToBase64String(output));
        }
        public void Should_generate_same_bytes_if_same_key()
        {
            var generator = new Rc4RandomGenerator(_key);
            var first32 = generator.GetRandomBytes(32);

            var generator2 = new Rc4RandomGenerator(_key);
            var second32 = generator2.GetRandomBytes(32);

            Assert.True(CryptographicBuffer
                .Compare(first32, second32));
        }
Exemple #5
0
        private static void Decrypt(FileHeaders headers, XDocument doc)
        {
            var protectedStrings = doc.Descendants("Entry")
                .SelectMany(x => x.Elements("String"))
                .Select(x => x.Element("Value"))
                .Where(x =>
                {
                    var protect = x.Attribute("Protected");
                    return protect != null && (bool)protect;
                });

            IRandomGenerator generator;
            switch (headers.RandomAlgorithm)
            {
                case CrsAlgorithm.ArcFourVariant:
                    generator = new Rc4RandomGenerator(
                        headers.ProtectedStreamKey);
                    break;

                default:
                    generator = new Salsa20RandomGenerator(
                        headers.ProtectedStreamKey);
                    break;
            }

            foreach (var protectedString in protectedStrings)
            {
                var encrypted = Convert.FromBase64String(
                    protectedString.Value);
                var length = encrypted.Length;

                var padding = generator.GetRandomBytes(length);

                for (var i = 0U; i < length; i++)
                    encrypted[i] ^= padding.GetByte(i);

                protectedString.Value = Encoding.UTF8
                    .GetString(encrypted, 0, length);
            }
        }