예제 #1
0
        public void ExtractContent(Stream outputStream, ContentChunkRecord contentRecord)
        {
            if (!this.Cryptor.NormalKey.ContainsKey(0x40))
            {
                this.Cryptor.LoadTitleKeyFromTicket(this.Info.Ticket);
            }

            if (!this.Info.ActiveContents.Contains(contentRecord))
            {
                throw new ArgumentException($"The specified CIA does not contain content {contentRecord.GetContentName()}");
            }

            long offset = this.Info.Regions[CIASection.Contents].Offset;

            for (int i = 0; i < this.Info.ActiveContents.Count; i++)
            {
                if (this.Info.ActiveContents[i] == contentRecord)
                {
                    break;
                }

                offset += this.Info.ActiveContents[i].Size;
            }

            if (contentRecord.Flags.Encrypted)
            {
                if (this.Cryptor == null)
                {
                    throw new ArgumentException($"Current content ({contentRecord.GetContentName()}) is encrypted, and no Crypto Engine was passed to the method.");
                }

                using (Aes aes = Aes.Create())
                {
                    aes.Key = this.Cryptor.NormalKey[(int)Keyslot.DecryptedTitleKey];
                    byte[] cindex = BitConverter.GetBytes(contentRecord.ContentIndex);
                    aes.IV      = (BitConverter.IsLittleEndian ? cindex.FReverse() : cindex).PadRight(0x00, 0x10);
                    aes.Padding = PaddingMode.Zeros;
                    aes.Mode    = CipherMode.CBC;

                    Tools.CryptFileStreamPart(this.CIAMemoryMappedFile, outputStream, aes.CreateDecryptor(), offset, contentRecord.Size);
                }
            }
            else
            {
                using (outputStream)
                {
                    Tools.ExtractFileStreamPart(this.CIAMemoryMappedFile, outputStream, offset, contentRecord.Size);
                }
            }
        }
예제 #2
0
        public void ExtractAllContents(DirectoryInfo outputDirectory)
        {
            if (this.Info.ActiveContents.Any(ac => ac.Flags.Encrypted) && this.Cryptor == null)
            {
                throw new FileNotFoundException("Some contents in the CIA are encrypted, but no crypto engine to decrypt them has been passed to the method.");
            }

            for (int i = 0; i < this.Info.ActiveContents.Count; i++)
            {
                ContentChunkRecord currentRecord = this.Info.ActiveContents[i];

                ExtractContent(File.Create($"{outputDirectory.FullName}/{currentRecord.GetContentName()}.ncch"), currentRecord);
            }
        }