From 6396dac27ca99e84a2e3c772fb079bceddf67ff8 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期一, 01 十二月 2025 18:17:45 +0800
Subject: [PATCH] feat:开发
---
ApiTools.Core/Utils/WxmpUtils/Crypto/Cryptography.cs | 232 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 232 insertions(+), 0 deletions(-)
diff --git a/ApiTools.Core/Utils/WxmpUtils/Crypto/Cryptography.cs b/ApiTools.Core/Utils/WxmpUtils/Crypto/Cryptography.cs
new file mode 100644
index 0000000..05b5f77
--- /dev/null
+++ b/ApiTools.Core/Utils/WxmpUtils/Crypto/Cryptography.cs
@@ -0,0 +1,232 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Security.Cryptography;
+using System.IO;
+using System.Net;
+namespace Tencent
+{
+ class Cryptography
+ {
+ public static UInt32 HostToNetworkOrder(UInt32 inval)
+ {
+ UInt32 outval = 0;
+ for (int i = 0; i < 4; i++)
+ outval = (outval << 8) + ((inval >> (i * 8)) & 255);
+ return outval;
+ }
+
+ public static Int32 HostToNetworkOrder(Int32 inval)
+ {
+ Int32 outval = 0;
+ for (int i = 0; i < 4; i++)
+ outval = (outval << 8) + ((inval >> (i * 8)) & 255);
+ return outval;
+ }
+ /// <summary>
+ /// 瑙e瘑鏂规硶
+ /// </summary>
+ /// <param name="Input">瀵嗘枃</param>
+ /// <param name="EncodingAESKey"></param>
+ /// <returns></returns>
+ ///
+ public static string AES_decrypt(String Input, string EncodingAESKey, ref string appid)
+ {
+ byte[] Key;
+ Key = Convert.FromBase64String(EncodingAESKey + "=");
+ byte[] Iv = new byte[16];
+ Array.Copy(Key, Iv, 16);
+ byte[] btmpMsg = AES_decrypt(Input, Iv, Key);
+
+ int len = BitConverter.ToInt32(btmpMsg, 16);
+ len = IPAddress.NetworkToHostOrder(len);
+
+
+ byte[] bMsg = new byte[len];
+ byte[] bAppid = new byte[btmpMsg.Length - 20 - len];
+ Array.Copy(btmpMsg, 20, bMsg, 0, len);
+ Array.Copy(btmpMsg, 20+len , bAppid, 0, btmpMsg.Length - 20 - len);
+ string oriMsg = Encoding.UTF8.GetString(bMsg);
+ appid = Encoding.UTF8.GetString(bAppid);
+
+
+ return oriMsg;
+ }
+
+ public static String AES_encrypt(String Input, string EncodingAESKey, string appid)
+ {
+ byte[] Key;
+ Key = Convert.FromBase64String(EncodingAESKey + "=");
+ byte[] Iv = new byte[16];
+ Array.Copy(Key, Iv, 16);
+ string Randcode = CreateRandCode(16);
+ byte[] bRand = Encoding.UTF8.GetBytes(Randcode);
+ byte[] bAppid = Encoding.UTF8.GetBytes(appid);
+ byte[] btmpMsg = Encoding.UTF8.GetBytes(Input);
+ byte[] bMsgLen = BitConverter.GetBytes(HostToNetworkOrder(btmpMsg.Length));
+ byte[] bMsg = new byte[bRand.Length + bMsgLen.Length + bAppid.Length + btmpMsg.Length];
+
+ Array.Copy(bRand, bMsg, bRand.Length);
+ Array.Copy(bMsgLen, 0, bMsg, bRand.Length, bMsgLen.Length);
+ Array.Copy(btmpMsg, 0, bMsg, bRand.Length + bMsgLen.Length, btmpMsg.Length);
+ Array.Copy(bAppid, 0, bMsg, bRand.Length + bMsgLen.Length + btmpMsg.Length, bAppid.Length);
+
+ return AES_encrypt(bMsg, Iv, Key);
+
+ }
+ private static string CreateRandCode(int codeLen)
+ {
+ string codeSerial = "2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z";
+ if (codeLen == 0)
+ {
+ codeLen = 16;
+ }
+ string[] arr = codeSerial.Split(',');
+ string code = "";
+ int randValue = -1;
+ Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
+ for (int i = 0; i < codeLen; i++)
+ {
+ randValue = rand.Next(0, arr.Length - 1);
+ code += arr[randValue];
+ }
+ return code;
+ }
+
+ private static String AES_encrypt(String Input, byte[] Iv, byte[] Key)
+ {
+ var aes = new RijndaelManaged();
+ //绉橀挜鐨勫ぇ灏忥紝浠ヤ綅涓哄崟浣�
+ aes.KeySize = 256;
+ //鏀寔鐨勫潡澶у皬
+ aes.BlockSize = 128;
+ //濉厖妯″紡
+ aes.Padding = PaddingMode.PKCS7;
+ aes.Mode = CipherMode.CBC;
+ aes.Key = Key;
+ aes.IV = Iv;
+ var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
+ byte[] xBuff = null;
+
+ using (var ms = new MemoryStream())
+ {
+ using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
+ {
+ byte[] xXml = Encoding.UTF8.GetBytes(Input);
+ cs.Write(xXml, 0, xXml.Length);
+ }
+ xBuff = ms.ToArray();
+ }
+ String Output = Convert.ToBase64String(xBuff);
+ return Output;
+ }
+
+ private static String AES_encrypt(byte[] Input, byte[] Iv, byte[] Key)
+ {
+ var aes = new RijndaelManaged();
+ //绉橀挜鐨勫ぇ灏忥紝浠ヤ綅涓哄崟浣�
+ aes.KeySize = 256;
+ //鏀寔鐨勫潡澶у皬
+ aes.BlockSize = 128;
+ //濉厖妯″紡
+ //aes.Padding = PaddingMode.PKCS7;
+ aes.Padding = PaddingMode.None;
+ aes.Mode = CipherMode.CBC;
+ aes.Key = Key;
+ aes.IV = Iv;
+ var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
+ byte[] xBuff = null;
+
+ #region 鑷繁杩涜PKCS7琛ヤ綅锛岀敤绯荤粺鑷繁甯︾殑涓嶈
+ byte[] msg = new byte[Input.Length + 32 - Input.Length % 32];
+ Array.Copy(Input, msg, Input.Length);
+ byte[] pad = KCS7Encoder(Input.Length);
+ Array.Copy(pad, 0, msg, Input.Length, pad.Length);
+ #endregion
+
+ #region 娉ㄩ噴鐨勪篃鏄竴绉嶆柟娉曪紝鏁堟灉涓�鏍�
+ //ICryptoTransform transform = aes.CreateEncryptor();
+ //byte[] xBuff = transform.TransformFinalBlock(msg, 0, msg.Length);
+ #endregion
+
+ using (var ms = new MemoryStream())
+ {
+ using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
+ {
+ cs.Write(msg, 0, msg.Length);
+ }
+ xBuff = ms.ToArray();
+ }
+
+ String Output = Convert.ToBase64String(xBuff);
+ return Output;
+ }
+
+ private static byte[] KCS7Encoder(int text_length)
+ {
+ int block_size = 32;
+ // 璁$畻闇�瑕佸~鍏呯殑浣嶆暟
+ int amount_to_pad = block_size - (text_length % block_size);
+ if (amount_to_pad == 0)
+ {
+ amount_to_pad = block_size;
+ }
+ // 鑾峰緱琛ヤ綅鎵�鐢ㄧ殑瀛楃
+ char pad_chr = chr(amount_to_pad);
+ string tmp = "";
+ for (int index = 0; index < amount_to_pad; index++)
+ {
+ tmp += pad_chr;
+ }
+ return Encoding.UTF8.GetBytes(tmp);
+ }
+ /**
+ * 灏嗘暟瀛楄浆鍖栨垚ASCII鐮佸搴旂殑瀛楃锛岀敤浜庡鏄庢枃杩涜琛ョ爜
+ *
+ * @param a 闇�瑕佽浆鍖栫殑鏁板瓧
+ * @return 杞寲寰楀埌鐨勫瓧绗�
+ */
+ static char chr(int a)
+ {
+
+ byte target = (byte)(a & 0xFF);
+ return (char)target;
+ }
+ private static byte[] AES_decrypt(String Input, byte[] Iv, byte[] Key)
+ {
+ RijndaelManaged aes = new RijndaelManaged();
+ aes.KeySize = 256;
+ aes.BlockSize = 128;
+ aes.Mode = CipherMode.CBC;
+ aes.Padding = PaddingMode.None;
+ aes.Key = Key;
+ aes.IV = Iv;
+ var decrypt = aes.CreateDecryptor(aes.Key, aes.IV);
+ byte[] xBuff = null;
+ using (var ms = new MemoryStream())
+ {
+ using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
+ {
+ byte[] xXml = Convert.FromBase64String(Input);
+ byte[] msg = new byte[xXml.Length + 32 - xXml.Length % 32];
+ Array.Copy(xXml, msg, xXml.Length);
+ cs.Write(xXml, 0, xXml.Length);
+ }
+ xBuff = decode2(ms.ToArray());
+ }
+ return xBuff;
+ }
+ private static byte[] decode2(byte[] decrypted)
+ {
+ int pad = (int)decrypted[decrypted.Length - 1];
+ if (pad < 1 || pad > 32)
+ {
+ pad = 0;
+ }
+ byte[] res = new byte[decrypted.Length - pad];
+ Array.Copy(decrypted, 0, res, 0, decrypted.Length - pad);
+ return res;
+ }
+ }
+}
--
Gitblit v1.9.1