|

楼主 |
发表于 2022-4-5 20:57:09
|
显示全部楼层
- using System;
- using System.Text;
- using System.Security.Cryptography;
- using Microsoft.Office.Interop.Word;
- namespace ConsoleApp2
- {
- class Program
- {
- /// <summary>
- /// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文
- /// </summary>
- /// <param name="EncryptStr">明文</param>
- /// <param name="Key">密钥</param>
- /// <returns>加密后base64编码的密文</returns>
- public static string AesEncryptor_Base64(string EncryptStr, string Key)
- {
- try
- {
- //byte[] keyArray = Encoding.UTF8.GetBytes(Key);
- byte[] keyArray = Convert.FromBase64String(Key);
- byte[] toEncryptArray = Encoding.UTF8.GetBytes(EncryptStr);
- RijndaelManaged rDel = new RijndaelManaged();
- rDel.Key = keyArray;
- rDel.Mode = CipherMode.ECB;
- rDel.Padding = PaddingMode.PKCS7;
- ICryptoTransform cTransform = rDel.CreateEncryptor();
- byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- return Convert.ToBase64String(resultArray, 0, resultArray.Length);
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- /// <summary>
- /// AES 算法解密(ECB模式) 将密文base64解码进行解密,返回明文
- /// </summary>
- /// <param name="DecryptStr">密文</param>
- /// <param name="Key">密钥</param>
- /// <returns>明文</returns>
- public static string AesDecryptor_Base64(string DecryptStr, string Key)
- {
- try
- {
- //byte[] keyArray = Encoding.UTF8.GetBytes(Key);
- byte[] keyArray = Convert.FromBase64String(Key);
- byte[] toEncryptArray = Convert.FromBase64String(DecryptStr);
- RijndaelManaged rDel = new RijndaelManaged();
- rDel.Key = keyArray;
- rDel.Mode = CipherMode.ECB;
- rDel.Padding = PaddingMode.PKCS7;
- ICryptoTransform cTransform = rDel.CreateDecryptor();
- byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- return Encoding.UTF8.GetString(resultArray);// UTF8Encoding.UTF8.GetString(resultArray);
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- /// <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);
- }
- /// <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);
- }
- /// <summary>
- /// byte数组Hex编码
- /// </summary>
- /// <param name="bytes">需要进行编码的byte[]</param>
- /// <returns></returns>
- public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
- {
- string hexString = string.Empty;
- if (bytes != null)
- {
- StringBuilder strB = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- {
- strB.Append(bytes[i].ToString("X2"));
- }
- hexString = strB.ToString();
- }
- return hexString;
- }
- /// <summary>
- /// 字符串进行Hex解码(Hex.decodeHex())
- /// </summary>
- /// <param name="hexString">需要进行解码的字符串</param>
- /// <returns></returns>
- 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)
- {
- Application app = new Application();
- Document doc = null;
- object unknow = Type.Missing;
- app.Visible = true;
- object file = @"C:\Users\hp\Desktop\雷楚涵.docx";
- doc = app.Documents.Open(ref file,
- ref unknow, ref unknow, ref unknow, ref unknow,
- ref unknow, ref unknow, ref unknow, ref unknow,
- ref unknow, ref unknow, ref unknow, ref unknow,
- ref unknow, ref unknow, ref unknow);
- string temp = doc.Content.Text.Trim();
- string goal1 = AesEncryptor_Base64(temp, "11122233344455566677788812345678");
- Console.WriteLine("Base64加密之后:" + goal1);
- string goal2 = AesDecryptor_Base64(goal1, "11122233344455566677788812345678");
- Console.WriteLine("Base64解密之后:" + goal2);
- string goal3 = AesEncryptor_Hex(temp, "11122233344455566677788812345678");
- Console.WriteLine("Hex64加密之后:" + goal3);
- string goal4 = AesDecryptor_Hex(goal3, "11122233344455566677788812345678");
- Console.WriteLine("Hex解密之后:" + goal4);
- }
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|