教学服务系统

 找回密码
 立即注册
搜索
查看: 814|回复: 1

信息计算2019级1班12号雷楚涵

[复制链接]

9

主题

26

帖子

103

积分

注册会员

Rank: 2

积分
103
发表于 2022-4-5 19:00:37 | 显示全部楼层 |阅读模式



本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

26

帖子

103

积分

注册会员

Rank: 2

积分
103
 楼主| 发表于 2022-4-5 20:57:09 | 显示全部楼层
  1. using System;
  2. using System.Text;
  3. using System.Security.Cryptography;
  4. using Microsoft.Office.Interop.Word;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  99.             return Encoding.UTF8.GetString(resultArray);
  100.         }
  101.         /// <summary>
  102.         /// byte数组Hex编码
  103.         /// </summary>
  104.         /// <param name="bytes">需要进行编码的byte[]</param>
  105.         /// <returns></returns>
  106.         public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
  107.         {
  108.             string hexString = string.Empty;
  109.             if (bytes != null)
  110.             {
  111.                 StringBuilder strB = new StringBuilder();
  112.                 for (int i = 0; i < bytes.Length; i++)
  113.                 {
  114.                     strB.Append(bytes[i].ToString("X2"));
  115.                 }
  116.                 hexString = strB.ToString();
  117.             }
  118.             return hexString;
  119.         }
  120.         /// <summary>
  121.         /// 字符串进行Hex解码(Hex.decodeHex())
  122.         /// </summary>
  123.         /// <param name="hexString">需要进行解码的字符串</param>
  124.         /// <returns></returns>
  125.         public static byte[] StrToHexByte(string hexString)
  126.         {
  127.             hexString = hexString.Replace(" ", "");
  128.             if ((hexString.Length % 2) != 0)
  129.                 hexString += " ";
  130.             byte[] returnBytes = new byte[hexString.Length / 2];
  131.             for (int i = 0; i < returnBytes.Length; i++)
  132.                 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
  133.             return returnBytes;
  134.         }

  135.         static void Main(string[] args)
  136.         {
  137.             Application app = new Application();
  138.             Document doc = null;
  139.             object unknow = Type.Missing;
  140.             app.Visible = true;
  141.             object file = @"C:\Users\hp\Desktop\雷楚涵.docx";
  142.             doc = app.Documents.Open(ref file,
  143.               ref unknow, ref unknow, ref unknow, ref unknow,
  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);

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

  148.             string goal1 = AesEncryptor_Base64(temp, "11122233344455566677788812345678");
  149.             Console.WriteLine("Base64加密之后:" + goal1);

  150.             string goal2 = AesDecryptor_Base64(goal1, "11122233344455566677788812345678");
  151.             Console.WriteLine("Base64解密之后:" + goal2);

  152.             string goal3 = AesEncryptor_Hex(temp, "11122233344455566677788812345678");
  153.             Console.WriteLine("Hex64加密之后:" + goal3);

  154.             string goal4 = AesDecryptor_Hex(goal3, "11122233344455566677788812345678");
  155.             Console.WriteLine("Hex解密之后:" + goal4);
  156.         }
  157.     }
  158. }
复制代码





本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-5-6 14:34 , Processed in 0.015207 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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