Пример #1
0
        /// <summary>
        /// Opens users encrypted file. File is first decrypted and stored as a temporary file in a <em>temporary folder</em> after which is opened in a default app for that file set on the computer.
        /// </summary>
        /// <param name="pathOnEfs">The name of the encrypted file including files path and encrypted name with .at extension.</param>
        /// <param name="ownerPublicKey">Public RSA key from the file owner used to check files signature.</param>
        public void OpenFile(string pathOnEfs, RSAParameters ownerPublicKey)
        {
            var encryptedFile = new EncryptedFile(pathOnEfs.Substring(pathOnEfs.LastIndexOf('\\') + 1).Split('.')[0]);
            var originalFile  = encryptedFile.Decrypt(File.ReadAllBytes(pathOnEfs), currentUser.Id, currentUser.PrivateKey, ownerPublicKey);

            var tempFilePath = Path.GetTempPath() + "Enigma-" + Guid.NewGuid().ToString() + "." + originalFile.FileExtension;

            if (CanItBeStored(originalFile.FileContent.Length, tempFilePath.Substring(0, 2)))
            {
                // create a temporary file
                CreateFile(originalFile.FileContent, tempFilePath);

                // override existing encrypted file
                var encryptedFileUpdated = encryptedFile.Flush();
                if (CanItBeStored(encryptedFileUpdated.Length, pathOnEfs.Substring(0, 2)))
                {
                    CreateFile(encryptedFileUpdated, pathOnEfs);
                }

                var startInfo = new ProcessStartInfo(tempFilePath);
                Process.Start(startInfo);
            }
            else
            {
                throw new Exception("Insufficient storage available. File can't be read.");
            }
        }
Пример #2
0
        /// <summary>
        /// Downloads selected encrypted file. File is first decrypted after which is stored on the specified path on file system.
        /// </summary>
        /// <param name="pathOnEfs">The name of the file to downloaded.</param>
        /// <param name="pathOnFs">Path on the file system where the file will be stored.</param>
        /// <param name="ownerPublicKey">Public RSA key from the file owner used to check files signature.</param>
        public void Download(string pathOnEfs, string pathOnFs, RSAParameters ownerPublicKey)
        {
            var encryptedFile = new EncryptedFile(pathOnEfs.Substring(pathOnEfs.LastIndexOf('\\') + 1).Split('.')[0]);
            var originalFile  = encryptedFile.Decrypt(File.ReadAllBytes(pathOnEfs), currentUser.Id, currentUser.PrivateKey, ownerPublicKey);

            if (CanItBeStored(originalFile.FileContent.Length, pathOnFs.Substring(0, 2)))
            {
                // write a new unencrypted file
                CreateFile(originalFile.FileContent, pathOnFs /* + "\\" + originalFile.GetOriginalFileFullName()*/);

                // override existing encrypted file
                CreateFile(encryptedFile.Flush(), pathOnEfs);
            }
            else
            {
                throw new Exception("Insufficient storage available. File can't be downloaded.");
            }
        }