1 Star 0 Fork 0

vinge_ven / nwSerial

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Form1.cs 5.56 KB
一键复制 编辑 原始数据 按行查看 历史
vinge_ven 提交于 2024-03-10 16:12 . 添加项目文件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WindowsFormsencrapt
{
/// <summary>
/// 给女娲生成有效序列号的程序,把三个字母拆开BIT分散到每个字符里
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<string> rlist = new List<string>(); //只要没关程序,就保证不重复
private void btTest_Click(object sender, EventArgs e)
{
int t = int.Parse(textType.Text);
int num = int.Parse(textNum.Text);
textResult.Text = "";
string temp = "";
int startIndex = rlist.Count;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num; i++)
{
temp = Encrypt(textBox1.Text, t);
while (rlist.Contains(temp))
{
temp = Encrypt(textBox1.Text, t);
}
rlist.Add(temp);
textBox2.Text= temp;
}
int index = 1;
textResult.Text = "";
int j = 0;
for (j = startIndex;j<rlist.Count ;j++)
{
sb.Append((index).ToString() + ": " + rlist[j] + "\r\n");
index++;
}
textResult.Text= sb.ToString();
//int rest = 0;
//textBox3.Text = Decrypt(textBox2.Text, ref rest);
//textBox5.Text= rest.ToString();
}
//ct 0-3 作为加密的变位标志,让变化更多
public string Encrypt(string plainText,int ct)
{
if (plainText.Length != 3)
{
throw new ArgumentException("The input string must be 3 characters long.");
}
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 24; i++)
{
int bitIndex = i % 8;
char c = i < 8 ? plainText[0] : i < 16 ? plainText[1] : plainText[2];
int bitValue = (c >> bitIndex) & 1;
//string validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
List<char> matchingChars = new List<char>();
for (int j = 0; j < validChars.Length; j++)
{
int temp = (int)validChars[j];
if (((temp >> bitIndex) & 1) == bitValue)
{
matchingChars.Add(validChars[j]);
}
}
if ((i == 7)|| (i == 15))
{
matchingChars = matchingChars.Where(o =>((int)o<89)&& ((int)o& 1) == 0).ToList();
}
char encryptedChar = matchingChars[random.Next(matchingChars.Count)];
if (i == 7)
{
encryptedChar = (char)((int)encryptedChar + (ct&1));
}else if(i==15)
{
encryptedChar = (char)((int)encryptedChar + ((ct>>1) & 1));
}
sb.Append(encryptedChar);
}
return AddCross(sb.ToString());
}
public string Decrypt(string encryptedText, ref int ct)
{
encryptedText=encryptedText.Replace("-", "");
if (encryptedText.Length != 24)
{
throw new ArgumentException("The input string must be 24 characters long.");
}
ct = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++)
{
int charValue = 0;
for (int j = 0; j < 8; j++)
{
int bitIndex = i * 8 + j;
char encryptedChar = encryptedText[bitIndex];
int bitValue = (encryptedChar >> j) & 1;
charValue += bitValue << j;
if (i == 0 && j == 7)
{
ct = encryptedChar & 1;
}
else if (i == 1 && j == 7)
{
ct += (encryptedChar & 1) << 1;
}
}
sb.Append((char)charValue);
}
return sb.ToString();
}
public string AddCross(string srcTet)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < srcTet.Length; i += 4)
{
sb.Append(srcTet.Substring(i, Math.Min(4, srcTet.Length - i)));
if (i + 4 < srcTet.Length)
{
sb.Append("-");
}
}
return sb.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
int rest = 0;
textBox3.Text = Decrypt(textBox2.Text, ref rest);
textBox5.Text= rest.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
}
}
1
https://gitee.com/vinge/nwSerial.git
git@gitee.com:vinge/nwSerial.git
vinge
nwSerial
nwSerial
master

搜索帮助