Countkey.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using Newtonsoft.Json.Linq;
  8. using SXLibrary;
  9. using Newtonsoft.Json;
  10. namespace MOKA_Factory_Tools
  11. {
  12. public class Keyinfo
  13. {
  14. public byte keyType { get; set; } = 0x00;
  15. public string keyValue { get; set; } = "";
  16. }
  17. /// <summary>
  18. /// 用于EEPROM key生成
  19. /// </summary>
  20. class Countkey
  21. {
  22. /// <summary>
  23. /// EepromKey的Key类型;
  24. /// </summary>
  25. public static Dictionary<string, Keyinfo> gKeydict = new Dictionary<string, Keyinfo>
  26. {
  27. { "mac", new Keyinfo{ keyType = 0x00 } },
  28. { "did", new Keyinfo{ keyType = 0x01 } },
  29. { "hdcp", new Keyinfo{ keyType =0x04 } },
  30. { "hdcp22", new Keyinfo{ keyType =0x05 } },
  31. { "widi", new Keyinfo{ keyType =0x05 } },
  32. { "esn", new Keyinfo{ keyType =0x07 } },
  33. { "widevine", new Keyinfo{ keyType =0x08 } },
  34. { "cikey", new Keyinfo{ keyType =0x09 } },
  35. { "attestation", new Keyinfo{ keyType =0x0E }},
  36. { "mgk", new Keyinfo{ keyType =0x0F } },
  37. { "playready", new Keyinfo{ keyType =0x10 }}
  38. };
  39. /// <summary>
  40. /// 字节数组转16进制字符串
  41. /// </summary>
  42. /// <param name="bytes"></param>
  43. /// <returns></returns>
  44. private static string byteToHexStr(byte[] bytes)
  45. {
  46. string returnStr = "";
  47. if (bytes != null)
  48. {
  49. for (int i = 0; i < bytes.Length; i++)
  50. {
  51. returnStr += bytes[i].ToString("X2");
  52. }
  53. }
  54. return returnStr;
  55. }
  56. public static bool Countfile(string path, string Type, out string result, out int num)
  57. {
  58. result = null;
  59. num = 0;
  60. DirectoryInfo getfile = new DirectoryInfo(path);
  61. foreach (FileInfo file in getfile.GetFiles())
  62. {
  63. num += 1;
  64. switch (Type)
  65. {
  66. case "MAC":
  67. {
  68. if (file.Length != 6)
  69. {
  70. result = file.FullName.ToString() + "length don't match MAC 6 byte rules";
  71. return false;
  72. }
  73. break;
  74. }
  75. case "DID":
  76. {
  77. if (file.Length != 40)
  78. {
  79. result = file.FullName.ToString() + "length don't match DID 40 byte rules";
  80. return false;
  81. }
  82. break;
  83. }
  84. case "ESN":
  85. {
  86. if (file.Length != 140)
  87. {
  88. result = file.FullName.ToString() + "length don't match ESN 140 byte rules";
  89. return false;
  90. }
  91. break;
  92. }
  93. case "burning key":
  94. {
  95. if (file.Length != 224)
  96. {
  97. result = file.FullName.ToString() + "length don't match burning key 224 byte rules";
  98. return false;
  99. }
  100. break;
  101. }
  102. default:
  103. {
  104. result = "Can find the key type";
  105. break;
  106. }
  107. }
  108. }
  109. return true;
  110. }
  111. static byte[] head1 = { 0x03, 0x00, 0x00, 0x00, 0x00, 0x26, 0x00, 0x06 };
  112. static byte[] head2 = { 0x01, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x28 };
  113. static byte[] head3 = { 0x07, 0x00, 0x00, 0x00, 0x54, 0x00, 0x8C };
  114. static byte[] FirstAddress = { 0x00, 0x00, 0x00, 0x26 };
  115. public static bool Conver2Dirct(string keys, out int countNotEmpty, out Dictionary<string, Keyinfo> Keydict, out string keyinfo)
  116. {
  117. keyinfo = "";
  118. Keydict = new Dictionary<string, Keyinfo>()
  119. {
  120. { "mac", new Keyinfo{ keyType = 0x00 } },
  121. { "did", new Keyinfo{ keyType = 0x01 } },
  122. { "hdcp", new Keyinfo{ keyType =0x04 } },
  123. { "hdcp22", new Keyinfo{ keyType =0x05 } },
  124. { "widi", new Keyinfo{ keyType =0x05 } },
  125. { "esn", new Keyinfo{ keyType =0x07 } },
  126. { "widevine", new Keyinfo{ keyType =0x08 } },
  127. { "cikey", new Keyinfo{ keyType =0x09 } },
  128. { "attestation", new Keyinfo{ keyType =0x0E }},
  129. { "mgk", new Keyinfo{ keyType =0x0F } },
  130. { "playready", new Keyinfo{ keyType =0x10 }}
  131. };
  132. countNotEmpty = 0;
  133. try
  134. {
  135. Dictionary<string, string> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(keys);
  136. foreach (var item in dict)
  137. {
  138. if (Keydict.ContainsKey(item.Key))
  139. {
  140. if (item.Key.Equals("mac"))
  141. Keydict[item.Key].keyValue = item.Value.Replace("-", "");
  142. else
  143. {
  144. if ( item.Key.Equals("did"))
  145. Keydict[item.Key].keyValue = byteToHexStr(Encoding.ASCII.GetBytes(item.Value));
  146. else
  147. Keydict[item.Key].keyValue = item.Value;
  148. }
  149. if (item.Value.Length > 0)
  150. {
  151. keyinfo += item.Key+";";
  152. countNotEmpty++;
  153. }
  154. }
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. Log.WriteErrorLog("\r\n合成EEPROM KEY失败:\r\n" + ex.Message);
  160. return false;
  161. }
  162. return true;
  163. }
  164. public static bool CreateEEPROMKey(string keys, string order, out string keyinfo, out string error)
  165. {
  166. error = "";
  167. keyinfo = "";
  168. int countNotEmpty;
  169. Dictionary<string, Keyinfo> Keydict;
  170. if (!Conver2Dirct(keys, out countNotEmpty, out Keydict, out keyinfo))
  171. return false;
  172. if (Keydict.ContainsKey("widi") && Keydict.ContainsKey("hdcp22"))
  173. {
  174. if (Keydict["widi"].keyValue.Length > 0 && Keydict["widi"].keyValue.Length > 0)
  175. {
  176. error = "Key包里面同时包含HDCP22和WiDikey,请检查!";
  177. return false;
  178. }
  179. }
  180. try
  181. {
  182. int LengthIndex = 0x00;
  183. int AddressIndex = 5 + 11 * countNotEmpty;
  184. List<byte> eepromKeyInfo = new List<byte>();
  185. List<byte> eepromKeyData = new List<byte>();
  186. eepromKeyInfo.Add((byte)countNotEmpty);
  187. foreach (var Item in Keydict)
  188. {
  189. byte[] _Keydata = null;
  190. byte[] _KeyCRC = null;
  191. byte[] _KeyLength = { 0x00 };
  192. byte[] _KeyAddress = { 0x00 };
  193. if (Item.Value.keyValue.Length > 0)
  194. {
  195. _Keydata = SerialInit.HexToByte(Item.Value.keyValue);
  196. _KeyCRC = BitConverter.GetBytes(CRC32.GetCRC32(_Keydata));
  197. Array.Reverse(_KeyCRC);
  198. eepromKeyInfo.Add(Item.Value.keyType);//添加类型
  199. _KeyAddress = BitConverter.GetBytes(AddressIndex + LengthIndex);
  200. Array.Reverse(_KeyAddress);
  201. eepromKeyInfo.AddRange(_KeyAddress);
  202. _KeyLength = CommonMethod.InttoBytelist(_Keydata.Length);
  203. if (_KeyLength.Length == 1)
  204. _KeyLength = new byte[2] { 0x00, _KeyLength[0] };
  205. eepromKeyInfo.AddRange(_KeyLength);
  206. eepromKeyInfo.AddRange(_KeyCRC);
  207. eepromKeyData.AddRange(_Keydata);
  208. LengthIndex = _Keydata.Length;
  209. AddressIndex = CommonMethod.BytelisttoInt(_KeyAddress);
  210. }
  211. }
  212. // 计算eepromKeyInfo的crc
  213. byte[] headfile = new byte[eepromKeyInfo.Count];
  214. eepromKeyInfo.CopyTo(headfile);
  215. byte[] WholeCrc = BitConverter.GetBytes(CRC32.GetCRC32(headfile));
  216. Array.Reverse(WholeCrc);
  217. eepromKeyInfo.AddRange(WholeCrc);
  218. byte[] bytekeydata = new byte[eepromKeyData.Count];
  219. eepromKeyData.CopyTo(bytekeydata);
  220. eepromKeyInfo.AddRange(bytekeydata);
  221. byte[] burningkey = new byte[eepromKeyInfo.Count];
  222. eepromKeyInfo.CopyTo(burningkey);
  223. Savefile(burningkey, order, Keydict["mac"].keyValue + Keydict["did"].keyValue);
  224. return true;
  225. }
  226. catch(Exception ex)
  227. {
  228. Log.WriteErrorLog("\r\n合成EEPROM KEY失败:\r\n" + ex.Message);
  229. return false;
  230. }
  231. }
  232. public static void Savefile(byte[] data, string order, string filename)
  233. {
  234. if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\burningkey\" + order) == false)
  235. {
  236. Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\burningkey\" + order);
  237. }
  238. FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"\burningkey\" + order + @"\" + filename, FileMode.Create);
  239. fs.Write(data, 0, data.Length);
  240. fs.Close();
  241. }
  242. }
  243. /// <summary>
  244. /// 用于生成FireTV key
  245. /// </summary>
  246. class FireTVkey
  247. {
  248. public static bool CreateFireTVKeys(string keys, string order, out string error)
  249. {
  250. try
  251. {
  252. error = "";
  253. JObject jObject = JObject.Parse(keys);
  254. string Mac = jObject["mac"].Value<string>().Replace("-", "");
  255. string WifiMac = jObject["wifimac"].Value<string>().Replace("-", "");
  256. string BTMac = jObject["btmac"].Value<string>().Replace("-", "");
  257. string HDCP = jObject["hdcp"].Value<string>().Trim();
  258. string HDCP22 = jObject["hdcp22"].Value<string>().Trim();
  259. string Widevine = jObject["widevine"].Value<string>().Trim();
  260. //byte[] bytedata = SerialInit.HexToByte(data);
  261. if (Mac.Trim().Length > 0)
  262. {
  263. string path = AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\MAC\\MAC";
  264. CommonMethod.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\MAC");
  265. if (!LocalTxtRecord.LocalRecord(Mac, path))
  266. return false;
  267. }
  268. if (WifiMac.Trim().Length > 0)
  269. {
  270. string path = AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\WifiMac\\WifiMac";
  271. CommonMethod.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\WifiMac");
  272. if (!LocalTxtRecord.LocalRecord(WifiMac, path))
  273. return false;
  274. }
  275. if (BTMac.Trim().Length > 0)
  276. {
  277. string path = AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\BTMac\\BTMac";
  278. CommonMethod.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\BTMac");
  279. if (!LocalTxtRecord.LocalRecord(BTMac, path))
  280. return false;
  281. }
  282. if (HDCP.Trim().Length > 0)
  283. {
  284. string path = AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\HDCP\\" + GetMD5.GetStrMd5(HDCP);
  285. CommonMethod.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\HDCP");
  286. if (!CommonMethod.GetCreateHexkey(path, HDCP, out error))
  287. return false;
  288. }
  289. if (HDCP22.Trim().Length > 0)
  290. {
  291. string path = AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\HDCP22\\" + GetMD5.GetStrMd5(HDCP22);
  292. CommonMethod.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\HDCP22");
  293. if (!CommonMethod.GetCreateHexkey(path, HDCP22, out error))
  294. return false;
  295. }
  296. if (Widevine.Trim().Length > 0)
  297. {
  298. string path = AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\Widevine\\" + GetMD5.GetStrMd5(Widevine);
  299. CommonMethod.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\burningkey\\" + order + "\\Widevine");
  300. if (!CommonMethod.GetCreateHexkey(path, Widevine, out error))
  301. return false;
  302. }
  303. return true;
  304. }
  305. catch (Exception ex)
  306. {
  307. error = ex.Message;
  308. return false;
  309. }
  310. }
  311. }
  312. }