代码如下:
[C#] 纯文本查看 复制代码 private void Button_Reg_Click(object sender, RoutedEventArgs e)
{
string text = TextBox_Name.Text.ToString().Trim();
string text2 = DatePicker_EndDate.SelectedDate.Value.ToString("yyyyMMdd");
string text3 = EncodeString(TextBox_MachineCode.Text.ToString().Trim());
string text4 = CryptString(text3 + text + text2);
string text5 = ((text4.Length < 8) ? text4.PadLeft(8, '0') : text4.Substring(text4.Length - 8, 8));
string text6 = text5.Substring(0, 2);
string text7 = text5.Substring(2, 6);
TextBox_Code.Text = $"{text}-{text2}-{text3 + text6}-{text7}";
}
---------------------
private string EncodeString(string input, string prefix = "")
{
string text = CryptString(input);
if (text.Length >= 4)
{
return prefix + text.Substring(checked(text.Length - 4), 4);
}
return prefix + text.PadLeft(4, 'a');
}
----------------------------------
private string CryptString(string input)
{
try
{
MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] bytes = Encoding.UTF8.GetBytes(input);
bytes = mD5CryptoServiceProvider.ComputeHash(bytes);
StringBuilder stringBuilder = new StringBuilder();
byte[] array = bytes;
foreach (byte b in array)
{
stringBuilder.Append(b.ToString("x2").ToUpper());
}
return stringBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
-------------------------------------
设
text="ccde"
text2=20991231
TextBox_MachineCode.Text.= “CND9DF”
结果一直没算对,正确的是ccde-20991231-FEB60B-3A79F4
特别是private string CryptString(string input) 中 md5后后面没看懂,期待哪位高手解惑,多谢。“x2”直接加在文本串后面?
|