数据加密-AES数据加密WPF程序

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;
     }
 }
相关推荐
出发行进2 分钟前
Hive其四,Hive的数据导出,案例展示,表类型介绍
数据仓库·hive·hadoop
武子康2 小时前
大数据-258 离线数仓 - Griffin架构 配置安装 Livy 架构设计 解压配置 Hadoop Hive
java·大数据·数据仓库·hive·hadoop·架构
lucky_syq3 小时前
Flume和Kafka的区别?
大数据·kafka·flume
AI_NEW_COME4 小时前
构建全方位大健康零售帮助中心:提升服务与体验
大数据·人工智能
it噩梦4 小时前
es 中 terms set 使用
大数据·elasticsearch
中科岩创4 小时前
中科岩创边坡自动化监测解决方案
大数据·网络·物联网
DolphinScheduler社区5 小时前
作业帮基于 Apache DolphinScheduler 3_0_0 的缺陷修复与优化
大数据
SeaTunnel5 小时前
京东科技基于 Apache SeaTunnel 复杂场景适配 #数据集成
大数据
可喜~可乐6 小时前
C# WPF开发
microsoft·c#·wpf
喝醉酒的小白6 小时前
Elasticsearch 配置文件
大数据·elasticsearch·搜索引擎