using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 加密
{
public partial class Form1 : Form
{
#region
//获取硬盘号
private string GetDiskID()
{
try
{
//获取硬盘ID
String HDid = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;
}
moc = null;
mc = null;
return HDid;
}
catch
{
return "";
}
finally
{
}
}
//获取CPU信息
private string GetCpuInfo()
{
try
{
string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
return cpuInfo;
}
catch
{
}
return "";
}
//加密, 设置一个默认的加密密钥
public static string Encrypt(string content, string secret = "Opew1et4")
{
StringBuilder ret = new StringBuilder();
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(content);
des.Key = ASCIIEncoding.ASCII.GetBytes(secret);
des.IV = ASCIIEncoding.ASCII.GetBytes(secret);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
}
catch (Exception ex)
{
throw ex;
}
return ret.ToString();
}
//解密
public static string Decrypt(string content, string secret = "Opew1et4")
{
MemoryStream ms = new MemoryStream();
if (content != null)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[content.Length / 2];
for (int x = 0; x < content.Length / 2; x++)
{
int i = (Convert.ToInt32(content.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(secret);
des.IV = ASCIIEncoding.ASCII.GetBytes(secret);
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
}
catch (Exception ex)
{
throw ex;
}
}
return Encoding.Default.GetString(ms.ToArray());
}
#endregion
string cpu_info, Hd_info, Encrypt_info,Decrypt_info;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
cpu_info = GetCpuInfo();
MessageBox.Show($"{cpu_info}");
Encrypt(cpu_info);
}
private void button2_Click(object sender, EventArgs e)
{
Hd_info = GetDiskID();
MessageBox.Show($"{Hd_info}");
}
private void button3_Click(object sender, EventArgs e)
{
Encrypt_info = Encrypt(cpu_info,"Opew1et4");
MessageBox.Show(Encrypt_info);
}
private void button4_Click(object sender, EventArgs e)
{
Decrypt_info = Decrypt(Encrypt_info,"Opew1et4");
if (Decrypt_info == cpu_info)
{
MessageBox.Show("Encrypt_info == cpu_info");
}
}
}
}
03-26
771
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
07-05
07-05
07-05
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交