数据加密-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;
     }
 }
相关推荐
IT小哥哥呀21 小时前
电池制造行业数字化实施
大数据·制造·智能制造·数字化·mom·电池·信息化
Xi xi xi21 小时前
苏州唯理科技近期也正式发布了国内首款神经腕带产品
大数据·人工智能·经验分享·科技
yumgpkpm1 天前
华为鲲鹏 Aarch64 环境下多 Oracle 、mysql数据库汇聚到Cloudera CDP7.3操作指南
大数据·数据库·mysql·华为·oracle·kafka·cloudera
UMI赋能企业1 天前
制造业流程自动化提升生产力的全面分析
大数据·人工智能
TDengine (老段)1 天前
TDengine 数学函数 FLOOR 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
派可数据BI可视化1 天前
商业智能BI 浅谈数据孤岛和数据分析的发展
大数据·数据库·数据仓库·信息可视化·数据挖掘·数据分析
jiedaodezhuti1 天前
Flink性能调优基石:资源配置与内存优化实践
大数据·flink
mingupup1 天前
WPF/C#:使用Microsoft Agent Framework框架创建一个带有审批功能的终端Agent
c#·wpf
Lx3521 天前
Flink窗口机制详解:如何处理无界数据流
大数据
Lx3521 天前
深入理解Flink的流处理模型
大数据