c#中自定义Base16编码解码的方法示例

2019-12-30 18:09:05王振洲

其实解码原理也很简单了,首先,将编码的字符串拆分成字符,其次,根据字符寻找到第一个和第二个字符的下标值。将第一个下标值转换成8位二进制,然后左移4位,与第二个字符的下标值合成一个字节,保存在字节数组里,最后,将保存好的字节数组根据默认编码转换成字符串。(解码我就不详加解释了)下面是解码的代码


/// <summary>
  /// 自定义Base16解码
  /// </summary>
  /// <param name="str">需要解码的字符串</param>
  /// <param name="autoCode">自定义Base16编码数组,16个元素,可以为数字、字符、特殊符号,若不填,使用默认的Base16编码数组,解码与编码的Base16编码数组一样</param>
  /// <returns></returns>
  public static string AutoBase16Decrypt(string str, string[] autoCode)
  {
   int k = 0;
   string dnStr = string.Empty;
   int strLength = str.Length;
   if (autoCode == null || autoCode.Length < 16)
    autoCode = new string[] { "a", "2", "B", "g", "E", "5", "f", "6", "C", "8", "o", "9", "Z", "p", "k", "M" };
   byte[] data = new byte[strLength / 2];
   for (int i = 0, j = 0; i < data.Length; i++, j++)
   {
    byte s = 0;
    int index1 = autoCode.ToList().IndexOf(str[j].ToString());
    j += 1;
    int index2 = autoCode.ToList().IndexOf(str[j].ToString());
    s = (byte)(s ^ index1);
    s = (byte)(s << 4);
    s = (byte)(s ^ index2);
    data[k] = s;
    k++;
   }
   dnStr = Encoding.Default.GetString(data);
   return dnStr;
  }

三、Base16编码数组解析

最后说一下这个编码数组,这个编码数组呢,是一个字符串数组,元素总数不能小于16个,当然超过16个元素,也用不上,这16个字符完全自定义,这个比较灵活,最后给大家看一个随机编码数组的函数。


/// <summary>
  /// 随机编码数组
  /// </summary>
  /// <returns></returns>
  public string[] RandomEncrypt()
  {
   string[] code = new string[16];
   Random random = new Random();
   int j = 0;
   for (int i = 0; 1 < 2; i++)
   {
    char ch = (char)random.Next(1, 128);
    if (code.ToList().IndexOf(ch.ToString()) < 0 && (( ch >= '0' && ch <= '9')
     || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')))
    {
     code[j] = ch.ToString();
     j++;
    }
    if (!Array.Exists(code, string.IsNullOrEmpty) && code.Length == 16)
     break;
   }
   return code;
  }

C# 16进制转换为Base64字符串


MatchCollection mc = Regex.Matches(retrunValue.ToString(), "[A-F0-9]{2}"); 
byte[] bytes = new byte[mc.Count]; 
for (int i = 0; i < mc.Count; i++) 
{ 
  bytes[i] = byte.Parse(mc[i].Value, System.Globalization.NumberStyles.HexNumber); 
} 
 
retrunValue = Convert.ToBase64String(bytes);