Пример #1
0
        private void Encrypt(object obj)
        {
            _isProcessInProgress = true;
            UIServices.SetBusyState();

            _logger.Info("start encryption");
            _logger.Info("source: " + _sourceFilepath);
            _logger.Info("destination: " + _targetFilepath);
            _logger.Info("receipient: " + _contactName);

            if (CheckSoureAndTargetPath())
            {
                if (string.IsNullOrEmpty(ContactName))
                {
                    MasterLogin login = _database.GetAll <MasterLogin>().FirstOrDefault();

                    if (login != null)
                    {
                        _logger.Info("encrpyt file for myself");

                        _cryptographyService.EncryptFile(_sourceFilepath, _targetFilepath, login.PublicKey);
                        _messenger.Send(new EncryptionSuccsessMsg {
                            TargetPath = _targetFilepath + ".sfs"
                        });
                    }
                    else
                    {
                        _logger.Error("login is null!");
                    }
                }
                else
                {
                    _logger.Info("encrpyt file for: " + ContactName);

                    _cryptographyService.EncryptFile(_sourceFilepath, _targetFilepath, PublicKey);
                    _messenger.Send(new EncryptionSuccsessMsg {
                        TargetPath = _targetFilepath
                    });
                }

                _logger.Info("check if source file in temp directory and when it is, then delete");
                CheckSourceFileInTempDirectory();

                //reset inputs
                SourceFilepath = string.Empty;
                TargetFilepath = string.Empty;
                ContactName    = string.Empty;
            }
            else
            {
                _logger.Error("source and/or target path are not vaild!");
                _messenger.Send(new SourceTargetInvaildMsg());
            }

            _disableForEncryption = true;
            _isProcessInProgress  = false;
        }
        public void EncryptFile()
        {
            //create txt file
            const string fileContent = "hello world of encryption!";
            var          filePath    = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());

            File.WriteAllText(filePath, fileContent);

            _cryptographyService.AssignNewKeys();

            //encrypt
            var destinationFilename = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());

            _cryptographyService.EncryptFile(filePath, destinationFilename, _cryptographyService.GetPublicKey());

            //check content
            var fileContent2 = File.ReadAllText(destinationFilename + ".sfs");

            Assert.IsFalse(fileContent == fileContent2);

            File.Delete(filePath);
            File.Delete(destinationFilename + ".sfs");
        }