Skip to content

Commit

Permalink
Добавлена возможность расшифровать полученные от пользователей сообще…
Browse files Browse the repository at this point in the history
…ния.
  • Loading branch information
sven4500 committed Jun 15, 2021
1 parent 3b5225b commit bbcf018
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 13 deletions.
10 changes: 5 additions & 5 deletions DecryptionPage/DecryptionPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<ComboBox Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center" Width="300"/>
<ComboBox Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100"/>
<ComboBox Name="EmailsCbs" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center" Width="300" SelectionChanged="EmailsCbs_SelectionChanged"/>
<ComboBox Name="MessageCbs" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120" SelectionChanged="MessageCbs_SelectionChanged"/>
</Grid>
<Grid Grid.Row="1" Grid.ColumnSpan="3">
<Grid.ColumnDefinitions>
Expand All @@ -51,14 +51,14 @@
<ColumnDefinition Width="1.5*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="d" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBox Name="DTextBox" Grid.Column="1" VerticalAlignment="Center"/>
<TextBox Name="DTextBox" Text="0" Grid.Column="1" VerticalAlignment="Center"/>
<Label Grid.Column="2" Content="n" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBox Name="NTextBox" Grid.Column="3" VerticalAlignment="Center"/>
<TextBox Name="NTextBox" Text="0" Grid.Column="3" VerticalAlignment="Center"/>
<Button Name="DecryptButton" Grid.Column="5" Content="Дешифровать" Width="100" Height="20" Click="DecryptButton_Click"/>
</Grid>
<TextBlock Grid.Row="2" Grid.Column="0" Margin="10,0,0,0"
VerticalAlignment="Center" Text="Зашифрованное сообщение"/>
<TextBox Name="InputTextBox" Grid.Row="3" Grid.Column="0" IsReadOnly="True"/>
<TextBox Name="InputTextBox" Grid.Row="3" Grid.Column="0" IsReadOnly="True" TextWrapping="WrapWithOverflow"/>
<TextBox Name="InputBinTextBox" Grid.Row="5" Grid.Column="0" IsReadOnly="True" TextWrapping="WrapWithOverflow" VerticalScrollBarVisibility="Auto" FontFamily="Courier New"/>
<!-- PreviousAndNext потому что колонка имеет размер *-->
<GridSplitter Grid.Column="1" Grid.Row="2" Grid.RowSpan="4" Background="White"
Expand Down
128 changes: 122 additions & 6 deletions DecryptionPage/DecryptionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace RSATutor
{
public partial class DecryptionPage : Page
{
public ulong D
private ulong D
{
get
{
Expand All @@ -30,7 +30,7 @@ public ulong D
}
}

public ulong N
private ulong N
{
get
{
Expand All @@ -44,7 +44,7 @@ public ulong N
}

private ulong[] encryptedMessage = null;
public ulong[] EncryptedMessage
private ulong[] EncryptedMessage
{
get
{
Expand All @@ -63,6 +63,101 @@ public ulong[] EncryptedMessage
InputTextBox.Text = System.Text.Encoding.Default.GetString(encryptedBytes);
InputBinTextBox.Text = Utils.ToString(encryptedBytes);
}

OutputTextBox.Text = "";
OutputBinTextBox.Text = "";
}
}

private Dictionary<string, PrivateKey> privateKeys = new Dictionary<string, PrivateKey>();
public Dictionary<string, PrivateKey> PrivateKeys
{
set
{
if (value != null)
{
privateKeys = value;
}
}

get
{
return privateKeys;
}
}

private Dictionary<string, List<ulong[]>> encryptedMessages = new Dictionary<string, List<ulong[]>>();
public Dictionary<string, List<ulong[]>> EncryptedMessages
{
set
{
List<string> emails = new List<string>();

if (value != null)
{
encryptedMessages = value;
}

foreach (KeyValuePair<string, List<ulong[]>> pair in encryptedMessages)
{
// Добавляем в список только тех пользователей от которых
// были получены сообщения.
if (pair.Value != null && pair.Value.Count > 0)
{
emails.Add(pair.Key);
}
}

EmailsCbs.ItemsSource = emails;
EmailsCbs.SelectedIndex = 0;
}

get
{
return encryptedMessages;
}
}

private string email = "";
private string Email
{
set
{
List<string> messageTitles = new List<string>();

email = value;

if (privateKeys.ContainsKey(value))
{
D = privateKeys[value].d;
N = privateKeys[value].n;
}
else
{
D = 0;
N = 0;
}

if (EncryptedMessages.ContainsKey(value))
{
for (var i = 0; i < EncryptedMessages[value].Count; ++i)
{
messageTitles.Add(string.Format("Сообщение {0}", i + 1));
}
}

MessageCbs.ItemsSource = null;

if (messageTitles.Count > 0)
{
MessageCbs.ItemsSource = messageTitles;
MessageCbs.SelectedIndex = 0;
}
}

get
{
return email;
}
}

Expand All @@ -73,10 +168,31 @@ public DecryptionPage()

private void DecryptButton_Click(object sender, RoutedEventArgs e)
{
byte[] decryptedBytes = Utils.Decrypt(EncryptedMessage, D, N);
if (EncryptedMessage != null)
{
byte[] decryptedBytes = Utils.Decrypt(EncryptedMessage, D, N);

OutputTextBox.Text = System.Text.Encoding.Default.GetString(decryptedBytes);
OutputBinTextBox.Text = Utils.ToString(decryptedBytes);
}
}

private void EmailsCbs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<string> emails = EmailsCbs.ItemsSource as List<string>;

if (EmailsCbs.SelectedIndex >= 0)
{
Email = emails[EmailsCbs.SelectedIndex];
}
}

OutputTextBox.Text = System.Text.Encoding.Default.GetString(decryptedBytes);
OutputBinTextBox.Text = Utils.ToString(decryptedBytes);
private void MessageCbs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (EncryptedMessages.ContainsKey(Email) && MessageCbs.SelectedIndex >= 0)
{
EncryptedMessage = EncryptedMessages[Email][MessageCbs.SelectedIndex];
}
}
}
}
12 changes: 10 additions & 2 deletions EncryptionPage/EncryptionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,16 @@ private string Email

set
{
E = (publicKeys.ContainsKey(value)) ? publicKeys[value].e : 0;
N = (publicKeys.ContainsKey(value)) ? publicKeys[value].n : 0;
if (publicKeys.ContainsKey(value))
{
E = publicKeys[value].e;
N = publicKeys[value].n;
}
else
{
E = 0;
N = 0;
}

// Очистить зашифрованное сообщение при смене пользователя.
EncryptedMessage = new ulong[0];
Expand Down
2 changes: 2 additions & 0 deletions MainWindow/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ void setPage(int pageNumber)
break;
case 3:
page = decryptionPage;
decryptionPage.PrivateKeys = keyGeneratorPage.PrivateKeys;
decryptionPage.EncryptedMessages = encryptionPage.EncryptedMessages;
break;
default:
page = null;
Expand Down

0 comments on commit bbcf018

Please sign in to comment.