Countkey.cs 13 KB

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