/// <summary> /// 演示用户权限完成文件读写和加解密操作 /// </summary> /// <returns></returns> public static UInt32 UserUseDevice() { //1.枚举锁 //2.打开锁 //3.验证用户PIN //4.修改用户PIN //5.读只读文件 //6.修改可读写数据文件 //7.使用密钥文件加解密(AES128) //8.使用密钥文件(HMAC)计算Hash值 //9.关闭锁 UInt32 ret = MowayCS.MW_SUCCESS; UInt32 devInfoArraySize = 32; // 获取设备的最大数量 MW_DEVICE_INFO_CTX[] devInfoArray = new MW_DEVICE_INFO_CTX[devInfoArraySize]; UInt32 devCount = 0; IntPtr hHandle = IntPtr.Zero; //1.枚举锁 ret = MowayCS.mw_enum(devInfoArray, devInfoArraySize, ref devCount); if (ret != MowayCS.MW_SUCCESS) { Console.WriteLine("mw_enum failed, {0:X8}", ret); return(ret); } if (devCount == 0) { Console.WriteLine("Not found device"); return(ret); } //2.打开锁 ret = MowayCS.mw_open(ref devInfoArray[0], MowayCS.MW_OPEN_EXCLUSIVE_MODE, ref hHandle); if (ret != MowayCS.MW_SUCCESS) { Console.WriteLine("mw_open failed, {0:X8}", ret); return(ret); } //3.验证用户PIN ret = MowayCS.mw_verify_pin(hHandle, Convert.ToByte(MowayCS.MW_PIN_TYPE_USER), Encoding.ASCII.GetBytes(Program.userPIN)); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_verify_pin success PIN = {0}", Program.userPIN); } else { Console.WriteLine("mw_verify_pin failed, {0:X8}", ret); MowayCS.mw_close(hHandle); return(ret); } //4.修改用户PIN byte[] oldPIN = Encoding.ASCII.GetBytes(Program.userPIN); byte[] newPIN = Encoding.ASCII.GetBytes(Program.userPIN); // 本例仅用于演示接口使用方法,设置新PIN与旧PIN相同。 UInt16 limitCount = 0; // 使用默认值不限制重试次数 ret = MowayCS.mw_change_pin(hHandle, Convert.ToByte(MowayCS.MW_PIN_TYPE_USER), limitCount, oldPIN, newPIN); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_change_pin success oldPIN = {0}, newPIN = {1}.", Program.userPIN, Program.userPIN); } else { Console.WriteLine("mw_change_pin failed, {0:X8}", ret); } //5.读只读文件 byte[] readBuffer = null; UInt32 readBufferLen = 0; UInt32 readOffset = 0; readBuffer = new byte[128]; readBufferLen = (UInt32)userUseFileData.Length; ret = MowayCS.mw_read_file(hHandle, userRDFilename, readOffset, readBufferLen, readBuffer); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_read_file {0} success, {1}", userRDFilename, Encoding.ASCII.GetString(readBuffer)); } else { Console.WriteLine("mw_read_file {0} failed, {1:X8}", userRDFilename, ret); } //6.修改可读写数据文件 // 6.1 向可读写文件写入数据 byte[] writeBuffer = Encoding.ASCII.GetBytes("user privilege write!"); UInt32 writeOffset = 0; UInt32 writeBufferLen = (UInt32)writeBuffer.Length; ret = MowayCS.mw_write_file(hHandle, userRWFilename, writeOffset, writeBufferLen, writeBuffer); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_write_file {0} success.", userRWFilename); // 6.2 读取可读写文件内容 readBufferLen = (UInt32)readBuffer.Length; // 重置缓冲区长度 ret = MowayCS.mw_read_file(hHandle, userRWFilename, readOffset, readBufferLen, readBuffer); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_read_file {0} success, {1}", userRWFilename, Encoding.ASCII.GetString(readBuffer)); } else { Console.WriteLine("mw_read_file {0} failed, {1:X8}", userRWFilename, ret); } } else { Console.WriteLine("mw_write_file {0} failed, {1:X8}", userRWFilename, ret); } //7.使用密钥文件加解密(AES128) string encrypTestData = "1234567890123456"; byte[] data = Encoding.ASCII.GetBytes(encrypTestData); UInt32 dataLen = (UInt32)data.Length; byte[] iv = null; byte[] encryptData = new byte[128]; UInt32 encryptDataLen = (UInt32)encryptData.Length; byte[] decryptData = new byte[128]; UInt32 decryptDataLen = (UInt32)decryptData.Length; // 7.1 数据加密 ret = MowayCS.mw_sym_encrypt(hHandle, keyAES128Filename, Convert.ToByte(MowayCS.MW_SYM_ALGO_MODE_ECB), null, data, dataLen, encryptData, ref encryptDataLen); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_sym_encrypt({0}) success, encryptDataLen = {1}", keyAES128Filename, encryptDataLen); // 7.2 数据解密 ret = MowayCS.mw_sym_decrypt(hHandle, keyAES128Filename, Convert.ToByte(MowayCS.MW_SYM_ALGO_MODE_ECB), null, encryptData, encryptDataLen, decryptData, ref decryptDataLen); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_sym_decrypt {0} success, {1}", keyAES128Filename, Encoding.ASCII.GetString(decryptData)); // 比较加解密数据 if (Program.CompareBytes(data, decryptData, (int)dataLen)) { Console.WriteLine("data == decryptData"); } } else { Console.WriteLine("mw_sym_decrypt({0}) failed, {1:X8}", keyAES128Filename, ret); } } else { Console.WriteLine("mw_sym_encrypt {0} failed, {1:X8}", keyAES128Filename, ret); } //8.使用密钥文件(HMAC)计算Hash值 byte[] hmacData = new byte[128]; // 重置数据 UInt32 hmacDataLen = (UInt32)hmacData.Length; ret = MowayCS.mw_hmac_calc(hHandle, keyHMACMD5Filename, data, dataLen, hmacData, ref hmacDataLen); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_hmac_calc({0}) success, md5 = {1}", keyHMACMD5Filename, BitConverter.ToString(hmacData)); } else { Console.WriteLine("mw_hmac_calc {0} failed, {1:X8}", keyHMACMD5Filename, ret); } //9.关闭锁 MowayCS.mw_close(hHandle); return(MowayCS.MW_SUCCESS); }
/// <summary> /// B、用户导入升级包 /// </summary> /// <returns></returns> public static UInt32 UserUpdatePkg() { //1.枚举锁 //2.打开锁 //3.验证用户PIN //4.导入升级包 //5.验证升级包内容(读取验证内容与升级内容一致) //6.关闭锁 UInt32 ret = MowayCS.MW_SUCCESS; UInt32 devInfoArraySize = 32; // 获取设备的最大数量 MW_DEVICE_INFO_CTX[] devInfoArray = new MW_DEVICE_INFO_CTX[devInfoArraySize]; UInt32 devCount = 0; IntPtr hHandle = IntPtr.Zero; //1.枚举锁 ret = MowayCS.mw_enum(devInfoArray, devInfoArraySize, ref devCount); if (ret != MowayCS.MW_SUCCESS) { Console.WriteLine("mw_enum failed, {0:X8}", ret); return(ret); } if (devCount == 0) { Console.WriteLine("Not found device"); return(ret); } //2.打开锁 ret = MowayCS.mw_open(ref devInfoArray[0], MowayCS.MW_OPEN_EXCLUSIVE_MODE, ref hHandle); if (ret != MowayCS.MW_SUCCESS) { Console.WriteLine("mw_open failed, {0:X8}", ret); return(ret); } //3.验证用户PIN ret = MowayCS.mw_verify_pin(hHandle, Convert.ToByte(MowayCS.MW_PIN_TYPE_USER), Encoding.ASCII.GetBytes(Program.userPIN)); if (ret != MowayCS.MW_SUCCESS) { Console.WriteLine("mw_verify_pin failed, {0:X8}", ret); MowayCS.mw_close(hHandle); return(ret); } //4.导入升级包 // 注意:导入前已经调用 DeveloperMakeUpdatePkg() 生成数据包内容,并且当前加密锁的PID与升级包PID相同。 ret = MowayCS.mw_update(hHandle, pkgBuffer, pkgBufferLen); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_update success."); } else { Console.WriteLine("mw_update failed, {0:X8}", ret); } //5.验证升级包内容(读取验证内容与升级内容一致) // 5.1 读取升级后的锁内文件内容 byte[] readBuffer = null; UInt32 readBufferLen = 0; UInt32 readOffset = 0; readBuffer = new byte[128]; readBufferLen = (UInt32)readBuffer.Length; ret = MowayCS.mw_read_file(hHandle, updatePkgUpdateFilename, readOffset, readBufferLen, readBuffer); if (ret == MowayCS.MW_SUCCESS) { Console.WriteLine("mw_read_file {0} success, {1}", updatePkgUpdateFilename, Encoding.ASCII.GetString(readBuffer)); } else { Console.WriteLine("mw_read_file {0} failed, {1:X8}", updatePkgUpdateFilename, ret); } // 5.2 对比文件内容与升级包写入内容相同 FileStream fileStream = new FileStream(updatePkgUpdateLocalFilename, FileMode.Open); BinaryReader bReader = new BinaryReader(fileStream); byte[] updateDataBuffer = bReader.ReadBytes((int)fileStream.Length); int updateDataBufferLen = updateDataBuffer.Length; bReader.Close(); fileStream.Close(); if (Program.CompareBytes(readBuffer, updateDataBuffer, updateDataBufferLen)) { Console.WriteLine("mw_update success, readBuffer = updateDataBuffer"); } else { Console.WriteLine("mw_update failed, readBuffer != updateDataBuffer"); } //6.关闭锁 MowayCS.mw_close(hHandle); return(MowayCS.MW_SUCCESS); }