Advertisement
Guest User

Untitled

a guest
Dec 24th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace nfs2iso
  9. {
  10. class Program
  11. {
  12. public const int SECTOR_SIZE = 0x8000;
  13. public const int HEADER_SIZE = 0x200;
  14. public static readonly byte[] VWII_COMMON_KEY = !!!!ADD THIS YOURSELF!!!
  15. public static readonly byte[] WII_COMMON_KEY = !!!!ADD THIS YOURSELF!!!
  16.  
  17. static void Main(string[] args)
  18. {
  19. Console.WriteLine();
  20. string dir = Directory.GetCurrentDirectory();
  21. string folder = new DirectoryInfo(dir).Name;
  22. if (folder.Length != 7 || String.Compare("content", 0, folder, 0, 7, false) != 0)
  23. {
  24. Console.WriteLine("Wrong directory! Place this program in the 'content' folder.");
  25. return;
  26. }
  27. Console.WriteLine("Searching for key file...");
  28. string keyDir = Directory.GetParent(dir)+"\\code"+"\\htk.bin";
  29. if (!File.Exists(keyDir))
  30. {
  31. Console.WriteLine("Could not find the file ..\\code\\htk.bin.");
  32. return;
  33. }
  34. byte[] key = getKey(keyDir);
  35. if (key == null)
  36. {
  37. Console.WriteLine("..\\code\\htk.bin has wrong file size.");
  38. return;
  39. }
  40. Console.WriteLine("Key file found!");
  41. Console.WriteLine("Looking for .nfs files...");
  42. int nfsNo = -1;
  43. while (File.Exists(dir + "\\hif_" + String.Format("{0:D6}", nfsNo + 1) + ".nfs"))
  44. {
  45. nfsNo++;
  46. }
  47. Console.WriteLine((nfsNo + 1) + " .nfs files found!");
  48. Console.WriteLine("Joining .nfs files...");
  49. Console.WriteLine();
  50. combineNFSFiles(nfsNo);
  51. string InFile = "hif.nfs";
  52. string OutFile = "hif_dec.nfs";
  53. byte[] iv = buildZero(key.Length);
  54. DecryptNFS(InFile, OutFile, key, iv);
  55. InFile = "hif_dec.nfs";
  56. OutFile = "hif_dec.iso";
  57. manipulateISO(InFile, OutFile);
  58. }
  59.  
  60. public static byte[] getKey(string keyDir)
  61. {
  62. using (var keyFile = new BinaryReader(File.OpenRead(keyDir)))
  63. {
  64. long keySize = keyFile.BaseStream.Length;
  65. if (keySize != 16)
  66. return null;
  67. return keyFile.ReadBytes(0x10);
  68. }
  69. }
  70.  
  71. public static byte[] buildZero(int size)
  72. {
  73. byte[] iv = new byte[size];
  74. for (int i = 0; i < size; i++)
  75. iv[i] = 0;
  76. return iv;
  77. }
  78.  
  79. public static void combineNFSFiles(int size)
  80. {
  81. using (var nfs = new BinaryWriter(File.OpenWrite("hif.nfs")))
  82. {
  83. for (int i = 0; i <= size; i++)
  84. {
  85. Console.WriteLine("Processing hif_" + String.Format("{0:D6}", i) + ".nfs...");
  86. var nfsTemp = new BinaryReader(File.OpenRead(Directory.GetCurrentDirectory() + "\\hif_" + String.Format("{0:D6}", i) + ".nfs"));
  87. byte[] file = new byte[nfsTemp.BaseStream.Length];
  88. long NFSsize = nfsTemp.BaseStream.Length;
  89. if (i == 0)
  90. {
  91. nfsTemp.ReadBytes(HEADER_SIZE);
  92. NFSsize -= HEADER_SIZE;
  93. }
  94. nfs.Write(nfsTemp.ReadBytes((int)NFSsize));
  95. }
  96. }
  97. }
  98.  
  99. public static void DecryptNFS(string InFile, string OutFile, byte[] key, byte[] iv)
  100. {
  101. using (var er = new BinaryReader(File.OpenRead(InFile)))
  102. using (var ew = new BinaryWriter(File.OpenWrite(OutFile)))
  103. {
  104. Console.WriteLine();
  105. Console.WriteLine("Decrypting hif.nfs...");
  106. Console.WriteLine();
  107. byte[] Sector = new byte[SECTOR_SIZE];
  108. int timer = 0;
  109. int i = 0;
  110. //init size
  111. long leftSize = er.BaseStream.Length;
  112. do
  113. {
  114. if (timer == 8000)
  115. {
  116. timer = 0;
  117. i++;
  118. Console.WriteLine((i * 256)+ " MB processed...");
  119. }
  120. timer++;
  121. //read encrypted sector
  122. Sector = er.ReadBytes(leftSize > SECTOR_SIZE ? SECTOR_SIZE : (int)leftSize);
  123.  
  124. //decrypt it, note: this is needed to reset iv
  125. Sector = aes_128_cbc_dec(key, iv, Sector);
  126.  
  127. //write it to outfile
  128. ew.Write(Sector);
  129.  
  130. //decrease remaining size
  131. leftSize -= SECTOR_SIZE;
  132.  
  133. //loop till end of file
  134. } while (leftSize > 0);
  135. }
  136. }
  137.  
  138. public static void manipulateISO(string InFile, string OutFile)
  139. {
  140. using (var er = new BinaryReader(File.OpenRead(InFile)))
  141. using (var ew = new BinaryWriter(File.OpenWrite(OutFile)))
  142. {
  143. Console.WriteLine("Write file...");
  144. ew.Write(er.ReadBytes(0x8000));
  145.  
  146. byte[] partitionTable = er.ReadBytes(0x400);
  147. ew.Write(buildZero(0x400));
  148.  
  149. ew.Write(er.ReadBytes(0xDC00));
  150.  
  151. byte[] regionSettings = er.ReadBytes(0x20);
  152. ew.Write(buildZero(0x20));
  153.  
  154. ew.Write(er.ReadBytes(0x1FDC));
  155.  
  156. byte[] magicBytes = er.ReadBytes(0x4);
  157. ew.Write(buildZero(0x4));
  158.  
  159. ew.Write(buildZero(0x28000));
  160. Console.WriteLine("Write partition table...");
  161. ew.Write(partitionTable);
  162.  
  163. ew.Write(buildZero(0xDC00));
  164. Console.WriteLine("Write region settings...");
  165. ew.Write(regionSettings);
  166.  
  167. ew.Write(buildZero(0x1FDC));
  168. Console.WriteLine("Write magic bytes...");
  169. ew.Write(magicBytes);
  170.  
  171. Console.WriteLine("Write zeros...");
  172. ew.Write(buildZero(0xF7B0000));
  173.  
  174. ew.Write(er.ReadBytes(0x1BE)); //Write start of partiton
  175. byte[] enc_titlekey = er.ReadBytes(0x10); //read encrypted titlekey
  176. ew.Write(enc_titlekey); //Write encrypted titlekey
  177. ew.Write(er.ReadBytes(0xD)); //Write bytes till titleID
  178. byte[] titleID = er.ReadBytes(0x8); //read titleID
  179. ew.Write(titleID); //WritetitleID
  180. byte[] IV = new byte[0x10]; //build IV
  181. for (int i = 0; i <= 15; i++)
  182. if (i < 8)
  183. IV[i] = titleID[i];
  184. else IV[i] = 0x0;
  185. ew.Write(er.ReadBytes(0xC1)); //Write bytes till end of ticket
  186. ew.Write(er.ReadBytes(0x1FD5C)); //Write bytes till start of partition data
  187. byte[] titlekey = aes_128_cbc_dec(WII_COMMON_KEY, IV, enc_titlekey);
  188. Console.WriteLine("Write game partition...");
  189. IV = buildZero(0x10);
  190. long size = er.BaseStream.Length - 0x50000;
  191. byte[] Sector = new byte[SECTOR_SIZE];
  192. while (size >= SECTOR_SIZE)
  193. {
  194. Sector = er.ReadBytes(SECTOR_SIZE);
  195. Sector = aes_128_cbc_enc(titlekey, IV, Sector);
  196. ew.Write(Sector);
  197. size -= SECTOR_SIZE;
  198. }
  199. Sector = er.ReadBytes((int)size);
  200. Sector = aes_128_cbc_enc(titlekey, IV, Sector);
  201. ew.Write(Sector);
  202. }
  203. }
  204.  
  205. public static byte[] aes_128_cbc_dec(byte[] key, byte[] iv, byte[] data)
  206. {
  207. byte[] result = new byte[data.Length];
  208.  
  209. try
  210. {
  211. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
  212. rm.Mode = System.Security.Cryptography.CipherMode.CBC;
  213. rm.Padding = System.Security.Cryptography.PaddingMode.None;
  214. rm.KeySize = 128;
  215. rm.BlockSize = 128;
  216. rm.Key = key;
  217. rm.IV = iv;
  218.  
  219. using (System.Security.Cryptography.ICryptoTransform itc = rm.CreateDecryptor())
  220. {
  221. result = itc.TransformFinalBlock(data, 0, data.Length);
  222. }
  223.  
  224. rm.Clear();
  225.  
  226. return result;
  227. }
  228. catch (System.Security.Cryptography.CryptographicException e)
  229. {
  230. Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  231. return null;
  232. }
  233. }
  234.  
  235. public static byte[] aes_128_cbc_enc(byte[] key, byte[] iv, byte[] data)
  236. {
  237. byte[] result = new byte[data.Length];
  238.  
  239. try
  240. {
  241. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
  242. rm.Mode = System.Security.Cryptography.CipherMode.CBC;
  243. rm.Padding = System.Security.Cryptography.PaddingMode.None;
  244. rm.KeySize = 128;
  245. rm.BlockSize = 128;
  246. rm.Key = key;
  247. rm.IV = iv;
  248.  
  249. using (System.Security.Cryptography.ICryptoTransform itc = rm.CreateEncryptor())
  250. {
  251. result = itc.TransformFinalBlock(data, 0, data.Length);
  252. }
  253.  
  254. rm.Clear();
  255.  
  256. return result;
  257. }
  258. catch (System.Security.Cryptography.CryptographicException e)
  259. {
  260. Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  261. return null;
  262. }
  263. }
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement