예제 #1
0
        public void Should_return_empty_array_if_size_is_zero()
        {
            var generator = new Salsa20RandomGenerator(_key);

            var output = generator.GetRandomBytes(0);

            Assert.Equal(0U, output.Length);
        }
예제 #2
0
        public void Should_generate_random_bytes()
        {
            var generator = new Salsa20RandomGenerator(_key);
            var output    = generator.GetRandomBytes(32);

            Assert.Equal(
                "PgWWBZItJSLD8I7lZSjIFXXId8R743waCzmYYrAKDUo=",
                CryptographicBuffer.EncodeToBase64String(output));
        }
예제 #3
0
        public void Should_generate_different_bytes_after_each_request()
        {
            var generator = new Salsa20RandomGenerator(_key);
            var first32   = generator.GetRandomBytes(32);
            var second32  = generator.GetRandomBytes(32);

            Assert.False(CryptographicBuffer
                         .Compare(first32, second32));
        }
예제 #4
0
        public void Should_generate_same_bytes_if_same_key()
        {
            var generator = new Salsa20RandomGenerator(_key);
            var first32   = generator.GetRandomBytes(32);

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

            Assert.True(CryptographicBuffer
                        .Compare(first32, second32));
        }
예제 #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);
            }
        }