Exemplo n.º 1
0
        /// <summary>
        /// Uploads selected file. File is first encrypted after which is stored on the specified path on encrypted file system.
        /// </summary>
        /// <param name="originalFile">Original, unencrypted file.</param>
        /// <param name="pathOnEfs">Path on the encrypted file system where the file will be stored.</param>
        /// <param name="algorithmNameSignature">Name of the algorithm used for file encryption.</param>
        /// <param name="hashAlgorithmName">Name of the hashing algorithm used to create a file signature.</param>
        /// <returns>Encrypted name of the file.</returns>
        public string Upload(OriginalFile originalFile, string pathOnEfs, string algorithmNameSignature, string hashAlgorithmName)
        {
            CertificateCheck("You cannot import any new files.");

            string encryptedName;

            if (originalFile.FileSize > 1_900_000_000)
            {
                throw new Exception("File can't be larger than 2 GB.");
            }

            if (CanItBeStored(originalFile.FileSize))
            {
                var encryptedFile    = new EncryptedFile(originalFile.GetOriginalFileFullName(), (uint)currentUser.Id, algorithmNameSignature, hashAlgorithmName, currentUser.PublicKey, currentUser.PrivateKey);
                var encryptedFileRaw = encryptedFile.Encrypt(originalFile, currentUser.Id, currentUser.PrivateKey);
                encryptedName = encryptedFile.EncryptedName;

                // var userPrivateKey = currentUser.GetPrivateKey(privateKeyPath, password);

                if (CanItBeStored(encryptedFileRaw.Length))
                {
                    CreateFile(encryptedFileRaw, pathOnEfs + "\\" + encryptedFile.GetEncryptedFileFullName());
                }
                else
                {
                    throw new Exception("Insufficient storage available. File can't be uploaded.");
                }
            }
            else
            {
                throw new Exception("Insufficient storage available. File can't be uploaded.");
            }

            return(encryptedName);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Shares a file with other specific user on EnigmaEfs.
        /// </summary>
        /// <param name="pathOnEfs">The name of the shared file.</param>
        /// <param name="shareUser">User you are sharing a file with.</param>
        public void Share(string pathOnEfs, UserInformation shareUser)
        {
            CertificateCheck($"You cannot share files with {shareUser.Username}.");

            var encryptedFile           = new EncryptedFile(pathOnEfs.Substring(pathOnEfs.LastIndexOf('\\') + 1).Split('.')[0]);
            var updatedEncryptedFileRaw = encryptedFile.Share(File.ReadAllBytes(pathOnEfs), currentUser.Id, shareUser.Id, currentUser.PrivateKey, shareUser.PublicKey);

            if (CanItBeStored(updatedEncryptedFileRaw.Length))
            {
                CreateFile(updatedEncryptedFileRaw, SharedDir + "\\" + encryptedFile.GetEncryptedFileFullName());

                // When first sharing a file from user folder to shared folder.
                if (pathOnEfs.Substring(0, pathOnEfs.LastIndexOf('\\')) != SharedDir)
                {
                    DeleteFile(pathOnEfs);
                }
            }
            else
            {
                throw new Exception("Insufficient storage available. File can't be updated.");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Unshares a file with specific user on EnigmaEfs.
        /// </summary>
        /// <param name="pathOnEfs">The name of the shared file.</param>
        /// <param name="userId">Unique user identifier from the database.</param>
        public void Unshare(string pathOnEfs, int userId)
        {
            var encryptedFile           = new EncryptedFile(pathOnEfs.Substring(pathOnEfs.LastIndexOf('\\') + 1).Split('.')[0]);
            var updatedEncryptedFileRaw = encryptedFile.Unshare(File.ReadAllBytes(pathOnEfs), currentUser.Id, userId, out var numberOfSharedUsers);

            if (CanItBeStored(updatedEncryptedFileRaw.Length))
            {
                // File is moved from shared folder to user's folder.
                if (numberOfSharedUsers != 1)
                {
                    CreateFile(updatedEncryptedFileRaw, SharedDir + "\\" + encryptedFile.GetEncryptedFileFullName());
                }
                // If no other user other than file owner can access a file, it's moved from shared folder to file owner's folder.
                else
                {
                    CreateFile(updatedEncryptedFileRaw, RootDir + "\\" + UserDir + "\\" + encryptedFile.GetEncryptedFileFullName());
                    DeleteFile(pathOnEfs);
                }
            }
            else
            {
                throw new Exception("Insufficient storage available. File can't be updated.");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Unshares a file with all shared users on EnigmaEfs.
        /// </summary>
        /// <param name="pathOnEfs"></param>
        public void Unshare(string pathOnEfs)
        {
            var encryptedFile           = new EncryptedFile(pathOnEfs.Substring(pathOnEfs.LastIndexOf('\\') + 1).Split('.')[0]);
            var updatedEncryptedFileRaw = encryptedFile.Unshare(File.ReadAllBytes(pathOnEfs), currentUser.Id);

            if (CanItBeStored(updatedEncryptedFileRaw.Length))
            {
                CreateFile(updatedEncryptedFileRaw, RootDir + "\\" + UserDir + "\\" + encryptedFile.GetEncryptedFileFullName());
                DeleteFile(pathOnEfs);
            }
            else
            {
                throw new Exception("Insufficient storage available. File can't be updated.");
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Updates selected encrypted file with a specified unencrypted file.
        /// </summary>
        /// <param name="pathOnEfs">The name of the file to update.</param>
        /// <param name="updateFile">Updated, unencrypted file.</param>
        public void Update(string pathOnEfs, OriginalFile updateFile)
        {
            CertificateCheck("You cannot update files.");

            if (updateFile.FileSize > 2_000_000_000)
            {
                throw new Exception("File can't be larger than 2 GB.");
            }

            if (!CanItBeStored(updateFile.FileSize))
            {
                throw new Exception("Insufficient storage available. File can't be uploaded.");
            }

            var encryptedFile           = new EncryptedFile(pathOnEfs.Substring(pathOnEfs.LastIndexOf('\\') + 1).Split('.')[0]);
            var updatedEncryptedFileRaw = encryptedFile.Update(updateFile, File.ReadAllBytes(pathOnEfs), currentUser.Id, currentUser.PrivateKey);

            // Name update is always necessary because file's IV has been changed and possibly a new file has a different name.
            encryptedFile.NameEncryption(updateFile.GetOriginalFileFullName(),
                                         new AesAlgorithm(((SecurityDescriptor)encryptedFile.Headers[1]).GetKey((int)currentUser.Id, currentUser.PrivateKey),
                                                          ((SecurityDescriptor)encryptedFile.Headers[1]).IV, "OFB"));

            // delete the old encrypted file
            DeleteFile(pathOnEfs);

            if (CanItBeStored(updatedEncryptedFileRaw.Length))
            {
                CreateFile(updatedEncryptedFileRaw, pathOnEfs.Substring(0, pathOnEfs.LastIndexOf('\\')) + "\\" + encryptedFile.GetEncryptedFileFullName());
            }
            else
            {
                throw new Exception("Insufficient storage available. File can't be updated.");
            }
        }