Beispiel #1
0
        public void EncryptAndDecryptTest()
        {
            //Tworzymy instancje RSA w celu pozyskania kluczy
            using (var rsa = new RSACryptoServiceProvider())
                using (var encryptedStream = new MemoryStream())
                    using (var decryptedStream = new MemoryStream())
                        using (var memoryStream = new MemoryStream())
                        {
                            var expected  = "Testowy ciag znakow";
                            var receivers = new List <Receiver>
                            {
                                new Receiver("*****@*****.**", rsa.ExportParameters(false))
                            };

                            using (var streamWriter = new StreamWriter(memoryStream))
                                using (var streamReader = new StreamReader(decryptedStream))
                                {
                                    streamWriter.Write(expected);
                                    streamWriter.Flush();

                                    var userKeys = SafeCloudFile.Encrypt(memoryStream, encryptedStream, receivers);
                                    SafeCloudFile.Decrypt(encryptedStream, decryptedStream, userKeys["*****@*****.**"],
                                                          rsa.ExportParameters(true));

                                    decryptedStream.Position = 0;
                                    var actual = streamReader.ReadToEnd();

                                    Assert.AreEqual(expected, actual);
                                }
                        }
        }
Beispiel #2
0
        public void SignAndVerifyTest()
        {
            using (var rsa = new RSACryptoServiceProvider())
                using (var encryptedStream = new MemoryStream())
                    using (var memoryStream = new MemoryStream())
                    {
                        var expected  = "Testowy ciag znakow";
                        var receivers = new List <Receiver>
                        {
                            new Receiver("*****@*****.**", rsa.ExportParameters(false))
                        };

                        using (var streamWriter = new StreamWriter(memoryStream))
                        {
                            streamWriter.Write(expected);
                            streamWriter.Flush();

                            SafeCloudFile.Encrypt(memoryStream, encryptedStream, receivers);

                            var signedData = SafeCloudFile.SignFile(encryptedStream, rsa.ExportParameters(true));
                            var isValid    =
                                SafeCloudFile.VerifySignedFile(encryptedStream, signedData, rsa.ExportParameters(false));

                            Assert.AreEqual(true, isValid);
                        }
                    }
        }
Beispiel #3
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);
                }
        }
Beispiel #4
0
        public async Task <ShareLinksDto> UploadFile(string fileName, string fileExtension, FileStream fileStream, List <Receiver> receivers, RSAParameters senderKey, bool isShared)
        {
            using (var inputStream = new MemoryStream())
                using (var outputStream = new MemoryStream())
                {
                    fileStream.CopyTo(inputStream);

                    var userKeys = SafeCloudFile.Encrypt(inputStream, outputStream, receivers);
                    inputStream.Dispose();

                    var fileSign = SafeCloudFile.SignFile(outputStream, senderKey);
                    var fileData = new FileData(fileSign, userKeys,
                                                new SenderPublicKey(senderKey.Exponent, senderKey.Modulus), fileName + fileExtension);
                    var fileDataJson = JsonConvert.SerializeObject(fileData);

                    var uploadJsonFileDto = await UploadJson(fileName, fileDataJson.GenerateStream(), isShared);

                    if (uploadJsonFileDto == null)
                    {
                        throw new Exception("Error while uploading file!");
                    }

                    var uploadFile = await UploadFile(fileName, fileExtension, outputStream, uploadJsonFileDto.Id, isShared);

                    if (uploadFile == null)
                    {
                        throw new Exception("Error while uploading file!");
                    }

                    return(new ShareLinksDto(uploadFile.ShareLink, uploadJsonFileDto.ShareLink));
                }
        }
Beispiel #5
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));
                }
        }
        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);
                }
        }