public void EncryptAndDecryptWithFilename()
        {
            #region Arrange

            var secret = Encryption.Random.CreateData(512 / 8);

            #endregion

            #region Act

            using (var input = File.OpenRead(this.InputFile))
                using (var output = File.Open(this.OutputFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    SymmetricEncryption.EncryptInternal(input, output, secret, new EncryptInternalParameter()
                    {
                        Filename = Guid.NewGuid().ToString()
                    });
                }

            Console.Out.WriteLine("Encrypted content: " + Convert.ToBase64String(File.ReadAllBytes(this.OutputFile)));

            using (var input = File.OpenRead(this.OutputFile))
                using (var output = File.Create(this.ResultFile))
                {
                    SymmetricEncryption.DecryptInternal(input, output, secret, null, new DecryptInternalParameter());
                }

            #endregion

            #region Assert

            Assert.That(File.ReadLines(this.InputFile), Is.EquivalentTo(File.ReadLines(this.ResultFile)));

            #endregion
        }
Exemple #2
0
        public void Tamper(SymmetricEncryptionTest.TamperEnum tamperEnum)
        {
            #region Arrange

            var secret = Encryption.Random.CreateData(512 / 8);

            using (var input = File.OpenRead(this.InputFile))
                using (var output = File.Create(this.OutputFile))
                {
                    SymmetricEncryption.EncryptInternal(input, output, secret);
                }

            #endregion

            #region Act

            switch (tamperEnum)
            {
            case SymmetricEncryptionTest.TamperEnum.AesKey:
                Assert.Fail("Not Implemented");
                break;

            case SymmetricEncryptionTest.TamperEnum.HmacHash:
                Assert.Fail("Not Implemented");
                //informationContainer.PublicInformation.HmacHash[0] ^= informationContainer.PublicInformation.HmacHash[0];
                break;

            case SymmetricEncryptionTest.TamperEnum.Iv:
                Assert.Fail("Not Implemented");
                // informationContainer.PublicInformation.IV[0] ^= informationContainer.PublicInformation.IV[0];
                break;

            case SymmetricEncryptionTest.TamperEnum.File:
                Assert.Fail("Not Implemented");
                break;

            case SymmetricEncryptionTest.TamperEnum.Nothing:
                break;
            }

            #endregion

            #region Assert

            Assert.Fail("Not Implemented");

            #endregion
        }
Exemple #3
0
        public static void Encrypt(Stream input, Stream output, EncryptionParameter parameter)
        {
            var secretKey = Random.CreateData(SymmetricEncryption.AesKeyLength + SymmetricEncryption.HmacKeyLength);

            var hybridFileInfo = EllipticCurveEncryptionInformation.Create(parameter.PublicKeys, secretKey);

            var internalParameter = new EncryptInternalParameter
            {
                Filename = parameter.Filename,
                PasswordDerivationSettings         = null,
                EllipticCurveEncryptionInformation = hybridFileInfo,
                Progress   = parameter.Progress,
                IsCanceled = parameter.IsCanceled
            };

            SymmetricEncryption.EncryptInternal(input, output, secretKey, internalParameter);
        }
        public void FileformatHelper_MemoryStream()
        {
            #region Arrange

            var secret = Encryption.Random.CreateData(512 / 8);
            var data   = Guid.NewGuid().ToByteArray();

            #endregion

            #region Act

            byte[] encryptedData = null;
            using (var input = new MemoryStream(data))
                using (var output = new MemoryStream())
                {
                    SymmetricEncryption.EncryptInternal(input, output, secret);
                    encryptedData = output.ToArray();
                }

            Console.Out.WriteLine("Encrypted content: " + Convert.ToBase64String(File.ReadAllBytes(this.OutputFile)));

            byte[] iv;
            byte[] hmac;
            using (var input = new MemoryStream(encryptedData))
            {
                iv   = RawFileAccessor.Read(input, RawFileAccessor.Field.InitializationVector);
                hmac = RawFileAccessor.Read(input, RawFileAccessor.Field.Hmac);
            }

            #endregion

            #region Assert

            Assert.That(iv, Is.Not.Null);
            Assert.That(hmac, Is.Not.Null);

            Assert.That(iv, Has.Length.EqualTo(128 / 8));
            Assert.That(hmac, Has.Length.EqualTo(512 / 8));

            Assert.That(iv, Has.Some.Not.EqualTo(0));
            Assert.That(hmac, Has.Some.Not.EqualTo(0));

            #endregion
        }
        public void FileformatHelper_FileStream()
        {
            #region Arrange

            var secret = Encryption.Random.CreateData(512 / 8);

            #endregion

            #region Act

            using (var input = File.OpenRead(this.InputFile))
                using (var output = File.Open(this.OutputFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    SymmetricEncryption.EncryptInternal(input, output, secret);
                }

            Console.Out.WriteLine("Encrypted content: " + Convert.ToBase64String(File.ReadAllBytes(this.OutputFile)));

            byte[] iv;
            byte[] hmac;
            using (var input = File.OpenRead(this.OutputFile))
            {
                iv   = RawFileAccessor.Read(input, RawFileAccessor.Field.InitializationVector);
                hmac = RawFileAccessor.Read(input, RawFileAccessor.Field.Hmac);
            }

            #endregion

            #region Assert

            Assert.That(iv, Is.Not.Null);
            Assert.That(hmac, Is.Not.Null);

            Assert.That(iv, Has.Length.EqualTo(128 / 8));
            Assert.That(hmac, Has.Length.EqualTo(512 / 8));

            Assert.That(iv, Has.Some.Not.EqualTo(0));
            Assert.That(hmac, Has.Some.Not.EqualTo(0));

            #endregion
        }
Exemple #6
0
        public void EncryptInternal_FileStream()
        {
            var data   = Guid.NewGuid().ToByteArray();
            var secret = Random.CreateData(512 / 8);

            using (var output = File.Open(this.OutputFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                SymmetricEncryption.EncryptInternal(new MemoryStream(data), output, secret);
            }

            var newStream = File.OpenRead(this.OutputFile);

            Assert.That(RawFileAccessor.Verify(newStream));

            var iv = RawFileAccessor.Read(newStream, RawFileAccessor.Field.InitializationVector);

            Assert.That(iv, Has.Some.Not.EqualTo(0), "InitializationVector");

            var hmac = RawFileAccessor.Read(newStream, RawFileAccessor.Field.Hmac);

            Assert.That(hmac, Has.Some.Not.EqualTo(0), "Hmac");
        }
Exemple #7
0
        public void EncryptInternal_MemoryStream()
        {
            var data   = Guid.NewGuid().ToByteArray();
            var secret = Random.CreateData(512 / 8);

            MemoryStream newStream;

            using (var encrypted = new MemoryStream())
            {
                SymmetricEncryption.EncryptInternal(new MemoryStream(data), encrypted, secret);
                newStream = new MemoryStream(encrypted.ToArray());
            }

            Assert.That(RawFileAccessor.Verify(newStream), "verify file signature");

            var iv = RawFileAccessor.Read(newStream, RawFileAccessor.Field.InitializationVector);

            Assert.That(iv, Has.Some.Not.EqualTo(0), "InitializationVector");

            var hmac = RawFileAccessor.Read(newStream, RawFileAccessor.Field.Hmac);

            Assert.That(hmac, Has.Some.Not.EqualTo(0), "Hmac");
        }
        public void EncryptAndDecrypt_MemoryStream()
        {
            #region Arrange

            var secret = Encryption.Random.CreateData(512 / 8);
            var data   = Encryption.Random.CreateData(512);

            #endregion

            #region Act

            MemoryStream output;
            using (var input = new MemoryStream(data))
                using (output = new MemoryStream())
                {
                    SymmetricEncryption.EncryptInternal(input, output, secret);
                }

            var encryptedData = output.ToArray();

            Console.Out.WriteLine("Encrypted content: " + Convert.ToBase64String(encryptedData));

            MemoryStream decrypted;
            using (var input = new MemoryStream(encryptedData))
                using (decrypted = new MemoryStream())
                {
                    SymmetricEncryption.DecryptInternal(input, decrypted, secret, null, new DecryptInternalParameter());
                }

            #endregion

            #region Assert

            Assert.That(data, Is.EquivalentTo(decrypted.ToArray()));

            #endregion
        }