From c88a5ef4560a8bcf6763b192069a83ede0bb3908 Mon Sep 17 00:00:00 2001
From: Moli0 <Moli0>
Date: 星期二, 15 二月 2022 09:43:17 +0800
Subject: [PATCH] 添加编码解码方法类
---
Coding/MyCoding.cs | 315 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 315 insertions(+), 0 deletions(-)
diff --git a/Coding/MyCoding.cs b/Coding/MyCoding.cs
new file mode 100644
index 0000000..931a22b
--- /dev/null
+++ b/Coding/MyCoding.cs
@@ -0,0 +1,315 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Test.MethodClass.Coding
+{
+ public class MyCoding
+ {
+ public static string Encoding(string str)
+ {
+ var res = Encoding(str, "asdbvsosdf");
+ return res;
+ }
+
+ public static string Decoding(string str)
+ {
+ var res = Decoding(str, "asdbvsosdf");
+ return res;
+ }
+
+
+ private static string Encoding(string str,string key, CodingType type = CodingType.Standard,int offset = 0)
+ {
+ string res = "";
+ offset += 21;
+ int countLeng = 1024 + 32;
+ str = StringToBase64(str);
+ byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
+
+ var newKey = MD5(key, true, 32);
+ int index = b.Length % (1024) == 0 ? b.Length / (1024) : b.Length / (1024) + 1;
+ byte[][] bs = new byte[index][];
+ while (index > 0)
+ {
+ index--;
+
+ int positionKey = (((index + offset) * index) + offset);
+ string _key = MD5(newKey + positionKey.ToString(), true, 32);
+ //_key = _key.Substring(0,(b.Length % 32));
+ byte[] _keyb = System.Text.Encoding.UTF8.GetBytes(_key);
+ byte[] _newb = new byte[_keyb.Length + countLeng]; //鏂板瓧绗﹂暱搴�
+
+ if (index == (bs.Length-1))
+ {
+ _newb = new byte[_keyb.Length + b.Length - (index * 1024)]; //鏂板瓧绗﹂暱搴�
+ }
+ else { }
+
+ int _newbIndex = 0;
+ int _keybIndex = 1;
+ int _bIndex = 0;
+ if (_newb.Length > 32 * 2)
+ {
+ //瀵嗘枃闀垮害澶т簬娣锋穯鍙傛暟闀垮害
+ while (_newbIndex < _newb.Length)
+ {
+
+ if (_newbIndex == (_newb.Length / _keyb.Length) * _keybIndex)
+ {
+ //濡傛灉褰撳墠浣嶇疆绛変簬娣疯偞鍙傛暟鐨勪綅缃紝鍒欐彃鍏ユ贩鑲村弬鏁�
+ _newb[_newbIndex] = _keyb[_keybIndex - 1];
+ _keybIndex++;
+ }
+ else
+ {
+ //鍚﹀垯鎻掑叆鏍囧噯鏂囦綋
+ _newb[_newbIndex] = b[(index * 1024) + _bIndex];
+ _bIndex++;
+ }
+
+ //if (_newbIndex == (_newb.Length / _keyb.Length * _keybIndex) - 1)
+ //{
+ // //濡傛灉褰撳墠浣嶇疆绛変簬娣疯偞鍙傛暟鐨勪綅缃紝鍒欐彃鍏ユ贩鑲村弬鏁�
+ // _newb[_newbIndex] = _keyb[_keybIndex - 1];
+ // if (_keybIndex < _keyb.Length)
+ // {
+ // _keybIndex++;
+ // }
+ //}
+ //else
+ //{
+ // //鍚﹀垯鎻掑叆鏍囧噯鏂囦綋
+ // _newb[_newbIndex] = b[(index * 1024) + _bIndex];
+ // _bIndex++;
+ //}
+ _newbIndex++;
+ }
+ }
+ else
+ {
+ //瀵嗘枃闀垮害灏忎簬娣锋穯鍙傛暟闀垮害
+ while (_newbIndex < _newb.Length)
+ {
+
+ if (_newbIndex == (_newb.Length / (_newb.Length - _keyb.Length) * _bIndex))
+ {
+ //濡傛灉褰撳墠浣嶇疆绛変簬瀵嗘枃鐨勪綅缃紝鍒欐彃鍏ュ瘑鏂�
+ _newb[_newbIndex] = b[(index * 1024) + _bIndex];
+ _bIndex++;
+ }
+ else
+ {
+ //鍚﹀垯鎻掑叆娣锋穯鍙傛暟
+ _newb[_newbIndex] = _keyb[_keybIndex - 1];
+ if (_keybIndex < _keyb.Length)
+ {
+ _keybIndex++;
+ }
+ }
+ _newbIndex++;
+ }
+ }
+ bs[index] = _newb;
+ }
+ int resByteLeng = 0;
+
+ foreach (var a in bs)
+ {
+ resByteLeng += a.Length;
+ }
+
+ byte[] allByte = new byte[resByteLeng];
+ int allByteIndex = 0;
+ foreach (var a in bs)
+ {
+ foreach (var c in a)
+ {
+ allByte[allByteIndex] = c;
+ allByteIndex++;
+ }
+ }
+ res = System.Text.Encoding.UTF8.GetString(allByte);
+
+ return res;
+ }
+
+ private static string Decoding(string str, string key, CodingType type = CodingType.Standard, int offset = 0)
+ {
+ string res = "";
+ offset += 21;
+ int countLeng = 1024 + 32;
+ byte[] strb = System.Text.Encoding.UTF8.GetBytes(str);
+ int leng = strb.Length % countLeng == 0 ? (strb.Length / countLeng) : ((strb.Length / countLeng) + 1);
+ byte[][] bs = new byte[leng][];
+
+ var newKey = MD5(key, true, 32);
+
+ for (int i = 0; i < leng; i++)
+ {
+ int positionKey = (((i + offset) * i) + offset);
+ string _key = MD5(newKey + positionKey.ToString(), true, 32);
+ byte[] _keyb = System.Text.Encoding.UTF8.GetBytes(_key);
+
+
+ byte[] tempb = new byte[1024];
+ int tempIndex = 0;
+ int tempKeyIndex = 1;
+
+ if (i == leng - 1)
+ {
+ //鏈�鍚庝竴涓�
+ var lastLeng = strb.Length - (i * countLeng);
+ tempb = new byte[lastLeng - _keyb.Length];
+ if (lastLeng > 32 * 2)
+ {
+ while (tempIndex < tempb.Length)
+ {
+ if (tempIndex == ((lastLeng / tempb.Length) * tempIndex)+ (tempKeyIndex - 1))
+ {
+ tempKeyIndex++;
+ }
+ else {
+ int strbIndex = ((countLeng * i) + tempIndex + (tempKeyIndex - 1));
+ tempb[tempIndex] = strb[strbIndex];
+ tempIndex++;
+ }
+ }
+ }
+ else
+ {
+ var __index = 0;
+ while (__index < lastLeng)
+ {
+
+ if (__index == ((lastLeng / tempb.Length) * tempIndex))
+ {
+ int strbIndex = ((countLeng * i) + tempIndex + (tempKeyIndex - 1));
+ tempb[tempIndex] = strb[strbIndex];
+ tempIndex++;
+ }
+ else
+ {
+ tempKeyIndex++;
+ }
+ __index++;
+ }
+ }
+ }
+ else
+ {
+ //鍏朵粬
+ while (tempIndex < tempb.Length)
+ {
+ if (tempIndex == (((countLeng / _keyb.Length) * tempKeyIndex) - 1))
+ {
+ tempKeyIndex++;
+ }
+ else { }
+ int strbIndex = ((countLeng * i) + tempIndex + (tempKeyIndex - 1));
+ tempb[tempIndex] = strb[strbIndex];
+ tempIndex++;
+ }
+ }
+ bs[i] = tempb;
+ }
+
+ int allLeng = 0;
+ foreach (var a in bs)
+ {
+ allLeng += a.Length;
+ }
+
+ var resb = new byte[allLeng];
+ int resbIndex = 0;
+ foreach (var a in bs)
+ {
+ foreach (var c in a)
+ {
+ resb[resbIndex++] = c;
+ }
+ }
+
+ res = Base64ToString(System.Text.Encoding.UTF8.GetString(resb));
+
+ return res;
+ }
+
+
+
+ /// <summary>
+ /// MD5鍔犲瘑
+ /// </summary>
+ /// <param name="str">寰呭姞瀵嗙殑瀛楃涓�</param>
+ /// <param name="IsUpper">鏄惁澶у啓</param>
+ /// <param name="bit">鍔犲瘑浣嶆暟16鎴栬��32</param>
+ /// <returns></returns>
+ private static string MD5(string str, bool IsUpper, int bit)
+ {
+ System.Security.Cryptography.MD5 md = new System.Security.Cryptography.MD5CryptoServiceProvider();
+ byte[] byteStream = System.Text.Encoding.UTF8.GetBytes(str);
+ byte[] temp = md.ComputeHash(byteStream);
+ StringBuilder tempStr = new StringBuilder();
+ string result = null;
+ foreach (var a in temp)
+ {
+ tempStr.Append(a.ToString("x2"));
+ }
+ if (IsUpper)
+ {
+ result = tempStr.ToString().ToUpper();
+ }
+ else
+ {
+ result = tempStr.ToString().ToLower();
+ }
+ if (bit == 16)
+ {
+ result = result.Substring(8, 16);
+ }
+ else if (bit == 32)
+ {
+ result = result.ToString();
+ }
+ else
+ {
+ result = string.Empty;
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// base64杞櫘閫氬瓧绗︿覆
+ /// </summary>
+ /// <param name="base64">base64瀛楃涓�</param>
+ /// <returns></returns>
+ private static string Base64ToString(string base64)
+ {
+ var res = "";
+ byte[] c = Convert.FromBase64String(base64);
+ res = System.Text.Encoding.UTF8.GetString(c);
+ return res;
+ }
+
+ /// <summary>
+ /// 瀛楃涓茶浆base64
+ /// </summary>
+ /// <param name="str">寰呰浆鎹㈢殑瀛楃涓�</param>
+ /// <returns></returns>
+ private static string StringToBase64(string str)
+ {
+ var res = "";
+ var b = System.Text.Encoding.UTF8.GetBytes(str);
+ res = Convert.ToBase64String(b);
+ return res;
+ }
+ }
+
+ internal enum CodingType
+ {
+ /// <summary>
+ /// 甯歌
+ /// </summary>
+ Standard = 0
+ }
+}
--
Gitblit v1.9.1