public void EncryptedFile() { using (OfficeCryptoStream s = OfficeCryptoStream.Open("test.xlsx")) { s.Password = "******"; s.Save(); } }
void ChangePassword(String oldPassword, String newPassword) { using (OfficeCryptoStream s = OfficeCryptoStream.Open(TestFile, oldPassword)) { s.Password = newPassword; s.Save(); } }
public static void AccessEncryptedFile() { using (OfficeCryptoStream stream = OfficeCryptoStream.Open("a.xlsx", "password")) { DoStuff(stream); stream.Save(); // Skip this line if you don't want to save/encrypt } }
public static void AccessPlaintextFile() { using (OfficeCryptoStream stream = OfficeCryptoStream.Open("a.xlsx")) { DoStuff(stream); stream.Save(); } }
void CreateTestWorkbook(String password) { using (OfficeCryptoStream s = OfficeCryptoStream.Open(TestFile)) { s.Password = password; s.Save(); } }
public void TryOpen() { CreateTestWorkbook("foo"); OfficeCryptoStream ocs; Assert.IsFalse(OfficeCryptoStream.TryOpen(TestFile, null, out ocs)); Assert.IsFalse(OfficeCryptoStream.TryOpen(TestFile, "bar", out ocs)); Assert.IsTrue(OfficeCryptoStream.TryOpen(TestFile, "foo", out ocs)); ocs.Close(); }
public static void AccessEncryptedFileManualSave() { // Create stream, decrypt OfficeCryptoStream stream = OfficeCryptoStream.Open("a.xlsx", "password"); // Do whatever is needed in your program DoStuff(stream); // When done, save and close the encrypted stream stream.Save(); stream.Close(); }
public static OfficeCryptoStream OpenPasswordProtectedFile(String file) { String password = null; OfficeCryptoStream stream = null; while (OfficeCryptoStream.TryOpen(file, password, out stream) == false) { // Replace with your own input method (e.g. a dialog box) Console.Write("Enter password: "); password = Console.ReadLine(); } return(stream); }
public static void CreateEncryptedFile() { using (OfficeCryptoStream stream = OfficeCryptoStream.Create("a.xlsx")) { DoStuff(stream); // Set or change the password anytime before the save. // Set to null to save as plaintext. stream.Password = "******"; stream.Save(); } }
void AssertFileCorrect(String password) { using (OfficeCryptoStream s = OfficeCryptoStream.Open(TestFile, password)) { using (ExcelPackage p = new ExcelPackage(s)) { Assert.IsNotNull(p, "Cannot create package."); ExcelWorksheet ws = p.Workbook.Worksheets["Test"]; Assert.IsNotNull(ws, "No Test worksheet."); String cval = ws.Cell(1, 1).Value; Assert.AreEqual("Test Cell", cval, "First cell value incorrect."); } } }
void CreateTestWorkbook(String password) { using (OfficeCryptoStream s = OfficeCryptoStream.Create(TestFile)) { s.Password = password; using (ExcelPackage p = new ExcelPackage(s)) { ExcelWorksheet ws = p.Workbook.Worksheets["Test"]; if (ws == null) { ws = p.Workbook.Worksheets.Add("Test"); } ws.Cell(1, 1).Value = "Test Cell"; p.Save(); } s.Save(); } }
public void TestFormatEncrypted() { CreateTestWorkbook("foo"); Assert.AreEqual(false, OfficeCryptoStream.IsPlaintext(TestFile), "IsPlaintext"); Assert.AreEqual(true, OfficeCryptoStream.IsEncrypted(TestFile), "IsEncrypted"); }
public void IsFileNotOfficePackage() { File.WriteAllText(TestFile, "Not an office package"); Assert.AreEqual(false, OfficeCryptoStream.IsPlaintext(TestFile), "IsPlaintext"); Assert.AreEqual(false, OfficeCryptoStream.IsEncrypted(TestFile), "IsEncrypted"); }
public void IsFilePlaintext() { CreateTestWorkbook(null); Assert.AreEqual(true, OfficeCryptoStream.IsPlaintext(TestFile), "IsPlaintext"); Assert.AreEqual(false, OfficeCryptoStream.IsEncrypted(TestFile), "IsEncrypted"); }