Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
126 lines (107 loc) · 3.72 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
126 lines (107 loc) · 3.72 KB
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
using System.Text;
using System.Windows;
namespace Caesar_Cipher
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Hook up the event handler
Plaintext_Btn.Click += Plaintext_Btn_Click;
Encrypt_Btn.Click += Encrypt_Btn_Click;
Decrypt_Btn.Click += Decrypt_Btn_Click;
Clear_Btn.Click += Clear_Btn_Click;
}
private void Close_Btn_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Plaintext_Btn_Click(object sender, RoutedEventArgs e)
{
// Enable the buttons and text box
Encrypt_Btn.IsEnabled = true;
Decrypt_Btn.IsEnabled = true;
Clear_Btn.IsEnabled = true;
Txb_EnDecrypt.IsEnabled = true;
}
private void Encrypt_Btn_Click(object sender, RoutedEventArgs e)
{
string plaintext = Txb_EnDecrypt.Text;
string encryptedText = Encrypt(plaintext);
Txb_EnDecrypt.Text = encryptedText;
Lbl_Info.Content = "Encrypted Successfully";
}
private void Decrypt_Btn_Click(object sender, RoutedEventArgs e)
{
string ciphertext = Txb_EnDecrypt.Text;
string decryptedText = Decrypt(ciphertext);
Txb_EnDecrypt.Text = decryptedText;
Lbl_Info.Content = "Decrypted Successfully";
}
private void Clear_Btn_Click(object sender, RoutedEventArgs e)
{
Txb_EnDecrypt.Clear();
Lbl_Info.Content = "Please Insert Text";
}
private string Encrypt(string plaintext)
{
StringBuilder encryptedText = new StringBuilder();
foreach (char c in plaintext)
{
if (char.IsLetter(c))
{
char encryptedChar = c;
if (char.IsLower(c))
{
// Encrypt lowercase letter
encryptedChar = (char)((c - 'a' + 3) % 26 + 'a');
}
else if (char.IsUpper(c))
{
// Encrypt uppercase letter
encryptedChar = (char)((c - 'A' + 3) % 26 + 'A');
}
encryptedText.Append(encryptedChar);
}
else
{
// Keep non-letter characters as they are
encryptedText.Append(c);
}
}
return encryptedText.ToString();
}
private string Decrypt(string ciphertext)
{
StringBuilder decryptedText = new StringBuilder();
foreach (char c in ciphertext)
{
if (char.IsLetter(c))
{
char decryptedChar = c;
if (char.IsLower(c))
{
// Decrypt lowercase letter
decryptedChar = (char)((c - 'a' - 3 + 26) % 26 + 'a');
}
else if (char.IsUpper(c))
{
// Decrypt uppercase letter
decryptedChar = (char)((c - 'A' - 3 + 26) % 26 + 'A');
}
decryptedText.Append(decryptedChar);
}
else
{
// Keep non-letter characters as they are
decryptedText.Append(c);
}
}
return decryptedText.ToString();
}
}
}