Guest User

Untitled

a guest
Dec 22nd, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 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.  
  15. static void Main(string[] args)
  16. {
  17. Console.WriteLine();
  18. string dir = Directory.GetCurrentDirectory();
  19. string folder = new DirectoryInfo(dir).Name;
  20. if (folder.Length != 7 || String.Compare("content", 0, folder, 0, 7, false) != 0)
  21. {
  22. Console.WriteLine("Wrong directory! Place this program in the 'content' folder.");
  23. return;
  24. }
  25. Console.WriteLine("Searching for key file...");
  26. string keyDir = Directory.GetParent(dir)+"\\code"+"\\htk.bin";
  27. if (!File.Exists(keyDir))
  28. {
  29. Console.WriteLine("Could not find the file ..\\code\\htk.bin.");
  30. return;
  31. }
  32. byte[] key = getKey(keyDir);
  33. if (key == null)
  34. {
  35. Console.WriteLine("..\\code\\htk.bin has wrong file size.");
  36. return;
  37. }
  38. Console.WriteLine("Key file found!");
  39. Console.WriteLine("Looking for .nfs files...");
  40. int nfsNo = -1;
  41. while (File.Exists(dir + "\\hif_" + String.Format("{0:D6}", nfsNo + 1) + ".nfs"))
  42. {
  43. nfsNo++;
  44. }
  45. Console.WriteLine((nfsNo + 1) + " .nfs files found!");
  46. Console.WriteLine("Joining .nfs files...");
  47. Console.WriteLine();
  48. combineNFSFiles(nfsNo);
  49. string InFile = "hif.nfs";
  50. string OutFile = "hif_dec.nfs";
  51. byte[] iv = buildZero(key.Length);
  52. DecryptNFS(InFile, OutFile, key, iv);
  53. InFile = "hif_dec.nfs";
  54. OutFile = "hif_dec.iso";
  55. manipulateISO(InFile, OutFile);
  56. }
  57.  
  58. public static byte[] getKey(string keyDir)
  59. {
  60. using (var keyFile = new BinaryReader(File.OpenRead(keyDir)))
  61. {
  62. long keySize = keyFile.BaseStream.Length;
  63. if (keySize != 16)
  64. return null;
  65. return keyFile.ReadBytes(0x10);
  66. }
  67. }
  68.  
  69. public static byte[] buildZero(int size)
  70. {
  71. byte[] iv = new byte[size];
  72. for (int i = 0; i < size; i++)
  73. iv[i] = 0;
  74. return iv;
  75. }
  76.  
  77. public static void combineNFSFiles(int size)
  78. {
  79. using (var nfs = new BinaryWriter(File.OpenWrite("hif.nfs")))
  80. {
  81. for (int i = 0; i <= size; i++)
  82. {
  83. Console.WriteLine("Processing hif_" + String.Format("{0:D6}", i) + ".nfs...");
  84. var nfsTemp = new BinaryReader(File.OpenRead(Directory.GetCurrentDirectory() + "\\hif_" + String.Format("{0:D6}", i) + ".nfs"));
  85. byte[] file = new byte[nfsTemp.BaseStream.Length];
  86. long NFSsize = nfsTemp.BaseStream.Length;
  87. if (i == 0)
  88. {
  89. nfsTemp.ReadBytes(HEADER_SIZE);
  90. NFSsize -= HEADER_SIZE;
  91. }
  92. nfs.Write(nfsTemp.ReadBytes((int)NFSsize));
  93. }
  94. }
  95. }
  96.  
  97. public static void DecryptNFS(string InFile, string OutFile, byte[] key, byte[] iv)
  98. {
  99. using (var er = new BinaryReader(File.OpenRead(InFile)))
  100. using (var ew = new BinaryWriter(File.OpenWrite(OutFile)))
  101. {
  102. Console.WriteLine();
  103. Console.WriteLine("Decrypting hif.nfs...");
  104. Console.WriteLine();
  105. byte[] Sector = new byte[SECTOR_SIZE];
  106. int timer = 0;
  107. int i = 0;
  108. //init size
  109. long leftSize = er.BaseStream.Length;
  110. do
  111. {
  112. if (timer == 8000)
  113. {
  114. timer = 0;
  115. i++;
  116. Console.WriteLine((i * 256)+ " MB processed...");
  117. }
  118. timer++;
  119. //read encrypted sector
  120. Sector = er.ReadBytes(leftSize > SECTOR_SIZE ? SECTOR_SIZE : (int)leftSize);
  121.  
  122. //decrypt it, note: this is needed to reset iv
  123. Sector = aes_128_cbc_dec(key, iv, Sector);
  124.  
  125. //write it to outfile
  126. ew.Write(Sector);
  127.  
  128. //decrease remaining size
  129. leftSize -= SECTOR_SIZE;
  130.  
  131. //loop till end of file
  132. } while (leftSize > 0);
  133. }
  134. }
  135.  
  136. public static byte[] aes_128_cbc_dec(byte[] key, byte[] iv, byte[] data)
  137. {
  138. byte[] result = new byte[data.Length];
  139.  
  140. try
  141. {
  142. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
  143. rm.Mode = System.Security.Cryptography.CipherMode.CBC;
  144. rm.Padding = System.Security.Cryptography.PaddingMode.None;
  145. rm.KeySize = 128;
  146. rm.BlockSize = 128;
  147. rm.Key = key;
  148. rm.IV = iv;
  149.  
  150. using (System.Security.Cryptography.ICryptoTransform itc = rm.CreateDecryptor())
  151. {
  152. result = itc.TransformFinalBlock(data, 0, data.Length);
  153. }
  154.  
  155. rm.Clear();
  156.  
  157. return result;
  158. }
  159. catch (System.Security.Cryptography.CryptographicException e)
  160. {
  161. Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  162. return null;
  163. }
  164. }
  165.  
  166. public static void manipulateISO(string InFile, string OutFile)
  167. {
  168. using (var er = new BinaryReader(File.OpenRead(InFile)))
  169. using (var ew = new BinaryWriter(File.OpenWrite(OutFile)))
  170. {
  171. Console.WriteLine("Write file...");
  172. ew.Write(er.ReadBytes(0x8000));
  173.  
  174. byte[] partitionTable = er.ReadBytes(0x400);
  175. ew.Write(buildZero(0x400));
  176.  
  177. ew.Write(er.ReadBytes(0xDC00));
  178.  
  179. byte[] regionSettings = er.ReadBytes(0x20);
  180. ew.Write(buildZero(0x20));
  181.  
  182. ew.Write(er.ReadBytes(0x1FDC));
  183.  
  184. byte[] magicBytes = er.ReadBytes(0x4);
  185. ew.Write(buildZero(0x4));
  186.  
  187. ew.Write(buildZero(0x28000));
  188. Console.WriteLine("Write partition table...");
  189. ew.Write(partitionTable);
  190.  
  191. ew.Write(buildZero(0xDC00));
  192. Console.WriteLine("Write region settings...");
  193. ew.Write(regionSettings);
  194.  
  195. ew.Write(buildZero(0x1FDC));
  196. Console.WriteLine("Write magic bytes...");
  197. ew.Write(magicBytes);
  198.  
  199. //Console.WriteLine("Write zeros...");
  200. //ew.Write(buildZero(0xF7B0000));
  201.  
  202. Console.WriteLine("Write game partition...");
  203. long size = er.BaseStream.Length - 0x50000;
  204. while (size > 0xFFFFFF)
  205. {
  206. ew.Write(er.ReadBytes(0xFFFFFF));
  207. size -= 0xFFFFFF;
  208. }
  209. ew.Write(er.ReadBytes((int)size));
  210. }
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment