-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
845 lines (762 loc) · 31.5 KB
/
MainWindow.xaml.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.WindowsAPICodePack.Dialogs;
namespace Diary
{
public partial class MainWindow : Window
{
int date;
int year;
int month;
readonly DispatcherTimer dispatcherTimer = new DispatcherTimer();
string FontSize_str;
string FontFamily_str;
string BackgroundLocation;
string LockScreenBackgroundLocation;
string Password;
string StorageLocation = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents");
string SecurityQuestion_str;
string SecurityQuestionAnswer;
bool Encryption = true;
bool HideDataFolder;
string SecurityQuestionAnswerPassword;
private List<Control> AllChildren(DependencyObject parent)
{
var list = new List<Control> { };
for (int count = 0; count < VisualTreeHelper.GetChildrenCount(parent); count++)
{
var child = VisualTreeHelper.GetChild(parent, count);
if (child is Control)
{
list.Add(child as Control);
}
list.AddRange(AllChildren(child));
}
return list;
}
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public MainWindow()
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
{
InitializeComponent();
MainComponents.Visibility = Visibility.Collapsed;
PasswordContainer.Visibility = Visibility.Visible;
date = 0;
using (var wb = new HttpClient())
{
var response = wb.GetStringAsync("http://api.weatherapi.com/v1/current.json?key=3e5f3bde7f6f4ebd91f92409221101&q=auto:ip");
string data = response.Result.Split('"')[38].Replace(",", "").Replace(":", "");
Temperature.Text = data + " °C/" + (float.Parse(data) * 9 / 5 + 32).ToString() + " °F";
}
dispatcherTimer.Tick += new EventHandler(Timer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
Timer_Tick(null, null);
date = DateTime.Now.Day;
year = DateTime.Now.Year;
month = DateTime.Now.Month;
Month.Text = GetAbbreviatedName(month);
Year.Text = year.ToString();
LoadingBar.Value = 0;
LoadingBar.Visibility = Visibility.Collapsed;
try
{
string[] SettingsData = File.ReadAllLines("config.txt");
FontSize_str = SettingsData[0];
FontFamily_str = SettingsData[1];
BackgroundLocation = SettingsData[2];
LockScreenBackgroundLocation = SettingsData[3];
Password = SettingsData[4];
if (Directory.Exists(SettingsData[5]))
{
StorageLocation = SettingsData[5];
}
SecurityQuestion_str = SettingsData[6];
SecurityQuestionAnswer = SettingsData[7];
HideDataFolder = bool.Parse(SettingsData[8]);
Encryption = bool.Parse(SettingsData[9]);
SecurityQuestionAnswerPassword = SettingsData[10];
}
catch
{
File.Create("config.txt").Dispose();
FontSize_str = (28).ToString();
FontFamily_str = "Caveat";
BackgroundLocation = "";
LockScreenBackgroundLocation = "";
Password = "";
if (Directory.Exists(""))
{
StorageLocation = "";
}
SecurityQuestion_str = "";
SecurityQuestionAnswer = "";
HideDataFolder = false;
Encryption = true;
SecurityQuestionAnswerPassword = "";
}
FontSize_input.Text = FontSize_str;
FontFamily_ComboBox.Text = FontFamily_str;
BackgroundLocationTextBlock.Text = BackgroundLocation;
LockScreenBackgroundLocationTextBlock.Text = LockScreenBackgroundLocation;
StorageLocationTextBlock.Text = StorageLocation;
SecurityQuestion_input.Text = SecurityQuestion_str;
HideFolderToggle.IsChecked = HideDataFolder;
EncryptionToggle.IsChecked = Encryption;
try
{
BackgroundImage.Source = new BitmapImage(new Uri(LockScreenBackgroundLocation));
}
catch
{
BackgroundImage.Source = new BitmapImage(new Uri("pack://application:,,,/resources/" + "Streets.jpg"));
}
if (StorageLocation == "")
{
StorageLocation = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents");
}
SaveSuccess.Visibility = Visibility.Collapsed;
this.SizeChanged += OnWindowSizeChanged;
FontFamily ff = new FontFamily(new Uri("pack://application:,,,/resources/"), "./#" + FontFamily_str);
Journal.FontFamily = ff;
try
{
Journal.FontSize = int.Parse(FontSize_str);
}
catch
{
FontSize_str = (28).ToString();
FontSize_input.Text = FontSize_str;
Journal.FontSize = int.Parse(FontSize_str);
}
BackgroundSelectButton.Content = "Browse...";
StorageButton.Content = "Browse...";
LockScreenBackgroundSelectButton.Content = "Browse...";
}
//Called on window size change
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
protected void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
{
float h = (float)e.NewSize.Height;
float w = (float)e.NewSize.Width;
Journal.Width = w / 1.6;
Journal.MinHeight = w / 1.6;
EnterPassword.Margin = new Thickness(30, h / 2.5, 0, 0);
}
static string GetAbbreviatedName(int month)
{
DateTime date = new DateTime(DateTime.Now.Year, month, DateTime.Now.Day);
return date.ToString("MMM");
}
private void Timer_Tick(object sender, EventArgs e)
{
Time.Text = DateTime.Now.ToLongTimeString();
Button Today = (Button)DefaultWindow.FindName("X" + DateTime.Now.Day.ToString());
Today.Background = Brushes.Red;
}
private void VerifyPassword(object sender, RoutedEventArgs args)
{
string PasswordInputText = PasswordInput.Password;
if (PasswordInputText == Decrypt(Password, PasswordInputText))
{
PasswordContainer.Visibility = Visibility.Collapsed;
try
{
BackgroundImage.Source = new BitmapImage(new Uri(BackgroundLocation));
}
catch
{
BackgroundImage.Source = new BitmapImage(new Uri("pack://application:,,,/resources/" + "Sunset.jpg"));
}
BackgroundImage.Opacity = 1;
MainComponents.Visibility = Visibility.Visible;
ReadJournal();
}
else
{
Wrong_Password.Visibility = Visibility.Visible;
PasswordInput.Clear();
}
}
private void Password_Enter(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
VerifyPassword(null, null);
}
}
void SelectDate(object sender, RoutedEventArgs e)
{
string dir = (StorageLocation + "\\Data\\" + year.ToString() + "\\" + GetAbbreviatedName(month) + "\\");
string path = dir + (date.ToString() + ".txt");
if (File.Exists(path))
{
if (Journal.Text != "" && Journal.Text != File.ReadAllText(path))
{
SaveVoid(null, null);
}
}
var button = sender as Button;
date = int.Parse(button.Tag.ToString());
Button SelectedButton = (Button)DefaultWindow.FindName("X" + date.ToString());
List<Control> children = AllChildren(Dates);
foreach (var child in children)
{
child.Style = (Style)DefaultWindow.FindResource("NormalDate");
}
SelectedButton.Style = (Style)DefaultWindow.FindResource("Today");
ReadJournal();
}
static string Encrypt(string clearText, string EncryptionKey)
{
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
static string Decrypt(string cipherText, string EncryptionKey)
{
byte[] cipherBytes;
try
{
cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
}
catch
{
cipherText = null;
}
return cipherText;
}
private void BackgroundImage_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//FocusManager.SetFocusedElement(FocusManager.GetFocusScope(Journal), null);
Journal.Focusable = false;
Journal.Focusable = true;
}
private void SelectBackground(object sender, RoutedEventArgs e)
{
var dialog = new Microsoft.Win32.OpenFileDialog
{
Multiselect = false,
Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"
};
// Show open file dialog box
bool? result = dialog.ShowDialog();
// Process open file dialog box results
if (result == true)
{
string filename = dialog.FileName;
if ((sender as Button).Tag.ToString() == "LSBG")
{
LockScreenBackgroundLocationTextBlock.Text = filename;
}
else
{
BackgroundLocationTextBlock.Text = filename;
}
}
}
private void SelectStorage(object sender, RoutedEventArgs e)
{
var dlg = new CommonOpenFileDialog
{
Title = "My Title",
IsFolderPicker = true,
InitialDirectory = "C:\\",
AddToMostRecentlyUsedList = false,
AllowNonFileSystemItems = false,
DefaultDirectory = "C:\\",
EnsureFileExists = true,
EnsurePathExists = true,
EnsureReadOnly = false,
EnsureValidNames = true,
Multiselect = false,
ShowPlacesList = true
};
if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
{
var folder = dlg.FileName;
StorageLocationTextBlock.Text = folder;
}
}
String[] contents = new string[11];
private void SaveSettings_Click(object sender, RoutedEventArgs e)
{
FontSize_str = FontSize_input.Text;
contents[0] = FontSize_str;
FontFamily_str = FontFamily_ComboBox.Text;
contents[1] = FontFamily_str;
BackgroundLocation = BackgroundLocationTextBlock.Text;
contents[2] = BackgroundLocation;
LockScreenBackgroundLocation = LockScreenBackgroundLocationTextBlock.Text;
contents[3] = LockScreenBackgroundLocation;
//if (Password != "")
//{
// if (Decrypt(Password, PasswordInput.Password) != NewPassword_input.Text)
// {
// decAll(PasswordInput.Password);
// encAll(NewPassword_input.Text);
// Password = Encrypt(NewPassword_input.Text, NewPassword_input.Text);
// contents[4] = Password;
// }
//}
contents[4] = Password;
StorageLocation = StorageLocationTextBlock.Text;
contents[5] = StorageLocation;
SecurityQuestion_str = SecurityQuestion_input.Text;
contents[6] = SecurityQuestion_str;
contents[8] = HideDataFolder.ToString();
contents[9] = Encryption.ToString();
contents[10] = SecurityQuestionAnswerPassword;
File.WriteAllLines("config.txt", contents);
try
{
BackgroundImage.Source = new BitmapImage(new Uri(BackgroundLocation));
}
catch
{
BackgroundImage.Source = new BitmapImage(new Uri("pack://application:,,,/resources/" + "Sunset.jpg"));
}
FontFamily ff = new FontFamily(new Uri("pack://application:,,,/resources/"), "./#" + FontFamily_str);
Journal.FontFamily = ff;
try
{
Journal.FontSize = int.Parse(FontSize_str);
}
catch
{
FontSize_str = (28).ToString();
FontSize_input.Text = FontSize_str;
Journal.FontSize = int.Parse(FontSize_str);
}
}
void ChangeMonth(object sender, RoutedEventArgs e)
{
var obj = sender as Button;
if ((string)obj.Tag == "+" && month < 12)
{
month++;
}
else if ((string)obj.Tag == "-" && month > 1)
{
month--;
}
Month.Text = new DateTime(2010, month, 1).ToString("MMM");
Year.Text = year.ToString();
}
void ChangeYear(object sender, RoutedEventArgs e)
{
var obj = sender as Button;
if ((string)obj.Tag == "+")
{
year++;
}
else if ((string)obj.Tag == "-" && year > 0)
{
year--;
}
Month.Text = new DateTime(2010, month, 1).ToString("MMM");
Year.Text = year.ToString();
}
static byte[] EncryptBytes(byte[] clearBytes, string EncryptionKey)
{
byte[] cipherBytes;
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
cipherBytes = ms.ToArray();
}
}
return cipherBytes;
}
static byte[] DecryptBytes(byte[] cipherBytes, string EncryptionKey)
{
byte[] plainBytes;
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
try
{
cs.Close();
plainBytes = ms.ToArray();
}
catch
{
plainBytes = new byte[0];
}
}
}
}
return plainBytes;
}
void SaveVoid(object sender, RoutedEventArgs e)
{
try
{
string dir = (StorageLocation + "\\Data\\" + year.ToString() + "\\" + GetAbbreviatedName(month) + "\\");
string path = dir + (date.ToString() + ".txt");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
string JournalText = Journal.Text;
File.WriteAllText(path, JournalText);
if (Encryption)
{
byte[] filebytes = File.ReadAllBytes(path);
File.WriteAllBytes(path, EncryptBytes(filebytes, PasswordInput.Password));
}
SaveSuccess.Text = "Save Success!!";
}
catch
{
SaveSuccess.Text = "Save Failed";
}
SaveSuccess.Visibility = Visibility.Visible;
}
void ReadJournal()
{
LoadingScreen.Visibility = Visibility.Visible;
LoadingBar.Value = 0;
string dir = StorageLocation + "\\Data\\" + year.ToString() + "\\" + GetAbbreviatedName(month) + "\\";
string path = dir + (date.ToString() + ".txt");
if (File.Exists(path))
{
if (Encryption)
{
byte[] filebytes = DecryptBytes(File.ReadAllBytes(path), PasswordInput.Password);
File.WriteAllBytes(path, filebytes);
Journal.Text = File.ReadAllText(path);
byte[] encFilebytes = EncryptBytes(File.ReadAllBytes(path), PasswordInput.Password);
File.WriteAllBytes(path, encFilebytes);
}
else
{
Journal.Text = File.ReadAllText(path);
}
}
else
{
Journal.Text = "";
}
LoadingBar.Value = 100;
LoadingScreen.Visibility = Visibility.Collapsed;
}
void ForgotPassword(object sender, RoutedEventArgs args)
{
if (SecurityQuestion_str != "")
{
SecurityQuestion.Text = SecurityQuestion_str;
ForgotPasswordMenu.Visibility = Visibility.Visible;
}
else
{
PasswordContainer.Visibility = Visibility.Collapsed;
ForgotPasswordMenu.Visibility = Visibility.Collapsed;
try
{
BackgroundImage.Source = new BitmapImage(new Uri(BackgroundLocation));
}
catch
{
BackgroundImage.Source = new BitmapImage(new Uri("pack://application:,,,/resources/" + "Sunset.jpg"));
}
BackgroundImage.Opacity = 1;
MainComponents.Visibility = Visibility.Visible;
string messageBoxText = "Software was automatially unlocked because of no security question was found. Create one now.";
string caption = "Fail Safe";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Information;
MessageBox.Show(messageBoxText, caption, button, icon);
}
}
void CloseForgotPassword(object sender, RoutedEventArgs args)
{
ForgotPasswordMenu.Visibility = Visibility.Collapsed;
}
private void VerifySecurityQuestionAnswer(object sender, RoutedEventArgs args)
{
string AnswerInputText = Answer.Text;
try
{
if (AnswerInputText == Decrypt(SecurityQuestionAnswer, AnswerInputText))
{
PasswordContainer.Visibility = Visibility.Collapsed;
ForgotPasswordMenu.Visibility = Visibility.Collapsed;
try
{
BackgroundImage.Source = new BitmapImage(new Uri(BackgroundLocation));
}
catch
{
BackgroundImage.Source = new BitmapImage(new Uri("pack://application:,,,/resources/" + "Sunset.jpg"));
}
BackgroundImage.Opacity = 1;
MainComponents.Visibility = Visibility.Visible;
string messageBoxText = "Your passsword is:" + Decrypt(SecurityQuestionAnswerPassword, AnswerInputText);
string caption = "Correct Answer";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Error;
PasswordInput.Password = Decrypt(SecurityQuestionAnswerPassword, AnswerInputText);
MessageBox.Show(messageBoxText, caption, button, icon);
}
else
{
string messageBoxText = "Wrong Answer";
string caption = "Wrong Answer";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Error;
MessageBox.Show(messageBoxText, caption, button, icon);
}
}
catch
{
;
}
}
private void SecurityQuestion_Enter(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
VerifySecurityQuestionAnswer(null, null);
}
}
private void UnhideFolder(object sender, RoutedEventArgs e)
{
string temp = StorageLocation;
temp += "\\Data";
string HideInfoCmd = "attrib -h -s -r \"" + temp + "\"";
if (!Directory.Exists(temp))
{
Directory.CreateDirectory(temp);
}
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = temp,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C " + HideInfoCmd
};
process.StartInfo = startInfo;
process.Start();
HideDataFolder = false;
}
private void HideFolder(object sender, RoutedEventArgs e)
{
string temp = StorageLocation;
temp += "\\Data";
string HideInfoCmd = "attrib +h +s +r \"" + temp + "\"";
if (!Directory.Exists(temp))
{
Directory.CreateDirectory(temp);
}
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = temp,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C " + HideInfoCmd
};
process.StartInfo = startInfo;
process.Start();
HideDataFolder = true;
}
private void HideDataInfo(object sender, RoutedEventArgs e)
{
string messageBoxText = "On Enabling this setting the folder won't be accessible by any user. It will be ccessible only through some technical ways. This is recommended to hide your diary data from anyone else using this PC.";
string caption = "Info";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Information;
MessageBox.Show(messageBoxText, caption, button, icon);
}
private void EncryptionEnable(object sender, RoutedEventArgs e)
{
EncAll(Decrypt(Password, PasswordInput.Password));
Encryption = true;
}
void EncAll(string key)
{
DirectoryInfo objDirectoryInfo = new DirectoryInfo(StorageLocation + "\\Data\\");
FileInfo[] allFiles = objDirectoryInfo.GetFiles("*.*", SearchOption.AllDirectories);
foreach (var file in allFiles)
{
try
{
byte[] fileBytes = File.ReadAllBytes(file.FullName);
byte[] encBytes = EncryptBytes(fileBytes, key);
File.WriteAllBytes(file.FullName, encBytes);
}
catch
{
;
}
}
}
void DecAll(string key)
{
DirectoryInfo objDirectoryInfo = new DirectoryInfo(StorageLocation + "\\Data\\");
FileInfo[] allFiles = objDirectoryInfo.GetFiles("*.*", SearchOption.AllDirectories);
foreach (var file in allFiles)
{
try
{
byte[] fileBytes = File.ReadAllBytes(file.FullName);
byte[] decBytes = DecryptBytes(fileBytes, key);
File.WriteAllBytes(file.FullName, decBytes);
}
catch
{
continue;
}
}
}
private void EncryptionDisable(object sender, RoutedEventArgs e)
{
DecAll(Decrypt(Password, PasswordInput.Password));
Encryption = false;
}
private void NewPassword(object sender, RoutedEventArgs e)
{
MyPersonalDiary.NewPasswordWindow dialog = new MyPersonalDiary.NewPasswordWindow();
dialog.ShowDialog();
var x = dialog.Password.ToCharArray();
for (int i = 0; i < dialog.Password.Length; i++)
{
if (x[i] == " ".ToCharArray()[0])
{
string messageBoxText = "Empty values or spaces are not allowed.";
string caption = "Error";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Error;
MessageBox.Show(messageBoxText, caption, button, icon);
return;
}
}
if (dialog.Password == "")
{
string messageBoxText = "Empty values or spaces are not allowed.";
string caption = "Error";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Error;
MessageBox.Show(messageBoxText, caption, button, icon);
return;
}
if (dialog.Password != "" || dialog.Password != " ")
{
DecAll(Decrypt(Password, PasswordInput.Password));
EncAll(dialog.Password);
Password = Encrypt(dialog.Password, dialog.Password);
PasswordInput.Password = dialog.Password;
SaveSettings_Click(null, null);
}
else
{
//string messageBoxText = "Can't keep value empty.";
//string caption = "Error";
//MessageBoxButton button = MessageBoxButton.OK;
//MessageBoxImage icon = MessageBoxImage.Error;
//MessageBox.Show(messageBoxText, caption, button, icon);
dialog.ShowDialog();
}
}
private void ChangeSecurityAnswer(object sender, RoutedEventArgs e)
{
MyPersonalDiary.NewPasswordWindow dialog = new MyPersonalDiary.NewPasswordWindow();
dialog.ShowDialog();
var x = dialog.Password.ToCharArray();
for (int i = 0; i < dialog.Password.Length; i++)
{
if (x[i] == " ".ToCharArray()[0])
{
string messageBoxText = "Empty values or spaces are not allowed.";
string caption = "Error";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Error;
MessageBox.Show(messageBoxText, caption, button, icon);
return;
}
}
if (dialog.Password == "")
{
string messageBoxText = "Empty values or spaces are not allowed.";
string caption = "Error";
MessageBoxButton button = MessageBoxButton.OK;
MessageBoxImage icon = MessageBoxImage.Error;
MessageBox.Show(messageBoxText, caption, button, icon);
return;
}
if (dialog.Password != "" || dialog.Password != " ")
{
SecurityQuestionAnswer = Encrypt(dialog.Password, dialog.Password);
contents[7] = SecurityQuestionAnswer;
contents[11] = Encrypt(PasswordInput.Password, dialog.Password);
SaveSettings_Click(null, null);
}
else
{
dialog.ShowDialog();
}
}
}
}