//[TestMethod()]
        public void TestEnDecryption()
        {
            PlainFileKey generatedFK = Crypto.GenerateFileKey();

            byte[] plainFileBytes = Encoding.UTF8.GetBytes("Dinge die ich jetzt testen will.");

            // Encrypt text
            PlainDataContainer     plainDC   = new PlainDataContainer(plainFileBytes);
            FileEncryptionCipher   encCipher = Crypto.CreateFileEncryptionCipher(generatedFK);
            EncryptedDataContainer encryptedResult;

            using (MemoryStream ms = new MemoryStream()) {
                EncryptedDataContainer encryptedDC = encCipher.ProcessBytes(plainDC);
                ms.Write(encryptedDC.Content, 0, encryptedDC.Content.Length);
                encryptedDC = encCipher.DoFinal();
                ms.Write(encryptedDC.Content, 0, encryptedDC.Content.Length);
                encryptedResult = new EncryptedDataContainer(ms.ToArray(), encryptedDC.Tag);
            }
            generatedFK.Tag = Convert.ToBase64String(encryptedResult.Tag);

            // Decrypt text
            FileDecryptionCipher decCipher = Crypto.CreateFileDecryptionCipher(generatedFK);
            PlainDataContainer   decryptedResult;

            using (MemoryStream ms = new MemoryStream()) {
                PlainDataContainer decryptedDC = decCipher.ProcessBytes(new EncryptedDataContainer(encryptedResult.Content, null));
                ms.Write(decryptedDC.Content, 0, decryptedDC.Content.Length);
                decryptedDC = decCipher.DoFinal(new EncryptedDataContainer(null, Convert.FromBase64String(generatedFK.Tag)));
                ms.Write(decryptedDC.Content, 0, decryptedDC.Content.Length);
                decryptedResult = new PlainDataContainer(ms.ToArray());
            }

            System.Diagnostics.Debug.WriteLine("Result: " + Encoding.UTF8.GetString(decryptedResult.Content));
        }
示例#2
0
        public void Encrypted_RunSync_ProcessBytesIOError()
        {
            // ARRANGE
            Node node = FactoryNode.Node;

            node.Size = 1024;
            byte[] expected = new byte[node.Size.Value];
            new Random().NextBytes(expected);
            IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock();
            Stream s = new MemoryStream();
            FileDownloadCallbackMock callback = new FileDownloadCallbackMock();
            EncFileDownload          f        = new EncFileDownload(c, "id1", node, s, FactoryUser.UserPrivateKey);

            f.AddFileDownloadCallback(callback);
            Mock.Arrange(() => c.Builder.PostFileDownload(Arg.AnyLong)).Returns(FactoryRestSharp.PostFileDownloadMock(2354));
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiDownloadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostDownloadToken, 0))
            .Returns(FactoryNode.ApiDownloadToken);
            Mock.Arrange(() => c.NodesImpl.GetEncryptedFileKey(Arg.AnyLong)).Returns(FactoryFile.EncryptedFileKey);
            FileDecryptionCipher cipher = Mock.Create <FileDecryptionCipher>();

            Mock.Arrange(() => Crypto.Sdk.Crypto.DecryptFileKey(Arg.IsAny <EncryptedFileKey>(), Arg.IsAny <UserPrivateKey>(), Arg.AnyString))
            .Returns(FactoryFile.PlainFileKey);
            Mock.Arrange(() => Crypto.Sdk.Crypto.CreateFileDecryptionCipher(Arg.IsAny <PlainFileKey>())).Returns(cipher);
            Mock.NonPublic.Arrange <byte[]>(f, "DownloadChunk", ArgExpr.IsAny <Uri>(), ArgExpr.IsAny <long>(), ArgExpr.IsAny <long>()).Returns(expected);
            Mock.Arrange(() => cipher.ProcessBytes(Arg.IsAny <EncryptedDataContainer>())).Throws(new IOException("Error"));
            Mock.Arrange(() => cipher.DoFinal(Arg.IsAny <EncryptedDataContainer>())).Returns(new PlainDataContainer(new byte[0]));

            // ACT
            Assert.Throws <DracoonFileIOException>(() => f.RunSync());
            s.Close();
        }
        private void TestDecryptProcessArguments(EncryptedDataContainer edc)
        {
            PlainFileKey         pfk       = TestUtilities.ReadTestResource <PlainFileKey>(TestResources.plain_file_key);
            FileDecryptionCipher decCipher = Crypto.CreateFileDecryptionCipher(pfk);

            decCipher.ProcessBytes(edc);
        }
        public void TestDecryptMultiBlock_Success()
        {
            PlainFileKey pfk = TestUtilities.ReadTestResource <PlainFileKey>(TestResources.plain_file_key);

            byte[] ft  = Convert.FromBase64String(pfk.Tag);
            byte[] efc = Convert.FromBase64String(TestResources.enc_file);
            byte[] pfc = Convert.FromBase64String(TestResources.plain_file);

            FileDecryptionCipher decryptCipher = Crypto.CreateFileDecryptionCipher(pfk);

            using (MemoryStream output = new MemoryStream()) {
                using (MemoryStream input = new MemoryStream(efc)) {
                    byte[] buffer = new byte[16];
                    int    bytesRead;
                    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        byte[] blockBytes = new byte[bytesRead];
                        Array.Copy(buffer, blockBytes, bytesRead);
                        PlainDataContainer currentPdc = decryptCipher.ProcessBytes(new EncryptedDataContainer(blockBytes, null));
                        output.Write(currentPdc.Content, 0, currentPdc.Content.Length);
                    }
                }
                PlainDataContainer testPdc = decryptCipher.DoFinal(new EncryptedDataContainer(null, ft));
                output.Write(testPdc.Content, 0, testPdc.Content.Length);
                byte[] testPfc = output.ToArray();
                CollectionAssert.AreEqual(pfc, testPfc, "File content does not match!");
            }
        }
        private PlainDataContainer TestDecryptSingleBlock(PlainFileKey pfk, EncryptedDataContainer edc)
        {
            FileDecryptionCipher decryptCipher = Crypto.CreateFileDecryptionCipher(pfk);

            using (MemoryStream ms = new MemoryStream()) {
                PlainDataContainer pdc = decryptCipher.ProcessBytes(new EncryptedDataContainer(edc.Content, null));
                ms.Write(pdc.Content, 0, pdc.Content.Length);
                pdc = decryptCipher.DoFinal(new EncryptedDataContainer(null, edc.Tag));
                ms.Write(pdc.Content, 0, pdc.Content.Length);
                return(new PlainDataContainer(ms.ToArray()));
            }
        }
示例#6
0
        public void Encrypted_RunSync_Success()
        {
            // ARRANGE
            Node node = FactoryNode.Node;

            node.Size = 1024;
            byte[] expected = new byte[node.Size.Value];
            new Random().NextBytes(expected);
            IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock();
            Stream s = new MemoryStream();
            FileDownloadCallbackMock callback = new FileDownloadCallbackMock();
            EncFileDownload          f        = new EncFileDownload(c, "id1", node, s, FactoryUser.UserPrivateKey);

            f.AddFileDownloadCallback(callback);
            Mock.Arrange(() => c.Builder.PostFileDownload(Arg.AnyLong)).Returns(FactoryRestSharp.PostFileDownloadMock(2354)).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiDownloadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostDownloadToken, 0))
            .Returns(FactoryNode.ApiDownloadToken).Occurs(1);
            Mock.Arrange(() => c.NodesImpl.GetEncryptedFileKey(Arg.AnyLong)).Returns(FactoryFile.EncryptedFileKey).Occurs(1);
            FileDecryptionCipher cipher = Mock.Create <FileDecryptionCipher>();

            Mock.Arrange(() => Crypto.Sdk.Crypto.DecryptFileKey(Arg.IsAny <EncryptedFileKey>(), Arg.IsAny <UserPrivateKey>(), Arg.AnyString))
            .Returns(FactoryFile.PlainFileKey).Occurs(1);
            Mock.Arrange(() => Crypto.Sdk.Crypto.CreateFileDecryptionCipher(Arg.IsAny <PlainFileKey>())).Returns(cipher).Occurs(1);
            Mock.NonPublic.Arrange <byte[]>(f, "DownloadChunk", ArgExpr.IsAny <Uri>(), ArgExpr.IsAny <long>(), ArgExpr.IsAny <long>()).Returns(expected);
            Mock.Arrange(() => cipher.ProcessBytes(Arg.IsAny <EncryptedDataContainer>())).Returns(new PlainDataContainer(expected)).OccursAtLeast(1);
            Mock.Arrange(() => cipher.DoFinal(Arg.IsAny <EncryptedDataContainer>())).Returns(new PlainDataContainer(new byte[0])).Occurs(1);

            Mock.Arrange(() => callback.OnStarted(Arg.AnyString)).Occurs(1);
            Mock.Arrange(() => callback.OnRunning(Arg.AnyString, Arg.AnyLong, Arg.AnyLong)).OccursAtLeast(1);
            Mock.Arrange(() => callback.OnFinished(Arg.AnyString)).Occurs(1);

            // ACT
            f.RunSync();
            s.Position = 0;
            byte[] actual = new byte[expected.Length];
            s.Read(actual, 0, expected.Length);
            s.Close();

            // ASSERT
            Assert.Equal(expected, actual);
            Mock.Assert(() => Crypto.Sdk.Crypto.DecryptFileKey(Arg.IsAny <EncryptedFileKey>(), Arg.IsAny <UserPrivateKey>(), Arg.AnyString));
            Mock.Assert(() => Crypto.Sdk.Crypto.CreateFileDecryptionCipher(Arg.IsAny <PlainFileKey>()));
            Mock.Assert(callback);
            Mock.Assert(cipher);
            Mock.Assert(c.Builder);
            Mock.Assert(c.Executor);
            Mock.Assert(c.NodesImpl);
        }
        /// <summary>
        /// Decrypts some bytes.
        /// </summary>
        /// <param name="fileKey">The file key to use.</param>
        ///  <param name="data">The encrypted bytes.</param>
        /// <returns>Plain bytes.</returns>
        private static byte[] DecryptData(PlainFileKey fileKey, byte[] data)
        {
            // !!! This method is an example for decryption. Like the method 'encryptData(...)', it uses
            //     byte array streams for input and output. However, any kind of stream
            //     (e.g. FileInputStream) could be used here.

            FileDecryptionCipher cipher = Crypto.CreateFileDecryptionCipher(fileKey);

            byte[] decData;
            using (MemoryStream is2 = new MemoryStream(data))
            {
                using (MemoryStream os = new MemoryStream())
                {
                    byte[] buffer = new byte[BLOCK_SIZE];
                    int    count;

                    try
                    {
                        PlainDataContainer pDataContainer;

                        // Decrypt blocks
                        while ((count = is2.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            byte[] eData = createByteArray(buffer, count);
                            pDataContainer = cipher.ProcessBytes(new EncryptedDataContainer(eData, null));
                            os.Write(pDataContainer.Content, 0, pDataContainer.Content.Length);
                        }

                        // Complete decryption
                        byte[] tag = Convert.FromBase64String(fileKey.Tag);
                        pDataContainer = cipher.DoFinal(new EncryptedDataContainer(null, tag));
                        os.Write(pDataContainer.Content, 0, pDataContainer.Content.Length);

                        decData = os.ToArray();
                    }
                    catch (IOException e)
                    {
                        throw new Exception("Error while reading/writing data!", e);
                    }
                    catch (CryptoException e)
                    {
                        throw new Exception("Error while decrypting data!", e);
                    }
                }
            }
            return(decData);
        }