教学服务系统

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

信息计算2019级二班13号孙鹏

[复制链接]

11

主题

13

帖子

65

积分

注册会员

Rank: 2

积分
65
发表于 2022-4-6 17:14:11 | 显示全部楼层 |阅读模式
  1. using Microsoft.Office.Interop.Word;
  2. using System;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Web;


  6. namespace Read_doc
  7. {
  8.     class Base64Helper
  9.     {
  10.         /// <summary>
  11.         /// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文
  12.         /// </summary>
  13.         /// <param name="EncryptStr">明文</param>
  14.         /// <param name="Key">密钥</param>
  15.         /// <returns>加密后base64编码的密文</returns>
  16.         public static string AesEncryptor_Base64(string EncryptStr, string Key)
  17.         {
  18.             try
  19.             {
  20.                 //byte[] keyArray = Encoding.UTF8.GetBytes(Key);//得出结果一致
  21.                 byte[] keyArray = Convert.FromBase64String(Key);
  22.                 byte[] toEncryptArray = Encoding.UTF8.GetBytes(EncryptStr);

  23.                 RijndaelManaged rDel = new RijndaelManaged();
  24.                 rDel.Key = keyArray;
  25.                 rDel.Mode = CipherMode.ECB;
  26.                 rDel.Padding = PaddingMode.PKCS7;

  27.                 ICryptoTransform cTransform = rDel.CreateEncryptor();
  28.                 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

  29.                 return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  30.             }
  31.             catch (Exception ex)
  32.             {
  33.                 return null;
  34.             }
  35.         }

  36.         /// <summary>
  37.         /// AES 算法解密(ECB模式) 将密文base64解码进行解密,返回明文
  38.         /// </summary>
  39.         /// <param name="DecryptStr">密文</param>
  40.         /// <param name="Key">密钥</param>
  41.         /// <returns>明文</returns>
  42.         public static string AesDecryptor_Base64(string DecryptStr, string Key)
  43.         {
  44.             try
  45.             {
  46.                 //byte[] keyArray = Encoding.UTF8.GetBytes(Key);
  47.                 byte[] keyArray = Convert.FromBase64String(Key);
  48.                 byte[] toEncryptArray = Convert.FromBase64String(DecryptStr);

  49.                 RijndaelManaged rDel = new RijndaelManaged();
  50.                 rDel.Key = keyArray;
  51.                 rDel.Mode = CipherMode.ECB;
  52.                 rDel.Padding = PaddingMode.PKCS7;

  53.                 ICryptoTransform cTransform = rDel.CreateDecryptor();
  54.                 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

  55.                 return Encoding.UTF8.GetString(resultArray);//  UTF8Encoding.UTF8.GetString(resultArray);
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 return null;
  60.             }
  61.         }

  62.         /// <summary>
  63.         ///AES 算法加密(ECB模式) 将明文加密,加密后进行Hex编码,返回密文
  64.         /// </summary>
  65.         /// <param name="str">明文</param>
  66.         /// <param name="key">密钥</param>
  67.         /// <returns>加密后Hex编码的密文</returns>
  68.         public static string AesEncryptor_Hex(string str, string key)
  69.         {
  70.             if (string.IsNullOrEmpty(str)) return null;
  71.             Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);

  72.             System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  73.             {
  74.                 Key = StrToHexByte(key),
  75.                 Mode = System.Security.Cryptography.CipherMode.ECB,
  76.                 Padding = System.Security.Cryptography.PaddingMode.PKCS7
  77.             };

  78.             System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  79.             Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

  80.             return ToHexString(resultArray);
  81.         }

  82.         /// <summary>
  83.         ///AES 算法解密(ECB模式) 将密文Hex解码后进行解密,返回明文
  84.         /// </summary>
  85.         /// <param name="str">密文</param>
  86.         /// <param name="key">密钥</param>
  87.         /// <returns>明文</returns>
  88.         public static string AesDecryptor_Hex(string str, string key)
  89.         {
  90.             if (string.IsNullOrEmpty(str)) return null;
  91.             Byte[] toEncryptArray = StrToHexByte(str);

  92.             System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  93.             {
  94.                 Key = StrToHexByte(key),
  95.                 Mode = System.Security.Cryptography.CipherMode.ECB,
  96.                 Padding = System.Security.Cryptography.PaddingMode.PKCS7
  97.             };

  98.             System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
  99.             Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

  100.             return Encoding.UTF8.GetString(resultArray);
  101.         }

  102.         /// <summary>
  103.         /// byte数组Hex编码
  104.         /// </summary>
  105.         /// <param name="bytes">需要进行编码的byte[]</param>
  106.         /// <returns></returns>
  107.         public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
  108.         {
  109.             string hexString = string.Empty;
  110.             if (bytes != null)
  111.             {
  112.                 StringBuilder strB = new StringBuilder();
  113.                 for (int i = 0; i < bytes.Length; i++)
  114.                 {
  115.                     strB.Append(bytes[i].ToString("X2"));
  116.                 }
  117.                 hexString = strB.ToString();
  118.             }
  119.             return hexString;
  120.         }
  121.         /// <summary>
  122.         /// 字符串进行Hex解码(Hex.decodeHex())
  123.         /// </summary>
  124.         /// <param name="hexString">需要进行解码的字符串</param>
  125.         /// <returns></returns>
  126.         public static byte[] StrToHexByte(string hexString)
  127.         {
  128.             hexString = hexString.Replace(" ", "");
  129.             if ((hexString.Length % 2) != 0)
  130.                 hexString += " ";
  131.             byte[] returnBytes = new byte[hexString.Length / 2];
  132.             for (int i = 0; i < returnBytes.Length; i++)
  133.                 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
  134.             return returnBytes;
  135.         }
  136.         public static void Main(string[] args)
  137.         {
  138.                 Application app = new Application();
  139.                 Document doc = null;
  140.                 object unknow = Type.Missing;
  141.                 app.Visible = true;
  142.                 object file = @"C:\Users\21414\桌面\密码学.docx";
  143.                 doc = app.Documents.Open(ref file,
  144.                     ref unknow, ref unknow, ref unknow, ref unknow,
  145.                     ref unknow, ref unknow, ref unknow, ref unknow,
  146.                     ref unknow, ref unknow, ref unknow, ref unknow,
  147.                     ref unknow, ref unknow, ref unknow);

  148.                 string temp = doc.Content.Text.Trim();

  149.             string rosee1 = AesEncryptor_Base64(temp, "15449232589797845212125488451212");
  150.             Console.WriteLine("Base64加密:"+rosee1);

  151.             string rosee2 = AesDecryptor_Base64(rosee1, "15449232589797845212125488451212");
  152.             Console.WriteLine("Base64解密:" + rosee2);

  153.             string rosee3 = AesEncryptor_Hex(temp, "15449232589797845212125488451212");
  154.             Console.WriteLine("Hex64加密:" + rosee3);

  155.             string rosee4 = AesDecryptor_Hex(rosee3, "15449232589797845212125488451212");
  156.             Console.WriteLine("Hex解密:" + rosee4);



  157.         }

  158.     }
  159. }
复制代码


回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-5-6 10:01 , Processed in 0.018219 second(s), 29 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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