BluetoothDecryptor.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using UnityEngine;
  3. public class BluetoothDecryptor
  4. {
  5. // 授权标志
  6. private static bool _authorizationFlg = false;
  7. // 密钥长度
  8. private const int KEY_LENGTH = 12;
  9. // 对称密钥
  10. private static readonly byte[] c_abySecretKey = System.Text.Encoding.ASCII.GetBytes("qingfengluan");
  11. private static readonly byte[] _decryptExpected = new byte[4];
  12. private static readonly byte[] _encryptExpected = new byte[4];
  13. /***********************************************************
  14. *@函 数 名 : AUTHOR_ImprovedEncrypt
  15. *@功能说明 : 加密函数
  16. *@形 参 : pbyData - 待加密字符串
  17. *@ dwDataLen - 加密数据长度
  18. *@返 回 值 : void
  19. *@作 者 : Shaoxin Wu
  20. *@日 期 : 2024-06-24
  21. ***********************************************************/
  22. private static void AUTHOR_ImprovedEncrypt(byte[] data, int offset, int length)
  23. {
  24. for (int i = offset; i < offset + length; i++)
  25. {
  26. for (int j = 0; j < KEY_LENGTH; j++)
  27. {
  28. data[i] ^= c_abySecretKey[j]; // 多轮异或
  29. data[i] = (byte)((data[i] << 1) | (data[i] >> 7)); // 左循环移位
  30. }
  31. }
  32. }
  33. /***********************************************************
  34. *@函 数 名 : AUTHOR_ImprovedDecrypt
  35. *@功能说明 : 解密函数
  36. *@形 参 : pbyData - 待解密字符串
  37. *@ dwDataLen - 解密数据长度
  38. *@返 回 值 : pbyData - 解密后的数据
  39. *@作 者 : Shaoxin Wu
  40. *@日 期 : 2024-06-24
  41. ***********************************************************/
  42. public static byte[] AUTHOR_ImprovedDecrypt(byte[] pbyData, uint dwDataLen)
  43. {
  44. for (uint i = 0; i < dwDataLen; i++)
  45. {
  46. for (int j = KEY_LENGTH - 1; j >= 0; j--)
  47. {
  48. pbyData[i] = (byte)((pbyData[i] >> 1) | (pbyData[i] << 7)); // 右循环移位
  49. pbyData[i] ^= c_abySecretKey[j]; // 多轮异或
  50. }
  51. }
  52. return pbyData;
  53. }
  54. /***********************************************************
  55. *@函 数 名 : CheckSum
  56. *@功能说明 : 累加和校验计算
  57. *@形 参 : pbyData - 数据地址
  58. *@ byDataLen - 校验数据长度
  59. *@返 回 值 : 校验和结果
  60. *@作 者 : Shaoxin Wu
  61. *@日 期 : 2024-06-24
  62. ***********************************************************/
  63. public static byte CheckSum(byte[] pbyData, byte byDataLen)
  64. {
  65. byte byRet = 0;
  66. for (byte i = 0; i < byDataLen; i++)
  67. {
  68. byRet += pbyData[i];
  69. }
  70. return byRet;
  71. }
  72. /// <summary>
  73. /// 最前面插入一个字节
  74. /// </summary>
  75. /// <param name="originalArray"></param>
  76. /// <param name="newByte"></param>
  77. /// <returns></returns>
  78. public static byte[] InsertByteAtBeginning(byte[] originalArray, byte newByte)
  79. {
  80. byte[] newArray = new byte[originalArray.Length + 1];
  81. newArray[0] = newByte;
  82. Array.Copy(originalArray, 0, newArray, 1, originalArray.Length);
  83. return newArray;
  84. }
  85. public static byte[] GetResponse(byte[] bytes) {
  86. //byte[] dataToDecrypt = { 0x5A, 0x76, 0xD1, 0x02, 0xEB, 0x8E, 0x5D };
  87. //需要解密的4个数据
  88. byte[] encryptedData = new byte[] { bytes[1], bytes[2], bytes[3], bytes[4] };
  89. uint dataLength = (uint)encryptedData.Length;
  90. byte checksumStart = CheckSum(new byte[] { bytes[0] , bytes[1], bytes[2], bytes[3], bytes[4] }, 5);
  91. // 打印开始校验和
  92. //Debug.Log("checksumStart: " + checksumStart + ",对应16进制=" + checksumStart.ToString("X"));
  93. // 打印原始加密数据
  94. Debug.Log("Original Data: " + System.BitConverter.ToString(bytes));
  95. //Debug.Log("Original Encrypted Data: " + System.BitConverter.ToString(encryptedData));
  96. // 解密数据
  97. byte[] decryptedData = AUTHOR_ImprovedDecrypt(encryptedData, dataLength);
  98. // 打印解密后的数据
  99. //Debug.Log("Decrypted Data: " + System.BitConverter.ToString(decryptedData));
  100. byte[] newArray = InsertByteAtBeginning(decryptedData, bytes[0]);
  101. //Debug.Log("前插 {0x5A} 后: " + System.BitConverter.ToString(newArray));
  102. // 计算累加和校验
  103. byte checksum = CheckSum(newArray, (byte)newArray.Length);
  104. // Debug.Log("checksum:" + checksum + ", 对应16进制:"+ checksum.ToString("X"));
  105. //byte checksum = Decryptor.CheckSum(decryptedData, (byte)dataLength);
  106. //// 打印校验和
  107. //Debug.Log("Checksum: " + checksum+",16=" + checksum.ToString("X"));
  108. // 组装应答数据
  109. byte[] responseData = new byte[7];
  110. responseData[0] = 0x5A; // 起始码
  111. System.Array.Copy(decryptedData, 0, responseData, 1, decryptedData.Length); // 解密数据
  112. responseData[5] = checksum; // 累加和校验
  113. responseData[6] = 0x5D; // 结束码
  114. // 将应答数据转换为字符串
  115. Debug.Log("Original Response Data: " + System.BitConverter.ToString(responseData));
  116. // string responseString = System.BitConverter.ToString(responseData).Replace("-", "");
  117. //Debug.Log("Response Data: " + responseString);
  118. return responseData;
  119. }
  120. /***********************************************************
  121. *@函 数 名 : AUTHOR_GenerateRandomData
  122. *@功能说明 : 生成随机数据函数
  123. *@形 参 : dwSeed - 种子值
  124. *@ pbyData - 存放随机数据的数组
  125. *@ dwDataLen - 随机数据长度
  126. *@返 回 值 : void
  127. *@作 者 : Shaoxin Wu
  128. *@日 期 : 2024-06-24
  129. ***********************************************************/
  130. private static void AUTHOR_GenerateRandomData(uint dwSeed, byte[] data, int offset, int length)
  131. {
  132. for (int i = 0; i < length; i++)
  133. {
  134. data[offset + i] = (byte)(dwSeed & 0xFF); // 简单地取低8位作为随机数据
  135. dwSeed >>= 1; // 移位
  136. }
  137. }
  138. /***********************************************************
  139. *@函 数 名 : AUTHOR_IsDecrypt
  140. *@功能说明 : 获取授权状态
  141. *@形 参 : void
  142. *@返 回 值 : void
  143. *@作 者 : Shaoxin Wu
  144. *@日 期 : 2024-07-28
  145. ***********************************************************/
  146. public static bool AUTHOR_IsDecrypt()
  147. {
  148. return _authorizationFlg;
  149. }
  150. /***********************************************************
  151. *@函 数 名 : AUTHOR_Decrypt
  152. *@功能说明 : 解析授权响应数据
  153. *@形 参 : void
  154. *@返 回 值 : void
  155. *@作 者 : Shaoxin Wu
  156. *@日 期 : 2024-07-28
  157. ***********************************************************/
  158. public static void AUTHOR_Decrypt(byte[] pbyData)
  159. {
  160. byte byCheckSum = CheckSum(pbyData, 5);
  161. if (byCheckSum == pbyData[5])
  162. {
  163. Array.Copy(pbyData, 1, _encryptExpected, 0, 4);
  164. if (BitConverter.ToUInt32(pbyData, 1) == BitConverter.ToUInt32(_decryptExpected, 0))
  165. {
  166. _authorizationFlg = true;
  167. }
  168. }
  169. }
  170. /***********************************************************
  171. *@函 数 名 : AUTHOR_Encryption
  172. *@功能说明 : 取消授权
  173. *@形 参 : void
  174. *@返 回 值 : void
  175. *@作 者 : Shaoxin Wu
  176. *@日 期 : 2024-07-28
  177. ***********************************************************/
  178. public static void AUTHOR_Encryption()
  179. {
  180. _authorizationFlg = false;
  181. }
  182. /***********************************************************
  183. *@函 数 名 : AUTHOR_SendReq
  184. *@功能说明 : 输入系统Tick,生成随机值并加密
  185. *@形 参 : void
  186. *@返 回 值 : void
  187. *@作 者 : Shaoxin Wu
  188. *@日 期 : 2024-07-28
  189. ***********************************************************/
  190. public static byte[] AUTHOR_SendReq(uint dwTick)
  191. {
  192. byte[] abySendData = new byte[7];
  193. abySendData[0] = 0x5A;
  194. // 根据种子生成随机值
  195. AUTHOR_GenerateRandomData(dwTick, abySendData, 1, 4);
  196. Array.Copy(abySendData, 1, _decryptExpected, 0, 4);
  197. // 加密数据
  198. AUTHOR_ImprovedEncrypt(abySendData, 1, 4);
  199. abySendData[5] = CheckSum(abySendData, 5);
  200. abySendData[6] = 0x5D;
  201. return abySendData;
  202. }
  203. //// 模拟发送数据
  204. //private static void SPP_DataNotify(byte[] data)
  205. //{
  206. // // 实际发送数据的实现
  207. // Console.WriteLine(BitConverter.ToString(data));
  208. //}
  209. }