Ejemplo n.º 1
0
        public async Task <MemoryStream> DownloadFile(ViewFile file, string receiverEmail, RSAParameters receiverKey)
        {
            using (var encryptedStream = new MemoryStream())
                using (var jsonFileData = new MemoryStream())
                {
                    var decryptedStream = new MemoryStream();
                    await DownloadFile(file.JsonFileId, jsonFileData);

                    var fileData = FileDataHelpers.DownloadFileData(jsonFileData, receiverEmail);

                    await DownloadFile(file.Id, encryptedStream);

                    var senderKey = new RSAParameters
                    {
                        Exponent = fileData.SenderPublicKey.Expontent,
                        Modulus  = fileData.SenderPublicKey.Modulus
                    };
                    var isValid = SafeCloudFile.VerifySignedFile(encryptedStream, fileData.FileSign, senderKey);
                    if (!isValid)
                    {
                        throw new Exception("Invalid file sign!");
                    }

                    SafeCloudFile.Decrypt(encryptedStream, decryptedStream, fileData.UserKeys[receiverEmail], receiverKey);

                    return(decryptedStream);
                }
        }
Ejemplo n.º 2
0
        public async Task <SharedDownload> DownloadShared(string encryptedFileLink, string jsonFileLink, string receiverEmail, RSAParameters receiverKey)
        {
            using (var jsonStream = new MemoryStream())
                using (var encryptedStream = new MemoryStream())
                {
                    var decryptedStream = new MemoryStream();
                    await GetFileStream(jsonFileLink, jsonStream);

                    var fileData = FileDataHelpers.DownloadFileData(jsonStream, receiverEmail);

                    await GetFileStream(encryptedFileLink, encryptedStream);

                    var senderKey = new RSAParameters
                    {
                        Exponent = fileData.SenderPublicKey.Expontent,
                        Modulus  = fileData.SenderPublicKey.Modulus
                    };
                    var isValid = SafeCloudFile.VerifySignedFile(encryptedStream, fileData.FileSign, senderKey);
                    if (!isValid)
                    {
                        throw new Exception("Invalid file sign!");
                    }

                    SafeCloudFile.Decrypt(encryptedStream, decryptedStream, fileData.UserKeys[receiverEmail], receiverKey);

                    jsonStream.Close();
                    encryptedStream.Close();

                    return(new SharedDownload(decryptedStream, fileData.FileName));
                }
        }
Ejemplo n.º 3
0
        public async Task <MemoryStream> DownloadFile(ViewFile file, string receiverEmail, RSAParameters receiverKey)
        {
            var decryptedStream = new MemoryStream();
            var jsonFileData    = await _graphServiceClient.Me.Drive.Special.AppRoot
                                  .ItemWithPath(file.Name.Split('.').First() + ".json")
                                  .Content.Request().GetAsync() as MemoryStream;

            if (jsonFileData == null)
            {
                throw new Exception("Error while downloading json file!");
            }

            var fileData = FileDataHelpers.DownloadFileData(jsonFileData, receiverEmail);

            var encryptedStream = await _graphServiceClient.Me.Drive.Special.AppRoot.ItemWithPath(file.Name).Content
                                  .Request()
                                  .GetAsync() as MemoryStream;

            if (encryptedStream == null)
            {
                throw new Exception("Error while downloading encrypted file!");
            }

            var senderKey = new RSAParameters
            {
                Exponent = fileData.SenderPublicKey.Expontent,
                Modulus  = fileData.SenderPublicKey.Modulus
            };

            encryptedStream.Position = 0;
            var isValid = SafeCloudFile.VerifySignedFile(encryptedStream, fileData.FileSign, senderKey);

            if (!isValid)
            {
                throw new Exception("Invalid file sign!");
            }

            SafeCloudFile.Decrypt(encryptedStream, decryptedStream, fileData.UserKeys[receiverEmail], receiverKey);

            jsonFileData.Close();
            encryptedStream.Close();

            return(decryptedStream);
        }
        public async Task <MemoryStream> DownloadFile(ViewFile file, string receiverEmail, RSAParameters receiverKey)
        {
            using (var encryptedStream = new MemoryStream())
                using (var jsonFileData = new MemoryStream())
                {
                    var decryptedStream          = new MemoryStream();
                    var jsonFileDownloadProgress =
                        await _driveService.Files.Get(file.JsonFileId).DownloadAsync(jsonFileData);

                    if (jsonFileDownloadProgress.Status != DownloadStatus.Completed)
                    {
                        throw new Exception("Error while downloading json file!");
                    }

                    var fileData = FileDataHelpers.DownloadFileData(jsonFileData, receiverEmail);

                    var encryptedFileDownloadProgress =
                        await _driveService.Files.Get(file.Id).DownloadAsync(encryptedStream);

                    if (encryptedFileDownloadProgress.Status != DownloadStatus.Completed)
                    {
                        throw new Exception("Error while downloading encrypted file!");
                    }

                    var senderKey = new RSAParameters
                    {
                        Exponent = fileData.SenderPublicKey.Expontent,
                        Modulus  = fileData.SenderPublicKey.Modulus
                    };
                    encryptedStream.Position = 0;
                    var isValid = SafeCloudFile.VerifySignedFile(encryptedStream, fileData.FileSign, senderKey);
                    if (!isValid)
                    {
                        throw new Exception("Invalid file sign!");
                    }

                    SafeCloudFile.Decrypt(encryptedStream, decryptedStream, fileData.UserKeys[receiverEmail], receiverKey);

                    return(decryptedStream);
                }
        }