AES(Advanced Encryption Standard)是一种广泛使用的对称密钥加密算法,由美国国家标准与技术研究院(NIST)于2001年发布。AES以其高效、安全的特点,在数据加密领域占据了重要地位。
html
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="350"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<TextBlock Text="密码转译" FontSize="20" FontWeight="Bold" Margin="10"/>
</Grid>
<Grid Grid.Row="1">
<Grid>
<Grid.RowDefinitions >
<RowDefinition Height="10*"/>
<RowDefinition Height="70*"/>
<RowDefinition Height="50*"/>
<RowDefinition Height="80*"/>
<RowDefinition Height="120*"/>
<RowDefinition Height="60*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="源码" Margin="0,6,5,0" FontSize="15" FontWeight="Bold"/>
<TextBox Name="InPutA" Width="210" Height="30" Margin="0,0,15,0" VerticalContentAlignment="Center"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="解密" Margin="0,6,5,0" FontSize="15" FontWeight="Bold"/>
<TextBox Name="OutPutB" Width="210" Height="30" Margin="0,0,15,0" VerticalContentAlignment="Center"/>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="加密" Background="#0047AB" Foreground="White" FontSize="15" HorizontalAlignment="Right"
Width="200" Height="27" Margin="0,0,0,0" BorderThickness="0"
Click="EncryptCommande"/>
<Button Content="解密" Background="#00ad43" Foreground="White" FontSize="15" HorizontalAlignment="Right"
Width="200" Height="27" Margin="0,5,0,0" BorderThickness="0"
Click="DecryptCommande"/>
</StackPanel>
<StackPanel Grid.Row="4" Height="30" Width="260" Orientation="Horizontal" VerticalAlignment="Bottom" Margin="0 0 0 10">
<Button Width="40" Click="P1" Content="P1" Foreground="#c8c8c8" BorderThickness="0" Background="#f8f8f8" Margin="25 0 15 0"/>
<Button Width="40" Click="P2" Content="P2" Foreground="#c8c8c8" BorderThickness="0" Background="#f8f8f8" Margin="0 0 15 0"/>
<Button Width="40" Click="P3" Content="P3" Foreground="#c8c8c8" BorderThickness="0" Background="#f8f8f8" Margin="0 0 15 0"/>
<Button Width="40" Click="P4" Content="Rand" Foreground="#c8c8c8" BorderThickness="0" Background="#f8f8f8" Margin="0 0 15 0"/>
</StackPanel>
<StackPanel Grid.Row="5">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="KEY:" Margin="0,2,5,0" FontSize="10" Foreground="#808080"/>
<TextBox Name="Key" Width="210" Height="17" Margin="0,0,15,0" VerticalContentAlignment="Center" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text=" I V:" Margin="0,8,5,0" FontSize="10" Foreground="#808080"/>
<TextBox Name="IV" Width="210" Height="17" Margin="0,6,15,0" VerticalContentAlignment="Center"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>
<TextBox Grid.Row="2" Name="Time" Height="17" Width="200" Margin="0 10 15 15" FontSize="13" HorizontalAlignment="Right" BorderThickness="0" HorizontalContentAlignment="Right"/>
</Grid>
cs
public partial class MainWindow : Window
{
private DispatcherTimer _timer;
public MainWindow()
{
InitializeComponent();
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(0.5); // 每0.5秒切换一次颜色
_timer.Tick += Timer_Tick; // 设置Tick事件处理程序
_timer.Start(); // 启动定时器
}
private void Timer_Tick(object sender, EventArgs e)
{
Time.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private void EncryptCommande(object sender, RoutedEventArgs e)
{
byte[] key = Encoding.UTF8.GetBytes(Key.Text);
byte[] iv = Encoding.UTF8.GetBytes(IV.Text);
if (key.Length != 32)
{
MessageBox.Show("Key错误\n请输入32位数字AES密钥", "警告");
}else
{
if (iv.Length != 16) {
MessageBox.Show("IV错误\n请输入16位数字IV", "警告");
}
else
{
// 输入的15位数字
string input = InPutA.Text;
// 加密
string encryptedText = Encrypt(input, key, iv);
OutPutB.Text = encryptedText;
}
}
}
private void DecryptCommande(object sender, RoutedEventArgs e)
{
byte[] key = Encoding.UTF8.GetBytes(Key.Text);
byte[] iv = Encoding.UTF8.GetBytes(IV.Text);
if (key.Length != 32)
{
MessageBox.Show("Key错误\n请输入32位数字AES密钥", "警告");
}
else
{
if (iv.Length != 16)
{
MessageBox.Show("IV错误\n请输入16位数字IV", "警告");
}
else
{
string input = InPutA.Text;
Regex regex = new Regex(@"[\u4e00-\u9fa5]+");
if (regex.IsMatch(input))
{
MessageBox.Show("输入错误\n待破解内容含有未知字符", "警告");
}
else
{
//解密
string decryptedText = Decrypt(input, key, iv);
OutPutB.Text = decryptedText;
}
}
}
}
static string Encrypt(string plainText, byte[] Key, byte[] IV)
{
byte[] encrypted;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return Convert.ToBase64String(encrypted);
}
static string Decrypt(string cipherText, byte[] Key, byte[] IV)
{
cipherText = cipherText.Replace(" ", "+"); // 处理Base64中的空格问题
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
return srDecrypt.ReadToEnd();
}
}
}
}
}
private void P1(object sender, RoutedEventArgs e)
{
Key.Text = "12345678901234567890123456789012";
IV.Text = "1234567890123456";
}
private void P2(object sender, RoutedEventArgs e)
{
Key.Text = "12345678901234567890123456789012";
IV.Text = "1234567890123456";
}
private void P3(object sender, RoutedEventArgs e)
{
Key.Text = "12345678901234567890123456789012";
IV.Text = "1234567890123456";
}
private void P4(object sender, RoutedEventArgs e)
{
Key.Text = DigitalGeneration(32);
IV.Text = DigitalGeneration(16);
}
static string DigitalGeneration(int A)
{
Random rand = new Random();
StringBuilder sb = new StringBuilder(A);
for (int i = 0; i < A; i++){
sb.Append(rand.Next(0, 10));}
string randomNumbers = sb.ToString();
return randomNumbers;
}
}