数据加密-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;
     }
 }
相关推荐
得物技术1 天前
从埋点需求到规则资产:Hermes Agent 重构得物数仓工作流
大数据·llm·ai编程
久美子1 天前
AI驱动数仓建设的Harness工程实践——本体建模、知识分层与上下文工程
大数据
大树882 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
大志哥1232 天前
ES和Logstash日志链路系统上线后遭遇切片爆炸(解决)
大数据·elasticsearch
果丁智能2 天前
物联网智能锁赋能集中式住宿:身份核验与远程权限管控的全链路技术实践
大数据·人工智能·物联网·智能家居
王小王-1232 天前
基于 Hive 的网易云音乐数据分析及可视化系统
hive·hadoop·数据分析·音乐数据分析·网易云音乐分析·hive音乐分析·hadoop网易云
ApacheSeaTunnel2 天前
实战演示 | 基于 Apache SeaTunnel 与 Apache DolphinScheduler 实现 MySQL 到 Doris 离线定时增量同步
大数据·mysql·开源·doris·数据集成·seatunnel·数据同步
weixin_397574092 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
极光代码工作室2 天前
基于数据仓库的电商数据分析平台
大数据·hadoop·python·spark·数据可视化
秋名山码民2 天前
Graph RAG 深度解析:从向量检索到知识推理的技术演进
大数据·人工智能·rag