|

楼主 |
发表于 2022-4-5 21:45:48
|
显示全部楼层
本帖最后由 信息计算敖若瑶 于 2022-4-5 22:49 编辑
- using System;
- using System.Text;
- using System.Reflection.Metadata;
- using Microsoft.Office.Interop.Word;
- namespace AES
- {
- class Program
- {
- private static object app;
- /// <summary>
- ///AES 算法加密(ECB模式) 将明文加密,加密后进行Hex编码,返回密文
- /// </summary>
- /// <param name="str">明文</param>
- /// <param name="key">密钥</param>
- /// <returns>加密后Hex编码的密文</returns>
- public static string AesEncryptor_Hex(string str, string key)
- {
- if (string.IsNullOrEmpty(str)) return null;
- Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
- System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
- {
- Key = StrToHexByte(key),
- Mode = System.Security.Cryptography.CipherMode.ECB,
- Padding = System.Security.Cryptography.PaddingMode.PKCS7
- };
- System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
- Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- return ToHexString(resultArray);
- }
- private static string ToHexString(byte[] resultArray)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- ///AES 算法解密(ECB模式) 将密文Hex解码后进行解密,返回明文
- /// </summary>
- /// <param name="str">密文</param>
- /// <param name="key">密钥</param>
- /// <returns>明文</returns>
- public static string AesDecryptor_Hex(string str, string key)
- {
- if (string.IsNullOrEmpty(str)) return null;
- Byte[] toEncryptArray = StrToHexByte(str);
- System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
- {
- Key = StrToHexByte(key),
- Mode = System.Security.Cryptography.CipherMode.ECB,
- Padding = System.Security.Cryptography.PaddingMode.PKCS7
- };
- System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
- Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- return Encoding.UTF8.GetString(resultArray);
- }
- public static byte[] StrToHexByte(string hexString)
- {
- hexString = hexString.Replace(" ", "");
- if ((hexString.Length % 2) != 0)
- hexString += " ";
- byte[] returnBytes = new byte[hexString.Length / 2];
- for (int i = 0; i < returnBytes.Length; i++)
- returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
- return returnBytes;
- }
- static void Main(string[] args)
- {
- Microsoft.Office.Interop.Word.Application A = new Microsoft.Office.Interop.Word.Application();//打开word程序
- Document doc = null;
- object unknow = Type.Missing;
- A.Visible = true;
- string str = @"/Users/gesilasapple/Desktop/大三下学业/密码学/读后感.docx";
- object file = str;
- doc = A.Documents.Open(ref file,
- ref unknow, ref unknow, ref unknow, ref unknow,
- ref unknow, ref unknow, ref unknow, ref unknow,
- ref unknow, ref unknow);
- string str1 = doc.Content.Text.Trim();
- string str2 = AesEncryptor_Hex(str1, "364836143850abc46746243098546756");
- Console.WriteLine("Hex加密密文:" + str2);
- string str3 = AesDecryptor_Hex(str2, "364836143850abc46746243098546756");
- Console.WriteLine("Hex解密密文:" + str3);
- }
- }
- }
复制代码
|
|