示例#1
0
        private void Decrypt(object obj)
        {
            _isProcessInProgress = true;
            UIServices.SetBusyState();

            _logger.Info("start decryption");
            _logger.Info("source: " + SourceFilepath);
            _logger.Info("destination: " + TargetFilepath);
            _logger.Info("sender: " + ContactName);

            if (CheckSoureAndTargetPath())
            {
                if (IsVaildFile())
                {
                    MasterLogin login = _database.GetAll <MasterLogin>().FirstOrDefault();

                    if (login != null)
                    {
                        bool isDecryped = _cryptographyService.DecryptFile(_sourceFilepath, _targetFilepath,
                                                                           login.PrivateKey);

                        if (isDecryped)
                        {
                            _messenger.Send(new DecryptionSuccsessMsg());
                        }
                        else
                        {
                            _logger.Error("file can not decrypt!");
                            _messenger.Send(new DecryptionFailedMsg());
                        }

                        _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("login is null!");
                    }
                }
                else
                {
                    _logger.Error("file is not vaild for encryption! maybe encrpytion?");
                    _messenger.Send(new DecryptionFailedMsg());
                }
            }
            else
            {
                _logger.Error("source and/or target path are not vaild!");
                _messenger.Send(new SourceTargetInvaildMsg());
            }

            _disableForEncryption = true;
            _isProcessInProgress  = false;
        }
        public void DecryptFile()
        {
            //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);

            //decrypt
            var destinationFilename2 = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
            var result = _cryptographyService.DecryptFile(destinationFilename + ".sfs", destinationFilename2,
                                                          _cryptographyService.GetPrivateKeyAsXml());

            Assert.IsTrue(result);

            //check content
            var fileContent3 = File.ReadAllText(destinationFilename2 + "..tmp");

            Assert.IsTrue(fileContent == fileContent3);

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