翻译自:https://blog.youkuaiyun.com/zgjllf1011/article/details/79297512
作者代码是Java代码,然后自己翻译成C#代码。实现原理原作者博客中有详细介绍;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static String stringtable = "abcdefghijkmnpqrstuvwxyz23456789";
static String password = "dak3le2";
//从byte转为字符表索引所需要的位数
static int convertByteCount = 5;
static void Main(string[] args)
{
List<string> list = CreateCode((byte)10, 10000, 12, password);
//验证是否有重复数据
var query = from l in list group l by new { l} into g
select new
{
RepeatNameCount = g.Count()
};
int count = 0;
foreach (var item in query)
{
if (item.RepeatNameCount > 1)
{
count++;
}
}
Console.WriteLine("重复数据数量:"+ count);
Console.Read();
}
public static List<string> CreateCode(byte groupid, int codecount, int codelength, String password)
{
List<string> list = new List<string>();
//8位的数据总长度
int fullcodelength = codelength * convertByteCount / 8;
//随机码对时间和id同时做异或处理
//类型1,id4,随机码n,校验码1
int randcount = fullcodelength - 6;//随机码有多少个
//如果随机码小于0 不生成
if (randcount <= 0)
{
return null;
}
for (int i = 0; i < codecount; i++)
{
//这里使用i作为code的id
//生成n位随机码
byte[] randbytes = new byte[randcount];
for (int j = 0; j < randcount; j++)
{
Random dom = new Random();
double param = dom.Next(1, 10) / 10.0;
randbytes[j] = (byte)(param * Byte.MaxValue);
}
//存储所有数据
ByteHapper byteHapper = ByteHapper.CreateBytes(fullcodelength);
byteHapper.AppendNumber(groupid).AppendNumber(i).AppendBytes(randbytes);
//计算校验码 这里使用所有数据相加的总和与byte.max 取余
byte verify = (byte)(byteHapper.GetSum() % Byte.MaxValue);
byteHapper.AppendNumber(verify);
//使用随机码与时间和ID进行异或
for (int j = 0; j < 5; j++)
{
byteHapper.bytes[j] = (byte)(byteHapper.bytes[j] ^ (byteHapper.bytes[5 + j % randcount]));
}
//使用密码与所有数据进行异或来加密数据
byte[] passwordbytes = System.Text.Encoding.ASCII.GetBytes(password);
for (int j = 0; j < byteHapper.bytes.Length; j++)
{
byteHapper.bytes[j] = (byte)(byteHapper.bytes[j] ^ passwordbytes[j % passwordbytes.Length]);
}
//这里存储最终的数据
byte[] bytes = new byte[codelength];
//按6位一组复制给最终数组
for (int j = 0; j < byteHapper.bytes.Length; j++)
{
for (int k = 0; k < 8; k++)
{
int sourceindex = j * 8 + k;
int targetindex_x = sourceindex / convertByteCount;
int targetindex_y = sourceindex % convertByteCount;
byte placeval = (byte)Math.Pow(2, k);
byte val = (byte)((byteHapper.bytes[j] & placeval) == placeval ? 1 : 0);
//复制每一个bit
bytes[targetindex_x] = (byte)(bytes[targetindex_x] | (val << targetindex_y));
}
}
StringBuilder result = new StringBuilder();
//编辑最终数组生成字符串
for (int j = 0; j < bytes.Length; j++)
{
result.Append(stringtable.Substring(bytes[j],1));
}
Console.WriteLine(result.ToString());
list.Add(result.ToString());
}
return list;
}
}
public class ByteHapper
{
//原始数组
public byte[] bytes { get; set; }
//记录当前写入到多少位
int index;
private ByteHapper(int capacity)
{
bytes = new byte[capacity];
index = 0;
}
public static ByteHapper CreateBytes(int capacity)
{
ByteHapper byteHapper = new ByteHapper(capacity);
return byteHapper;
}
//向数组中追加内容
public ByteHapper AppendNumber(long val)
{
byte[] arr = Number2byte(val);
AppendBytes(arr);
return this;
}
public ByteHapper AppendNumber(int val)
{
byte[] arr = Number2byte(val);
AppendBytes(arr);
return this;
}
public ByteHapper AppendNumber(short val)
{
byte[] arr = Number2byte(val);
AppendBytes(arr);
return this;
}
public ByteHapper AppendNumber(byte val)
{
byte[] arr = new byte[] { val };
AppendBytes(arr);
return this;
}
/**
* 获取数据的总和
* @return
*/
public int GetSum()
{
int ret = 0;
for (int i = 0; i < bytes.Length; i++)
{
ret += bytes[i];
}
return ret;
}
//追加byte数组
public ByteHapper AppendBytes(byte[] arr)
{
for (byte i = 0; i < arr.Length; i++)
{
bytes[index + i] = arr[i];
}
index += arr.Length;
return this;
}
/**
* 将数字转换为byte数组
*/
public static byte[] Number2byte(long val)
{
byte[] arr = new byte[]{
(byte) ((val >> 56) & 0xFF),
(byte) ((val >> 48) & 0xFF),
(byte) ((val >> 40) & 0xFF),
(byte) ((val >> 32) & 0xFF),
(byte) ((val >> 24) & 0xFF),
(byte) ((val >> 16) & 0xFF),
(byte) ((val >> 8) & 0xFF),
(byte) (val & 0xFF)
};
return arr;
}
public static byte[] Number2byte(int val)
{
byte[] arr = new byte[]{
(byte) ((val >> 24) & 0xFF),
(byte) ((val >> 16) & 0xFF),
(byte) ((val >> 8) & 0xFF),
(byte) (val & 0xFF)
};
return arr;
}
public static byte[] Number2byte(short val)
{
byte[] arr = new byte[]{
(byte) ((val >> 8) & 0xFF),
(byte) (val & 0xFF)
};
return arr;
}
}
}