using 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++;
}
_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;
}
///
/// MD5加密
///
/// 待加密的字符串
/// 是否大写
/// 加密位数16或者32
///
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;
}
///
/// base64转普通字符串
///
/// base64字符串
///
private static string Base64ToString(string base64)
{
var res = "";
byte[] c = Convert.FromBase64String(base64);
res = System.Text.Encoding.UTF8.GetString(c);
return res;
}
///
/// 字符串转base64
///
/// 待转换的字符串
///
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
{
///
/// 常规
///
Standard = 0
}
}