教学服务系统

 找回密码
 立即注册
搜索
查看: 620|回复: 0

信息计算2019级2班28号徐源璞

[复制链接]

10

主题

16

帖子

78

积分

注册会员

Rank: 2

积分
78
发表于 2022-4-15 16:42:23 | 显示全部楼层 |阅读模式
[attachimg]1428[/atta
  1. private void Button_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             string str1 = "明文.txt";
  4.             string str2 = "密文.txt";
  5.             if (p1.Password.Length<5)
  6.             {
  7.                 MessageBox.Show("密码不能低于5位");
  8.                 return;
  9.             }
  10.             byte[] key, iv;
  11.             aes.GenKeyIV(p1.Password, out key, out iv);
  12.             string ss = "";
  13.             FileStream fs = new FileStream(str1, FileMode.Open, FileAccess.Read);
  14.             StreamReader sr = new StreamReader(fs);
  15.             ss = sr.ReadToEnd();
  16.             fs.Close();
  17.             sr.Close();
  18.             a1.Text = "原始字符串:" + ss;
  19.             byte[] data1 = aes.EncryptString(str1, str2, key, iv);
  20.             string encryptedString = Convert.ToBase64String(data1);
  21.             a1.Text += "\n加密后的字符串:" + encryptedString;
  22.             byte[] data2 = Convert.FromBase64String(encryptedString);
  23.             string s = aes.DescrptString(data2, key, iv);
  24.             a1.Text += "\n解密后的字符串:" + s;
  25.         }
  26. public static byte[] EncryptString(string str1, string str2, byte[] key, byte[] iv)  //使用AES算法加密字符串
  27.         {
  28.             string ss = "";
  29.             FileStream fs = new FileStream(str1, FileMode.Open, FileAccess.Read);
  30.             StreamReader sr = new StreamReader(fs);
  31.             ss = sr.ReadToEnd();
  32.             fs.Close();
  33.             sr.Close();

  34.             FileStream fs1 = new FileStream(str2, FileMode.Create, FileAccess.Write);
  35.             using (Aes aesAlg = Aes.Create())
  36.             {
  37.                 ICryptoTransform encryptor = aesAlg.CreateEncryptor(key, iv);
  38.                 CryptoStream cs = new CryptoStream(fs1, encryptor, CryptoStreamMode.Write);
  39.                 using (StreamWriter sw = new StreamWriter(cs))
  40.                 {
  41.                     sw.Write(ss);
  42.                 }
  43.                 cs.Close();
  44.                 fs1.Close();
  45.             }
  46.             fs1 = new FileStream(str2, FileMode.Open, FileAccess.Read);
  47.             BinaryReader br = new BinaryReader(fs1);
  48.             byte[] encrypted = br.ReadBytes(10240);
  49.             br.Close();
  50.             fs1.Close();
  51.             return encrypted;
  52.         }
  53.         public static string DescrptString(byte[] data, byte[] key, byte[] iv)  //使用AES算法解密字符串
  54.         {
  55.             string str = "";
  56.             using (Aes aesAlg = Aes.Create())
  57.             {
  58.                 ICryptoTransform decryptor = aesAlg.CreateDecryptor(key, iv);
  59.                 MemoryStream ms = new MemoryStream(data);
  60.                 CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
  61.                 using (StreamReader sr = new StreamReader(cs))
  62.                 {
  63.                     str = sr.ReadToEnd();
  64.                 }
  65.                 cs.Close();
  66.                 ms.Close();
  67.             }
  68.             return str;
  69.         }
  70.         public static void GenKeyIV(string password, out byte[] key, out byte[] iv)   //根据提供的密码生成key和IV
  71.         {
  72.             using (Aes aes = Aes.Create())
  73.             {
  74.                 key = new byte[aes.Key.Length];
  75.                 byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
  76.                 for (int i = 0; i < pwdBytes.Length; i++)
  77.                 {
  78.                     key[i] = pwdBytes[i];
  79.                 }
  80.                 iv = new byte[aes.IV.Length];
  81.                 for (int i = 0; i < pwdBytes.Length; i++)
  82.                 {
  83.                     iv[i] = pwdBytes[i];
  84.                 }
  85.             }
  86.         }
  87.         public static void GenKeyIV(out byte[] key, out byte[] iv)   //随机生成Key和IV
  88.         {
  89.             using (Aes aes = Aes.Create())
  90.             {
  91.                 key = aes.Key;
  92.                 iv = aes.IV;
  93.             }
  94.         }
复制代码
chimg]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 12:28 , Processed in 0.015508 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表