/
Program.vb
79 lines (68 loc) · 3.44 KB
/
Program.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Imports System
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Diagnostics
Namespace word_processing_encryption
Friend Class Program
Private Shared Property IsValid As Boolean
Shared Sub Main(ByVal args As String())
Dim server As RichEditDocumentServer = New RichEditDocumentServer()
AddHandler server.EncryptedFilePasswordRequested, AddressOf Server_EncryptedFilePasswordRequested
AddHandler server.EncryptedFilePasswordCheckFailed, AddressOf Server_EncryptedFilePasswordCheckFailed
AddHandler server.DecryptionFailed, AddressOf Server_DecryptionFailed
server.Options.Import.EncryptionPassword = "test"
server.LoadDocument("Documents//testEncrypted.docx")
Dim encryptionOptions As EncryptionSettings = New EncryptionSettings()
encryptionOptions.Type = EncryptionType.Strong
encryptionOptions.Password = "12345"
Console.WriteLine("Select the file format: DOCX/DOC")
Dim answerFormat As String = Console.ReadLine()?.ToLower()
Dim documentFormat As DocumentFormat
If Equals(answerFormat, "docx") Then
documentFormat = DocumentFormat.OpenXml
Else
documentFormat = DocumentFormat.Doc
End If
Dim fileName As String = String.Format("EncryptedwithNewPassword.{0}", answerFormat)
server.SaveDocument(fileName, documentFormat, encryptionOptions)
Console.WriteLine("The document is saved with new password. Continue? (y/n)")
Dim answer As String = Console.ReadLine()?.ToLower()
If Equals(answer, "y") Then
Console.WriteLine("Re-opening the file...")
server.LoadDocument(fileName)
End If
If IsValid = True Then
server.SaveDocument(fileName, documentFormat)
Call Process.Start(fileName)
End If
End Sub
Private Shared Sub Server_DecryptionFailed(ByVal sender As Object, ByVal e As DecryptionFailedEventArgs)
Console.WriteLine(e.Exception.Message.ToString() & " Press any key to close...")
Console.ReadKey(True)
End Sub
Private Shared Sub Server_EncryptedFilePasswordCheckFailed(ByVal sender As Object, ByVal e As EncryptedFilePasswordCheckFailedEventArgs)
Select Case e.Error
Case RichEditDecryptionError.PasswordRequired
Console.WriteLine("You did not enter the password!")
e.TryAgain = True
e.Handled = True
Case RichEditDecryptionError.WrongPassword
Console.WriteLine("The password is incorrect. Try Again? (y/n)")
Dim answer As String = Console.ReadLine()?.ToLower()
If Equals(answer, "y") Then
e.TryAgain = True
e.Handled = True
Else
IsValid = False
End If
End Select
IsValid = False
End Sub
Private Shared Sub Server_EncryptedFilePasswordRequested(ByVal sender As Object, ByVal e As EncryptedFilePasswordRequestedEventArgs)
Console.WriteLine("Enter password:")
e.Password = Console.ReadLine()
e.Handled = True
IsValid = True
End Sub
End Class
End Namespace