/// <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."); } }