根据USB 序列号 生成USB Key
- /// <summary>
- /// USB 操作相关
- /// </summary>
- public class UsbUtility
- {
- const string Key_File_Name = "Usb.key";
- /// <summary>
- /// 获取优盘唯一序列号,可能需要以管理员身份运行程序
- /// </summary>
- /// <returns></returns>
- public static string GetUSBId()
- {
- ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher("Select * From Win32_USBHub");
- string id = "";
- foreach (ManagementObject wmi_USB in wmiSearcher.Get())
- {
- string strPath = wmi_USB.Path.RelativePath;
- string[] strArr = strPath.Split('/"');
- string strTemp = strArr[1];
- //VID_ 是真正的优盘标示
- if (strTemp.StartsWith("USBVID_"))
- id += strTemp + ";";
- }
- return id.TrimEnd(';');
- }
- /// <summary>
- /// 获取当前UsbKey,根据当前附加的序列号,生成 "序列号,UsbId"格式的字符串,并返回加密后的结果
- /// 如果没有找到设备,那么返回空字符串
- /// 如果返回值为空,请检查 message 的值。
- /// </summary>
- /// <param name="sourceNumber">要附加的序列号</param>
- /// <param name="message">生成的操作信息</param>
- /// <returns>当前加密后的UsbKey字符串</returns>
- public static string GetCurrentUsbKey(string sourceNumber,out string message)
- {
- string strUsbId = GetUSBId();
- if (strUsbId != "")
- {
- if (strUsbId.Split(';').Length == 1)
- {
- CheckCode cc = new CheckCode();
- message = "OK";
- string strTemp = sourceNumber+","+strUsbId ;
- return cc.Encode(strTemp);
- }
- else
- {
- message = "当前有多个U盘,请拨除其它U盘。";
- return "";
- }
- }
- message = "未找到U盘或者U盘没有物理序列号。";
- return "";
- }
- /// <summary>
- /// 将一个新的UsbKey写到当前U盘上
- /// </summary>
- /// <param name="sourceNumber">U盘所在盘符</param>
- /// <param name="sourceNumber">要附加的序列号</param>
- /// <param name="message">生成的操作信息</param>
- /// <returns></returns>
- public static bool WriteUsbKeyFile(string driveName, string sourceNumber,out string message)
- {
- string strCurrentUsbKey = GetCurrentUsbKey(sourceNumber, out message);
- if (strCurrentUsbKey != "")
- {
- if (driveName.EndsWith("//"))
- driveName += "//";
- string strCurrKeyFileName = driveName + Key_File_Name;
- try
- {
- using (StreamWriter writer = File.CreateText(strCurrKeyFileName))
- {
- writer.Write(strCurrentUsbKey);
- writer.Flush();
- }
- message = "OK";
- return true;
- }
- catch (Exception ex)
- {
- message = ex.Message;
- }
- }
- return false;
- }
- /// <summary>
- /// 读取UsbKey文件的内容,返回内容格式为 "序列号,UsbId"格式的字符串
- /// 如果未能正确读取文件,返回空字符。
- /// </summary>
- /// <param name="driveName">U盘所在盘符</param>
- /// <param name="message">操作的消息</param>
- /// <returns>UsbKey文件的内容</returns>
- public static string ReadUsbKeyFile(string driveName, out string message)
- {
- if (driveName.EndsWith("//"))
- driveName += "//";
- string strCurrKeyFileName = driveName + Key_File_Name;
- message = "";
- try
- {
- string strResult = "";
- using (StreamReader reader = File.OpenText(strCurrKeyFileName))
- {
- strResult = reader.ReadToEnd();
- }
- if (strResult != "")
- {
- message = "OK";
- CheckCode cc = new CheckCode();
- return cc.Decode(strResult);
- }
- else
- {
- message = "未能正确读取文件,文件内容可能为空!";
- }
- }
- catch (Exception ex)
- {
- message = ex.Message;
- }
- return "";
- }
- }
- /// <summary>
- /// 检查编码情况
- /// </summary>
- class CheckCode
- {
- const string KEY_64 = "fasfgew";
- const string IV_64 = "JDLJMAMI"; //注意了,是8个字符,64位
- /// <summary>
- /// 加密
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public string Encode(string data)
- {
- byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
- byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
- DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
- int i = cryptoProvider.KeySize;
- MemoryStream ms = new MemoryStream();
- CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
- StreamWriter sw = new StreamWriter(cst);
- sw.Write(data);
- sw.Flush();
- cst.FlushFinalBlock();
- sw.Flush();
- return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
- }
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public string Decode(string data)
- {
- byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
- byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
- byte[] byEnc;
- try
- {
- byEnc = Convert.FromBase64String(data);
- }
- catch
- {
- return null;
- }
- DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
- MemoryStream ms = new MemoryStream(byEnc);
- CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
- StreamReader sr = new StreamReader(cst);
- return sr.ReadToEnd();
- }
- }
在主窗体,使用下面的代码调用:
- private void btnMakeUsbKey_Click(object sender, EventArgs e)
- {
- string strMessage = "";
- MakeProductNumbers();
- if (UsbUtility.WriteUsbKeyFile(CurrentUSBDriveName, ProductNumbers, out strMessage))
- {
- MessageBox.Show("Usb Key File 生成成功!");
- }
- else
- {
- MessageBox.Show(strMessage);
- }
- }
- private void btnShowUsbKey_Click(object sender, EventArgs e)
- {
- string strMessage = "";
- string strText = UsbUtility.ReadUsbKeyFile(CurrentUSBDriveName, out strMessage);
- if (strText!="")
- {
- MessageBox.Show(strText,"查看USBKEY");
- }
- else
- {
- MessageBox.Show(strMessage,"Error");
- }
- }
- private string _CurrentUSBDriveName = "";
- /// <summary>
- /// 当前USB 驱动器号
- /// </summary>
- public string CurrentUSBDriveName
- {
- get
- {
- if (_CurrentUSBDriveName == "")
- {
- string[] strDrivers = Environment.GetLogicalDrives();
- _CurrentUSBDriveName = strDrivers[strDrivers.Length - 1];
- }
- return _CurrentUSBDriveName;
- }
- set
- {
- _CurrentUSBDriveName = value ;
- }
- }
属性 CurrentUSBDriveName 会返回当前最后一个驱动器号,我们认为它是优盘,但是这不是很保险。为此,我们需要监视系统合适插入了U盘,并取得它的盘符。
- public const int WM_DEVICECHANGE = 0x219;
- public const int DBT_DEVICEARRIVAL = 0x8000;
- public const int DBT_CONFIGCHANGECANCELED = 0x0019;
- public const int DBT_CONFIGCHANGED = 0x0018;
- public const int DBT_CUSTOMEVENT = 0x8006;
- public const int DBT_DEVICEQUERYREMOVE = 0x8001;
- public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
- public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
- public const int DBT_DEVICEREMOVEPENDING = 0x8003;
- public const int DBT_DEVICETYPESPECIFIC = 0x8005;
- public const int DBT_DEVNODES_CHANGED = 0x0007;
- public const int DBT_QUERYCHANGECONFIG = 0x0017;
- public const int DBT_USERDEFINED = 0xFFFF;
- protected override void WndProc(ref Message m)
- {
- try
- {
- if (m.Msg == WM_DEVICECHANGE)
- {
- switch (m.WParam.ToInt32())
- {
- case WM_DEVICECHANGE:
- break;
- case DBT_DEVICEARRIVAL://U盘插入
- DriveInfo[] s = DriveInfo.GetDrives();
- foreach (DriveInfo drive in s)
- {
- if (drive.DriveType == DriveType.Removable)
- {
- mpnUC.CurrentUSBDriveName = drive.Name.ToString();
- //
- MessageBox.Show("U盘已插入,盘符为:" + drive.Name.ToString() + "/r/n");
- break;
- }
- }
- break;
- case DBT_CONFIGCHANGECANCELED:
- break;
- case DBT_CONFIGCHANGED:
- break;
- case DBT_CUSTOMEVENT:
- break;
- case DBT_DEVICEQUERYREMOVE:
- break;
- case DBT_DEVICEQUERYREMOVEFAILED:
- break;
- case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
- MessageBox.Show("U盘已卸载!");
- break;
- case DBT_DEVICEREMOVEPENDING:
- break;
- case DBT_DEVICETYPESPECIFIC:
- break;
- case DBT_DEVNODES_CHANGED:
- break;
- case DBT_QUERYCHANGECONFIG:
- break;
- case DBT_USERDEFINED:
- break;
- default:
- break;
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- base.WndProc(ref m);
- }
根据USB 序列号 生成USB Key相关推荐
- 使用STM32CubeMX 快速生成 USB HID 工程 - STM32F107VCT6
背景 最近在研究USB Device通信,如USB HID .USB Device. 使用STM32CubeMX,可以快速生成 USB HID .USB Device的工程. 学习USB,通过实践的方 ...
- python生成USB播放器歌单
用python生成USB播放器歌单 个人比较喜欢听歌,前段时间还买了个可以插U盘和储存卡的播放器,它支持直接按数字跳转歌曲,但自己却不知道歌曲对应的号码. 测试发现歌曲顺序并不是按文件名排序,也不是按 ...
- 使用STM32CubeMX生成USB驱动程序 USB无法检测到的问题 生成USB驱动
以STM32CubeMX生成USB驱动为例,其步骤如下: 一.使用STM32CubeMX生成USB驱动程序 打开STM32CubeMX软件,选择Start My project formMCU,点AC ...
- [FAQ10927][USB serial number客制化][系列6]:能否实现adb devices的序列号,usb serialnumber, cts device ID,SN一致?
[DESCRIPTION] 能否实现adb devices的序列号,usb serial number, cts device ID,SN一致? [KEYWORD] adb devices, Seri ...
- USB耳机方案|USB麦克风方案|SSS1629方案设计
USB耳机方案|USB麦克风方案|SSS1629方案设计 一.SSS1629/3S1629产品描述: SSS1629/3S1629除了传统 USB 耳麦芯片所具有的基本功能外,SSS1629-A5(3 ...
- linux usb组合设备,USB组合设备(5)
最近做一个组合设备,说白了就是在一个usb 设备上面,实现2个或者更多功能,但是说实话中文的资料还是比较少,没找到多少比较靠谱的,就去外文网站翻翻了 还是不少的,下面我将自己的理解理清改一下,希望也可 ...
- 关于流水帐表序列号生成时的并发操作问题
关于流水帐表序列号生成时的并发操作问题 1.某些流水张表的序号不是而是带有一定规则的, 如:四位的年份+2位周数+7位数字的序号 2007130000002. 假定表的定义如下: ...
- Android通过cat /sys/kernel/debug/usb/devices获取USB信息
cat /sys/kernel/debug/usb/devices得到的信息如下 下图是我们产品USB 拓扑图 USB设备通过debugfs导出/sys/kernel/debug/usb/device ...
- .NET平台开源项目速览(9)软件序列号生成组件SoftwareProtector介绍与使用
原文:.NET平台开源项目速览(9)软件序列号生成组件SoftwareProtector介绍与使用 在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,给大家初步介绍了 ...
最新文章
- Bitmap Cache
- 二维绕任意点旋转_二维图形复合线性变换程序设计:三角形绕任意点旋转2wfhbh...
- 龙芯完成.NET移植稳步推进生态建设
- 如何禁用计算机的服务,如何彻底禁用电脑中的迅雷服务XLservicePlatform
- C++之 字符串拷贝函数(面试题目)
- NetSuite二次开发服务改变中小企业发展困境!
- python学习笔记-Day5
- 序列标注模型结果评估模块seqeval学习使用
- rabbitMq多个队列多种任务同时监听
- 一文带你详解矩阵键盘工作原理
- sql 计算周环比wow_Oracle计算环比的方法
- 记一次阿里云面试心得
- taobao tt 配置
- 洛谷P1873 [COCI 2011/2012 #5] EKO / 砍树(二分法)
- python嵌入式系统开发_图解嵌入式系统开发之语言篇:初识Python
- 微信小程序常见面试题
- 大数据开发课程:大数据开发培训课程
- org.json.JSONException: Value [{“id“:10,“userId“:6,“adminId“:5,“content“:“7777“,“state“:-1,“image1“:
- 求解关系模式R的候选码
- python怎么撤销_关于Python:Python-如何删除/取消导入之前已导入的库